Commit 3b2b3cff authored by Marin Jankovski's avatar Marin Jankovski

Move logic to image_service.

parent 8bec6b0b
...@@ -163,19 +163,11 @@ class ProjectsController < ApplicationController ...@@ -163,19 +163,11 @@ class ProjectsController < ApplicationController
end end
def upload_image def upload_image
uploader = FileUploader.new('uploads', upload_path, accepted_images) link_to_image = ::Projects::ImageService.new(repository, params, root_url).execute
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
respond_to do |format| respond_to do |format|
if link if link_to_image
format.json { render json: { link: link } } format.json { render json: { link: link_to_image } }
else else
format.json { render json: "Invalid file.", status: :unprocessable_entity } format.json { render json: "Invalid file.", status: :unprocessable_entity }
end end
......
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 ...@@ -6,7 +6,7 @@ describe Projects::CommitsController do
before do before do
sign_in(user) sign_in(user)
project.creator = user project.team << [user, :master]
end end
describe "GET show" do describe "GET show" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment