diff options
author | Heinrich Lee Yu <hleeyu@gmail.com> | 2018-12-31 11:23:50 +0800 |
---|---|---|
committer | Heinrich Lee Yu <hleeyu@gmail.com> | 2019-01-07 11:16:58 +0800 |
commit | 63e9969ca3ac57839b78d9cc44bcf32bc9a45248 (patch) | |
tree | cda51e40b5ea915d26425edfce815ca6fae991e9 | |
parent | ccbc45559b7bedd1d76c1840bbdfba3cce542af7 (diff) | |
download | gitlab-ce-63e9969ca3ac57839b78d9cc44bcf32bc9a45248.tar.gz |
Refactor upload service to return uploader
Also changes old calls to the service
-rw-r--r-- | app/controllers/concerns/uploads_actions.rb | 2 | ||||
-rw-r--r-- | app/controllers/projects/issues_controller.rb | 10 | ||||
-rw-r--r-- | app/services/upload_service.rb | 8 | ||||
-rw-r--r-- | lib/api/projects.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/email/attachment_uploader.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/import_export/uploads_manager.rb | 2 | ||||
-rw-r--r-- | spec/services/upload_service_spec.rb | 2 |
7 files changed, 13 insertions, 15 deletions
diff --git a/app/controllers/concerns/uploads_actions.rb b/app/controllers/concerns/uploads_actions.rb index 0eea0cdd50f..5992ad5f1e1 100644 --- a/app/controllers/concerns/uploads_actions.rb +++ b/app/controllers/concerns/uploads_actions.rb @@ -7,7 +7,7 @@ module UploadsActions UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo favicon).freeze def create - link_to_file = UploadService.new(model, params[:file], uploader_class).execute + link_to_file = UploadService.new(model, params[:file], uploader_class).execute.to_h respond_to do |format| if link_to_file diff --git a/app/controllers/projects/issues_controller.rb b/app/controllers/projects/issues_controller.rb index f88eb9e0322..21688e54481 100644 --- a/app/controllers/projects/issues_controller.rb +++ b/app/controllers/projects/issues_controller.rb @@ -37,6 +37,8 @@ class Projects::IssuesController < Projects::ApplicationController # Allow create a new branch and empty WIP merge request from current issue before_action :authorize_create_merge_request_from!, only: [:create_merge_request] + before_action :authorize_import_issues!, only: [:import_csv] + before_action :set_suggested_issues_feature_flags, only: [:new] respond_to :html @@ -176,12 +178,10 @@ class Projects::IssuesController < Projects::ApplicationController end def import_csv - return render_404 unless Feature.enabled?(:issues_import_csv) && can?(current_user, :import_issues, project) - - service = UploadService.new(project, params[:file]) + return render_404 unless Feature.enabled?(:issues_import_csv) - if service.execute - ImportIssuesCsvWorker.perform_async(current_user.id, project.id, service.uploader.upload.id) + if uploader = UploadService.new(project, params[:file]).execute + ImportIssuesCsvWorker.perform_async(current_user.id, project.id, uploader.upload.id) flash[:notice] = _("Your issues are being imported. Once finished, you'll get a confirmation email.") else diff --git a/app/services/upload_service.rb b/app/services/upload_service.rb index 47903eb48c3..41ca95b3b6f 100644 --- a/app/services/upload_service.rb +++ b/app/services/upload_service.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true class UploadService - attr_accessor :uploader - def initialize(model, file, uploader_class = FileUploader, **uploader_context) @model, @file, @uploader_class, @uploader_context = model, file, uploader_class, uploader_context end @@ -10,10 +8,10 @@ class UploadService def execute return nil unless @file && @file.size <= max_attachment_size - @uploader = @uploader_class.new(@model, nil, @uploader_context) - @uploader.store!(@file) + uploader = @uploader_class.new(@model, nil, @uploader_context) + uploader.store!(@file) - @uploader.to_h + uploader end private diff --git a/lib/api/projects.rb b/lib/api/projects.rb index f5d21d8923f..9f3a1699146 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -475,7 +475,7 @@ module API requires :file, type: File, desc: 'The file to be uploaded' end post ":id/uploads" do - UploadService.new(user_project, params[:file]).execute + UploadService.new(user_project, params[:file]).execute.to_h end desc 'Get the users list of a project' do diff --git a/lib/gitlab/email/attachment_uploader.rb b/lib/gitlab/email/attachment_uploader.rb index a826519b2dd..bffea697f9a 100644 --- a/lib/gitlab/email/attachment_uploader.rb +++ b/lib/gitlab/email/attachment_uploader.rb @@ -23,7 +23,7 @@ module Gitlab content_type: attachment.content_type } - link = UploadService.new(project, file).execute + link = UploadService.new(project, file).execute.to_h attachments << link if link ensure tmp.close! diff --git a/lib/gitlab/import_export/uploads_manager.rb b/lib/gitlab/import_export/uploads_manager.rb index 474e9d45566..e232198150a 100644 --- a/lib/gitlab/import_export/uploads_manager.rb +++ b/lib/gitlab/import_export/uploads_manager.rb @@ -40,7 +40,7 @@ module Gitlab def add_upload(upload) uploader_context = FileUploader.extract_dynamic_path(upload).named_captures.symbolize_keys - UploadService.new(@project, File.open(upload, 'r'), FileUploader, uploader_context).execute + UploadService.new(@project, File.open(upload, 'r'), FileUploader, uploader_context).execute.to_h end def copy_project_uploads diff --git a/spec/services/upload_service_spec.rb b/spec/services/upload_service_spec.rb index 9b232a52efa..57382c4bc8d 100644 --- a/spec/services/upload_service_spec.rb +++ b/spec/services/upload_service_spec.rb @@ -68,6 +68,6 @@ describe UploadService do end def upload_file(project, file) - described_class.new(project, file, FileUploader).execute + described_class.new(project, file, FileUploader).execute.to_h end end |