2017-08-17 22:00:37 +05:30
|
|
|
class FileUploader < GitlabUploader
|
|
|
|
include RecordsUploads
|
2015-11-26 14:37:03 +05:30
|
|
|
include UploaderHelper
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
MARKDOWN_PATTERN = %r{\!?\[.*?\]\(/uploads/(?<secret>[0-9a-f]{32})/(?<file>.*?)\)}
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
storage :file
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.absolute_path(upload_record)
|
|
|
|
File.join(
|
|
|
|
self.dynamic_path_segment(upload_record.model),
|
|
|
|
upload_record.path
|
|
|
|
)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Not using `GitlabUploader.base_dir` because all project namespaces are in
|
|
|
|
# the `public/uploads` dir.
|
|
|
|
#
|
|
|
|
def self.base_dir
|
|
|
|
root_dir
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Returns the part of `store_dir` that can change based on the model's current
|
|
|
|
# path
|
|
|
|
#
|
|
|
|
# This is used to build Upload paths dynamically based on the model's current
|
|
|
|
# namespace and path, allowing us to ignore renames or transfers.
|
|
|
|
#
|
|
|
|
# model - Object that responds to `path_with_namespace`
|
|
|
|
#
|
|
|
|
# Returns a String without a trailing slash
|
|
|
|
def self.dynamic_path_segment(model)
|
2017-09-10 17:25:29 +05:30
|
|
|
File.join(CarrierWave.root, base_dir, model.full_path)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
attr_accessor :model
|
|
|
|
attr_reader :secret
|
|
|
|
|
|
|
|
def initialize(model, secret = nil)
|
|
|
|
@model = model
|
|
|
|
@secret = secret || generate_secret
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def store_dir
|
|
|
|
File.join(dynamic_path_segment, @secret)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def relative_path
|
|
|
|
self.file.path.sub("#{dynamic_path_segment}/", '')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def to_markdown
|
|
|
|
to_h[:markdown]
|
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def to_h
|
2016-08-24 12:49:21 +05:30
|
|
|
filename = image_or_video? ? self.file.basename : self.file.filename
|
2016-01-14 18:37:52 +05:30
|
|
|
escaped_filename = filename.gsub("]", "\\]")
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
markdown = "[#{escaped_filename}](#{secure_url})"
|
|
|
|
markdown.prepend("!") if image_or_video? || dangerous?
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
{
|
|
|
|
alt: filename,
|
2017-08-17 22:00:37 +05:30
|
|
|
url: secure_url,
|
2016-01-14 18:37:52 +05:30
|
|
|
markdown: markdown
|
|
|
|
}
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def dynamic_path_segment
|
|
|
|
self.class.dynamic_path_segment(model)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_secret
|
2016-06-02 11:05:42 +05:30
|
|
|
SecureRandom.hex
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def secure_url
|
|
|
|
File.join('/uploads', @secret, file.filename)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|