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
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
......
......@@ -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
......
# 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
......
......@@ -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
......
......@@ -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!
......
......@@ -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
......
......@@ -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
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