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
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
File.join(CarrierWave.root, base_dir, model.path_with_namespace)
|
|
|
|
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
|