Move logic to image_service.

This commit is contained in:
Marin Jankovski 2014-05-26 14:53:34 +02:00
parent 8bec6b0bcb
commit 3b2b3cff04
3 changed files with 43 additions and 12 deletions

View file

@ -163,19 +163,11 @@ class ProjectsController < ApplicationController
end
def upload_image
uploader = FileUploader.new('uploads', upload_path, accepted_images)
image = params['markdown_img']
if image && accepted_images.map{ |format| image.content_type.include? format }.any?
alt = image.original_filename
uploader.store!(image)
link = { 'alt' => File.basename(alt, '.*'),
'url' => File.join(root_url, uploader.url) }
end
link_to_image = ::Projects::ImageService.new(repository, params, root_url).execute
respond_to do |format|
if link
format.json { render json: { link: link } }
if link_to_image
format.json { render json: { link: link_to_image } }
else
format.json { render json: "Invalid file.", status: :unprocessable_entity }
end

View file

@ -0,0 +1,39 @@
module Projects
class ImageService < BaseService
include Rails.application.routes.url_helpers
def initialize(repository, params, root_url)
@repository, @params, @root_url = repository, params.dup, root_url
end
def execute
uploader = FileUploader.new('uploads', upload_path, accepted_images)
image = @params['markdown_img']
if image && correct_mime_type?(image)
alt = image.original_filename
uploader.store!(image)
link = {
'alt' => File.basename(alt, '.*'),
'url' => File.join(@root_url, uploader.url)
}
else
link = nil
end
end
protected
def upload_path
base_dir = FileUploader.generate_dir
File.join(@repository.path_with_namespace, base_dir)
end
def accepted_images
%w(png jpg jpeg gif)
end
def correct_mime_type?(image)
accepted_images.map{ |format| image.content_type.include? format }.any?
end
end
end

View file

@ -6,7 +6,7 @@ describe Projects::CommitsController do
before do
sign_in(user)
project.creator = user
project.team << [user, :master]
end
describe "GET show" do