Commit 63e9969c authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Refactor upload service to return uploader

Also changes old calls to the service
parent ccbc4555
...@@ -7,7 +7,7 @@ module UploadsActions ...@@ -7,7 +7,7 @@ module UploadsActions
UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo favicon).freeze UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo favicon).freeze
def create 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| respond_to do |format|
if link_to_file if link_to_file
......
...@@ -37,6 +37,8 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -37,6 +37,8 @@ class Projects::IssuesController < Projects::ApplicationController
# Allow create a new branch and empty WIP merge request from current issue # 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_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] before_action :set_suggested_issues_feature_flags, only: [:new]
respond_to :html respond_to :html
...@@ -176,12 +178,10 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -176,12 +178,10 @@ class Projects::IssuesController < Projects::ApplicationController
end end
def import_csv def import_csv
return render_404 unless Feature.enabled?(:issues_import_csv) && can?(current_user, :import_issues, project) return render_404 unless Feature.enabled?(:issues_import_csv)
service = UploadService.new(project, params[:file])
if service.execute if uploader = UploadService.new(project, params[:file]).execute
ImportIssuesCsvWorker.perform_async(current_user.id, project.id, service.uploader.upload.id) 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.") flash[:notice] = _("Your issues are being imported. Once finished, you'll get a confirmation email.")
else else
......
# frozen_string_literal: true # frozen_string_literal: true
class UploadService class UploadService
attr_accessor :uploader
def initialize(model, file, uploader_class = FileUploader, **uploader_context) def initialize(model, file, uploader_class = FileUploader, **uploader_context)
@model, @file, @uploader_class, @uploader_context = model, file, uploader_class, uploader_context @model, @file, @uploader_class, @uploader_context = model, file, uploader_class, uploader_context
end end
...@@ -10,10 +8,10 @@ class UploadService ...@@ -10,10 +8,10 @@ class UploadService
def execute def execute
return nil unless @file && @file.size <= max_attachment_size return nil unless @file && @file.size <= max_attachment_size
@uploader = @uploader_class.new(@model, nil, @uploader_context) uploader = @uploader_class.new(@model, nil, @uploader_context)
@uploader.store!(@file) uploader.store!(@file)
@uploader.to_h uploader
end end
private private
......
...@@ -475,7 +475,7 @@ module API ...@@ -475,7 +475,7 @@ module API
requires :file, type: File, desc: 'The file to be uploaded' requires :file, type: File, desc: 'The file to be uploaded'
end end
post ":id/uploads" do post ":id/uploads" do
UploadService.new(user_project, params[:file]).execute UploadService.new(user_project, params[:file]).execute.to_h
end end
desc 'Get the users list of a project' do desc 'Get the users list of a project' do
......
...@@ -23,7 +23,7 @@ module Gitlab ...@@ -23,7 +23,7 @@ module Gitlab
content_type: attachment.content_type content_type: attachment.content_type
} }
link = UploadService.new(project, file).execute link = UploadService.new(project, file).execute.to_h
attachments << link if link attachments << link if link
ensure ensure
tmp.close! tmp.close!
......
...@@ -40,7 +40,7 @@ module Gitlab ...@@ -40,7 +40,7 @@ module Gitlab
def add_upload(upload) def add_upload(upload)
uploader_context = FileUploader.extract_dynamic_path(upload).named_captures.symbolize_keys 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 end
def copy_project_uploads def copy_project_uploads
......
...@@ -68,6 +68,6 @@ describe UploadService do ...@@ -68,6 +68,6 @@ describe UploadService do
end end
def upload_file(project, file) def upload_file(project, file)
described_class.new(project, file, FileUploader).execute described_class.new(project, file, FileUploader).execute.to_h
end end
end end
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