Move logic to image_service.
This commit is contained in:
parent
8bec6b0bcb
commit
3b2b3cff04
3 changed files with 43 additions and 12 deletions
|
@ -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
|
||||
|
|
39
app/services/projects/image_service.rb
Normal file
39
app/services/projects/image_service.rb
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue