Commit 3b2b3cff04993c7247e953c10aa8c6fb5e8d6ddb
1 parent
8bec6b0b
Exists in
spb-stable
and in
2 other branches
Move logic to image_service.
Showing
3 changed files
with
43 additions
and
12 deletions
Show diff stats
app/controllers/projects_controller.rb
| ... | ... | @@ -163,19 +163,11 @@ class ProjectsController < ApplicationController |
| 163 | 163 | end |
| 164 | 164 | |
| 165 | 165 | def upload_image |
| 166 | - uploader = FileUploader.new('uploads', upload_path, accepted_images) | |
| 167 | - image = params['markdown_img'] | |
| 168 | - | |
| 169 | - if image && accepted_images.map{ |format| image.content_type.include? format }.any? | |
| 170 | - alt = image.original_filename | |
| 171 | - uploader.store!(image) | |
| 172 | - link = { 'alt' => File.basename(alt, '.*'), | |
| 173 | - 'url' => File.join(root_url, uploader.url) } | |
| 174 | - end | |
| 166 | + link_to_image = ::Projects::ImageService.new(repository, params, root_url).execute | |
| 175 | 167 | |
| 176 | 168 | respond_to do |format| |
| 177 | - if link | |
| 178 | - format.json { render json: { link: link } } | |
| 169 | + if link_to_image | |
| 170 | + format.json { render json: { link: link_to_image } } | |
| 179 | 171 | else |
| 180 | 172 | format.json { render json: "Invalid file.", status: :unprocessable_entity } |
| 181 | 173 | end | ... | ... |
| ... | ... | @@ -0,0 +1,39 @@ |
| 1 | +module Projects | |
| 2 | + class ImageService < BaseService | |
| 3 | + include Rails.application.routes.url_helpers | |
| 4 | + def initialize(repository, params, root_url) | |
| 5 | + @repository, @params, @root_url = repository, params.dup, root_url | |
| 6 | + end | |
| 7 | + | |
| 8 | + def execute | |
| 9 | + uploader = FileUploader.new('uploads', upload_path, accepted_images) | |
| 10 | + image = @params['markdown_img'] | |
| 11 | + | |
| 12 | + if image && correct_mime_type?(image) | |
| 13 | + alt = image.original_filename | |
| 14 | + uploader.store!(image) | |
| 15 | + link = { | |
| 16 | + 'alt' => File.basename(alt, '.*'), | |
| 17 | + 'url' => File.join(@root_url, uploader.url) | |
| 18 | + } | |
| 19 | + else | |
| 20 | + link = nil | |
| 21 | + end | |
| 22 | + end | |
| 23 | + | |
| 24 | + protected | |
| 25 | + | |
| 26 | + def upload_path | |
| 27 | + base_dir = FileUploader.generate_dir | |
| 28 | + File.join(@repository.path_with_namespace, base_dir) | |
| 29 | + end | |
| 30 | + | |
| 31 | + def accepted_images | |
| 32 | + %w(png jpg jpeg gif) | |
| 33 | + end | |
| 34 | + | |
| 35 | + def correct_mime_type?(image) | |
| 36 | + accepted_images.map{ |format| image.content_type.include? format }.any? | |
| 37 | + end | |
| 38 | + end | |
| 39 | +end | ... | ... |