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

49 lines
954 B
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
2014-09-02 18:07:02 +05:30
storage :file
2015-04-26 12:48:37 +05:30
attr_accessor :project, :secret
def initialize(project, secret = self.class.generate_secret)
@project = project
@secret = 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 self.generate_secret
SecureRandom.hex
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
def to_h
filename = image? ? self.file.basename : self.file.filename
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{self.secure_url})"
markdown.prepend("!") if image?
{
alt: filename,
url: self.secure_url,
is_image: image?,
markdown: markdown
}
end
2014-09-02 18:07:02 +05:30
end