debian-mirror-gitlab/app/uploaders/file_uploader.rb

53 lines
1.1 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
# encoding: utf-8
class FileUploader < CarrierWave::Uploader::Base
2015-11-26 14:37:03 +05:30
include UploaderHelper
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
2015-04-26 12:48:37 +05:30
attr_accessor :project, :secret
2016-06-02 11:05:42 +05:30
def initialize(project, secret = nil)
2015-04-26 12:48:37 +05:30
@project = project
2016-06-02 11:05:42 +05:30
@secret = secret || self.class.generate_secret
2014-09-02 18:07:02 +05:30
end
def base_dir
2015-04-26 12:48:37 +05:30
"uploads"
2014-09-02 18:07:02 +05:30
end
def store_dir
2015-04-26 12:48:37 +05:30
File.join(base_dir, @project.path_with_namespace, @secret)
2014-09-02 18:07:02 +05:30
end
def cache_dir
2015-04-26 12:48:37 +05:30
File.join(base_dir, 'tmp', @project.path_with_namespace, @secret)
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def secure_url
2015-10-24 18:46:33 +05:30
File.join("/uploads", @secret, file.filename)
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
def to_markdown
to_h[:markdown]
end
def to_h
2016-08-24 12:49:21 +05:30
filename = image_or_video? ? self.file.basename : self.file.filename
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{self.secure_url})"
2016-08-24 12:49:21 +05:30
markdown.prepend("!") if image_or_video?
{
alt: filename,
url: self.secure_url,
markdown: markdown
}
end
2016-06-02 11:05:42 +05:30
def self.generate_secret
SecureRandom.hex
end
2014-09-02 18:07:02 +05:30
end