2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
class UploadService
|
2021-03-25 08:09:19 -04:00
|
|
|
# Temporarily introduced for upload API: https://gitlab.com/gitlab-org/gitlab/-/issues/325788
|
|
|
|
attr_accessor :override_max_attachment_size
|
|
|
|
|
2018-07-11 08:52:48 -04:00
|
|
|
def initialize(model, file, uploader_class = FileUploader, **uploader_context)
|
2021-04-19 17:09:27 -04:00
|
|
|
@model = model
|
|
|
|
@file = file
|
|
|
|
@uploader_class = uploader_class
|
|
|
|
@uploader_context = uploader_context
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2020-12-09 22:09:53 -05:00
|
|
|
return unless file && file.size <= max_attachment_size
|
2017-05-01 09:14:35 -04:00
|
|
|
|
2020-12-09 22:09:53 -05:00
|
|
|
uploader = uploader_class.new(model, nil, **uploader_context)
|
|
|
|
uploader.store!(file)
|
2017-05-01 09:14:35 -04:00
|
|
|
|
2018-12-30 22:23:50 -05:00
|
|
|
uploader
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-12-09 22:09:53 -05:00
|
|
|
attr_reader :model, :file, :uploader_class, :uploader_context
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
def max_attachment_size
|
2021-03-25 08:09:19 -04:00
|
|
|
override_max_attachment_size || Gitlab::CurrentSettings.max_attachment_size.megabytes.to_i
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|