2014-05-23 04:22:00 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
class FileUploader < CarrierWave::Uploader::Base
|
2015-11-14 13:29:58 -05:00
|
|
|
include UploaderHelper
|
|
|
|
|
2014-05-23 04:22:00 -04:00
|
|
|
storage :file
|
|
|
|
|
2015-02-20 09:37:37 -05:00
|
|
|
attr_accessor :project, :secret
|
|
|
|
|
2015-02-16 13:58:40 -05:00
|
|
|
def initialize(project, secret = self.class.generate_secret)
|
|
|
|
@project = project
|
|
|
|
@secret = secret
|
2014-05-23 04:22:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def base_dir
|
2015-02-20 09:37:37 -05:00
|
|
|
"uploads"
|
2014-05-23 04:22:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def store_dir
|
2015-02-16 13:58:40 -05:00
|
|
|
File.join(base_dir, @project.path_with_namespace, @secret)
|
2014-05-23 04:22:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_dir
|
2015-02-16 13:58:40 -05:00
|
|
|
File.join(base_dir, 'tmp', @project.path_with_namespace, @secret)
|
2014-05-23 04:22:00 -04:00
|
|
|
end
|
|
|
|
|
2015-02-16 13:58:40 -05:00
|
|
|
def self.generate_secret
|
2015-02-16 16:08:55 -05:00
|
|
|
SecureRandom.hex
|
2014-05-23 04:22:00 -04:00
|
|
|
end
|
|
|
|
|
2015-02-20 09:56:12 -05:00
|
|
|
def secure_url
|
2015-10-06 10:09:03 -04:00
|
|
|
File.join("/uploads", @secret, file.filename)
|
2015-02-20 09:56:12 -05:00
|
|
|
end
|
2016-01-08 11:38:53 -05:00
|
|
|
|
|
|
|
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-05-23 04:22:00 -04:00
|
|
|
end
|