2016-08-18 10:31:44 -04:00
|
|
|
class FileUploader < GitlabUploader
|
2017-02-15 13:11:44 -05:00
|
|
|
include RecordsUploads
|
2015-11-14 13:29:58 -05:00
|
|
|
include UploaderHelper
|
2017-02-15 13:11:44 -05:00
|
|
|
|
2016-03-24 05:01:30 -04:00
|
|
|
MARKDOWN_PATTERN = %r{\!?\[.*?\]\(/uploads/(?<secret>[0-9a-f]{32})/(?<file>.*?)\)}
|
2015-11-14 13:29:58 -05:00
|
|
|
|
2014-05-23 04:22:00 -04:00
|
|
|
storage :file
|
|
|
|
|
2017-02-28 13:34:43 -05:00
|
|
|
def self.absolute_path(upload_record)
|
|
|
|
File.join(
|
|
|
|
self.dynamic_path_segment(upload_record.model),
|
|
|
|
upload_record.path
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-06-07 23:32:38 -04:00
|
|
|
# Not using `GitlabUploader.base_dir` because all project namespaces are in
|
|
|
|
# the `public/uploads` dir.
|
|
|
|
#
|
|
|
|
def self.base_dir
|
|
|
|
root_dir
|
|
|
|
end
|
|
|
|
|
2017-02-28 13:34:43 -05:00
|
|
|
# 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.
|
|
|
|
#
|
2017-11-21 12:34:00 -05:00
|
|
|
# model - Object that responds to `full_path` and `disk_path`
|
2017-02-28 13:34:43 -05:00
|
|
|
#
|
|
|
|
# Returns a String without a trailing slash
|
2017-12-06 06:36:11 -05:00
|
|
|
def self.dynamic_path_segment(model)
|
|
|
|
if model.hashed_storage?(:attachments)
|
|
|
|
dynamic_path_builder(model.disk_path)
|
2017-11-21 12:34:00 -05:00
|
|
|
else
|
2017-12-06 06:36:11 -05:00
|
|
|
dynamic_path_builder(model.full_path)
|
2017-11-21 12:34:00 -05:00
|
|
|
end
|
2017-02-28 13:34:43 -05:00
|
|
|
end
|
|
|
|
|
2017-11-24 03:59:21 -05:00
|
|
|
# Auxiliary method to build dynamic path segment when not using a project model
|
|
|
|
#
|
|
|
|
# Prefer to use the `.dynamic_path_segment` as it includes Hashed Storage specific logic
|
|
|
|
def self.dynamic_path_builder(path)
|
|
|
|
File.join(CarrierWave.root, base_dir, path)
|
|
|
|
end
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
attr_accessor :model
|
2017-02-23 16:54:25 -05:00
|
|
|
attr_reader :secret
|
2015-02-20 09:37:37 -05:00
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
def initialize(model, secret = nil)
|
|
|
|
@model = model
|
2017-02-23 16:54:25 -05:00
|
|
|
@secret = secret || generate_secret
|
2014-05-23 04:22:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def store_dir
|
2017-02-28 13:34:43 -05:00
|
|
|
File.join(dynamic_path_segment, @secret)
|
2014-05-23 04:22:00 -04:00
|
|
|
end
|
|
|
|
|
2017-02-28 13:34:43 -05:00
|
|
|
def relative_path
|
|
|
|
self.file.path.sub("#{dynamic_path_segment}/", '')
|
|
|
|
end
|
|
|
|
|
2016-03-30 04:56:25 -04:00
|
|
|
def to_markdown
|
|
|
|
to_h[:markdown]
|
|
|
|
end
|
|
|
|
|
2016-01-08 11:38:53 -05:00
|
|
|
def to_h
|
2016-04-03 01:00:06 -04:00
|
|
|
filename = image_or_video? ? self.file.basename : self.file.filename
|
2016-01-08 11:38:53 -05:00
|
|
|
escaped_filename = filename.gsub("]", "\\]")
|
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
markdown = "[#{escaped_filename}](#{secure_url})"
|
2017-02-13 17:42:46 -05:00
|
|
|
markdown.prepend("!") if image_or_video? || dangerous?
|
2016-01-08 11:38:53 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
alt: filename,
|
2017-02-23 16:54:25 -05:00
|
|
|
url: secure_url,
|
2016-01-08 11:38:53 -05:00
|
|
|
markdown: markdown
|
|
|
|
}
|
|
|
|
end
|
2016-03-30 06:11:27 -04:00
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
private
|
|
|
|
|
2017-02-28 13:34:43 -05:00
|
|
|
def dynamic_path_segment
|
|
|
|
self.class.dynamic_path_segment(model)
|
|
|
|
end
|
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
def generate_secret
|
2016-03-30 06:11:27 -04:00
|
|
|
SecureRandom.hex
|
|
|
|
end
|
2017-02-23 16:54:25 -05:00
|
|
|
|
|
|
|
def secure_url
|
|
|
|
File.join('/uploads', @secret, file.filename)
|
|
|
|
end
|
2014-05-23 04:22:00 -04:00
|
|
|
end
|