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

49 lines
978 B
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
# encoding: utf-8
class FileUploader < CarrierWave::Uploader::Base
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
2015-04-26 12:48:37 +05:30
def file_storage?
self.class.storage == CarrierWave::Storage::File
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def image?
img_ext = %w(png jpg jpeg gif bmp tiff)
if file.respond_to?(:extension)
img_ext.include?(file.extension.downcase)
else
# Not all CarrierWave storages respond to :extension
ext = file.path.split('.').last.downcase
img_ext.include?(ext)
end
rescue
false
2014-09-02 18:07:02 +05:30
end
end