2015-02-16 13:58:40 -05:00
|
|
|
module Projects
|
|
|
|
class UploadService < BaseService
|
|
|
|
def initialize(project, file)
|
|
|
|
@project, @file = project, file
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2015-03-20 08:11:12 -04:00
|
|
|
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
|
|
|
{
|
2015-08-19 13:18:05 -04: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
|
2015-03-20 08:11:12 -04:00
|
|
|
|
|
|
|
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
|