diff options
author | Marin Jankovski <marin@gitlab.com> | 2014-05-26 16:53:34 +0400 |
---|---|---|
committer | Marin Jankovski <marin@gitlab.com> | 2014-05-26 16:54:03 +0400 |
commit | 3b2b3cff04993c7247e953c10aa8c6fb5e8d6ddb (patch) | |
tree | 4d5112ed8e0066a516bf9d46dbc8029ed02e31eb /app/services | |
parent | 8bec6b0bcb100b30a43fcd9c6649d1bee113b6a7 (diff) |
Move logic to image_service.
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/projects/image_service.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/app/services/projects/image_service.rb b/app/services/projects/image_service.rb new file mode 100644 index 00000000000..c79ddddd972 --- /dev/null +++ b/app/services/projects/image_service.rb @@ -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 |