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
|
|
|
|
return nil unless @file && @file.size <= max_attachment_size
|
|
|
|
|
2018-07-11 08:52:48 -04:00
|
|
|
uploader = @uploader_class.new(@model, nil, @uploader_context)
|
2017-05-01 09:14:35 -04:00
|
|
|
uploader.store!(@file)
|
|
|
|
|
|
|
|
uploader.to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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
|