2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
class UploadService
|
2021-04-29 21:17:54 +05:30
|
|
|
# Temporarily introduced for upload API: https://gitlab.com/gitlab-org/gitlab/-/issues/325788
|
|
|
|
attr_accessor :override_max_attachment_size
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def initialize(model, file, uploader_class = FileUploader, **uploader_context)
|
2021-04-29 21:17:54 +05:30
|
|
|
@model = model
|
|
|
|
@file = file
|
|
|
|
@uploader_class = uploader_class
|
|
|
|
@uploader_context = uploader_context
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-02-22 17:27:13 +05:30
|
|
|
return unless file && file.size <= max_attachment_size
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
uploader = uploader_class.new(model, nil, **uploader_context)
|
|
|
|
uploader.store!(file)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
uploader
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
attr_reader :model, :file, :uploader_class, :uploader_context
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def max_attachment_size
|
2021-04-29 21:17:54 +05:30
|
|
|
override_max_attachment_size || Gitlab::CurrentSettings.max_attachment_size.megabytes.to_i
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|