diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-02-16 19:58:40 +0100 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-02-17 22:23:31 +0100 |
commit | d2ebdf664b42d4fac6b2e060ef79aa9fe0b0e72d (patch) | |
tree | 061aa99c9a95b506a4ee665c25bd1d83a467c46f /app/services/projects | |
parent | 192e7306626e073a5e6fb3b41d69f0e3fddb0821 (diff) | |
download | gitlab-ce-d2ebdf664b42d4fac6b2e060ef79aa9fe0b0e72d.tar.gz |
Refactor.
Diffstat (limited to 'app/services/projects')
-rw-r--r-- | app/services/projects/file_service.rb | 55 | ||||
-rw-r--r-- | app/services/projects/upload_service.rb | 22 |
2 files changed, 22 insertions, 55 deletions
diff --git a/app/services/projects/file_service.rb b/app/services/projects/file_service.rb deleted file mode 100644 index 8c149bf53a1..00000000000 --- a/app/services/projects/file_service.rb +++ /dev/null @@ -1,55 +0,0 @@ -module Projects - class FileService < 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("#{Rails.root}/uploads", upload_path, accepted_files) - file = @params['markdown_file'] - - if file - alt = file.original_filename - uploader.store!(file) - filename = nil - if image?(file) - filename=File.basename(alt, '.*') - else - filename=File.basename(alt) - end - link = { - 'alt' => filename, - 'url' => uploader.secure_url, - 'is_image' => image?(file) - } - else - link = nil - end - end - - protected - - def accepted_files - # insert accepted mime types here (e.g %w(jpg jpeg gif png)) - nil - end - - def accepted_images - %w(jpg jpeg gif png) - end - - def image?(file) - accepted_images.map { |format| file.content_type.include? format }.any? - end - - def upload_path - base_dir = FileUploader.generate_dir - File.join(@repository.path_with_namespace, base_dir) - end - - def correct_mime_type?(file) - accepted_files.map { |format| image.content_type.include? format }.any? - end - end -end diff --git a/app/services/projects/upload_service.rb b/app/services/projects/upload_service.rb new file mode 100644 index 00000000000..a186c97628f --- /dev/null +++ b/app/services/projects/upload_service.rb @@ -0,0 +1,22 @@ +module Projects + class UploadService < BaseService + def initialize(project, file) + @project, @file = project, file + end + + def execute + return nil unless @file + + uploader = FileUploader.new(@project) + uploader.store!(@file) + + filename = uploader.image? ? uploader.file.basename : uploader.file.filename + + { + 'alt' => filename, + 'url' => uploader.secure_url, + 'is_image' => uploader.image? + } + end + end +end |