2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
class UploadService
|
2018-07-11 08:52:48 -04:00
|
|
|
def initialize(model, file, uploader_class = FileUploader, **uploader_context)
|
|
|
|
@model, @file, @uploader_class, @uploader_context = model, file, uploader_class, 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
|
2018-02-02 13:39:55 -05:00
|
|
|
Gitlab::CurrentSettings.max_attachment_size.megabytes.to_i
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|