gitlab-org--gitlab-foss/app/uploaders/file_uploader.rb

51 lines
1 KiB
Ruby
Raw Normal View History

2016-08-18 10:31:44 -04:00
class FileUploader < GitlabUploader
include UploaderHelper
MARKDOWN_PATTERN = %r{\!?\[.*?\]\(/uploads/(?<secret>[0-9a-f]{32})/(?<file>.*?)\)}
storage :file
attr_accessor :project
attr_reader :secret
def initialize(project, secret = nil)
2015-02-16 13:58:40 -05:00
@project = project
@secret = secret || generate_secret
end
def store_dir
2015-02-16 13:58:40 -05:00
File.join(base_dir, @project.path_with_namespace, @secret)
end
def cache_dir
2015-02-16 13:58:40 -05:00
File.join(base_dir, 'tmp', @project.path_with_namespace, @secret)
end
def to_markdown
to_h[:markdown]
end
2016-01-08 11:38:53 -05:00
def to_h
filename = image_or_video? ? self.file.basename : self.file.filename
2016-01-08 11:38:53 -05:00
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{secure_url})"
markdown.prepend("!") if image_or_video? || dangerous?
2016-01-08 11:38:53 -05:00
{
alt: filename,
url: secure_url,
2016-01-08 11:38:53 -05:00
markdown: markdown
}
end
private
def generate_secret
SecureRandom.hex
end
def secure_url
File.join('/uploads', @secret, file.filename)
end
end