gitlab-org--gitlab-foss/app/services/projects/upload_service.rb

34 lines
823 B
Ruby
Raw Normal View History

2015-02-16 13:58:40 -05:00
module Projects
class UploadService < BaseService
def initialize(project, file)
@project, @file = project, file
end
def execute
return nil unless @file and @file.size <= max_attachment_size
2015-02-16 13:58:40 -05:00
uploader = FileUploader.new(@project)
uploader.store!(@file)
filename = uploader.image? ? uploader.file.basename : uploader.file.filename
2016-01-07 07:37:14 -05:00
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{uploader.secure_url})"
markdown.prepend("!") if uploader.image?
2015-02-16 13:58:40 -05:00
{
alt: filename,
url: uploader.secure_url,
2016-01-07 07:37:14 -05:00
is_image: uploader.image?,
markdown: markdown
2015-02-16 13:58:40 -05:00
}
end
private
def max_attachment_size
current_application_settings.max_attachment_size.megabytes.to_i
end
2015-02-16 13:58:40 -05:00
end
end