2016-08-18 10:31:44 -04:00
|
|
|
class GitlabUploader < CarrierWave::Uploader::Base
|
2017-02-28 13:34:43 -05:00
|
|
|
def self.absolute_path(upload_record)
|
|
|
|
File.join(CarrierWave.root, upload_record.path)
|
|
|
|
end
|
|
|
|
|
2017-06-07 23:32:38 -04:00
|
|
|
def self.root_dir
|
2017-02-23 16:54:25 -05:00
|
|
|
'uploads'
|
|
|
|
end
|
|
|
|
|
2017-06-07 23:32:38 -04:00
|
|
|
# When object storage is used, keep the `root_dir` as `base_dir`.
|
|
|
|
# The files aren't really in folders there, they just have a name.
|
|
|
|
# The files that contain user input in their name, also contain a hash, so
|
|
|
|
# the names are still unique
|
|
|
|
#
|
|
|
|
# This method is overridden in the `FileUploader`
|
|
|
|
def self.base_dir
|
|
|
|
return root_dir unless file_storage?
|
2017-06-08 01:23:52 -04:00
|
|
|
|
2017-07-17 03:16:33 -04:00
|
|
|
File.join(root_dir, '-', 'system')
|
2017-06-07 23:32:38 -04:00
|
|
|
end
|
2017-02-23 16:54:25 -05:00
|
|
|
|
2017-06-07 23:32:38 -04:00
|
|
|
def self.file_storage?
|
2017-06-08 01:23:52 -04:00
|
|
|
self.storage == CarrierWave::Storage::File
|
2017-05-04 17:02:51 -04:00
|
|
|
end
|
|
|
|
|
2017-06-07 23:32:38 -04:00
|
|
|
delegate :base_dir, :file_storage?, to: :class
|
|
|
|
|
2017-06-01 03:52:19 -04:00
|
|
|
def file_cache_storage?
|
2017-05-04 17:02:51 -04:00
|
|
|
cache_storage.is_a?(CarrierWave::Storage::File)
|
2017-02-23 16:54:25 -05:00
|
|
|
end
|
|
|
|
|
2016-08-18 10:31:44 -04:00
|
|
|
# Reduce disk IO
|
|
|
|
def move_to_cache
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reduce disk IO
|
|
|
|
def move_to_store
|
|
|
|
true
|
|
|
|
end
|
2017-02-28 13:34:43 -05:00
|
|
|
|
|
|
|
# Designed to be overridden by child uploaders that have a dynamic path
|
|
|
|
# segment -- that is, a path that changes based on mutable attributes of its
|
|
|
|
# associated model
|
|
|
|
#
|
|
|
|
# For example, `FileUploader` builds the storage path based on the associated
|
|
|
|
# project model's `path_with_namespace` value, which can change when the
|
|
|
|
# project or its containing namespace is moved or renamed.
|
|
|
|
def relative_path
|
|
|
|
self.file.path.sub("#{root}/", '')
|
|
|
|
end
|
2017-05-01 09:14:35 -04:00
|
|
|
|
|
|
|
def exists?
|
|
|
|
file.try(:exists?)
|
|
|
|
end
|
2017-06-05 01:12:18 -04:00
|
|
|
|
|
|
|
# Override this if you don't want to save files by default to the Rails.root directory
|
|
|
|
def work_dir
|
|
|
|
# Default path set by CarrierWave:
|
|
|
|
# https://github.com/carrierwaveuploader/carrierwave/blob/v1.0.0/lib/carrierwave/uploader/cache.rb#L182
|
|
|
|
CarrierWave.tmp_path
|
|
|
|
end
|
|
|
|
|
2017-06-12 17:42:11 -04:00
|
|
|
def filename
|
|
|
|
super || file&.filename
|
|
|
|
end
|
|
|
|
|
2017-06-05 01:12:18 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# To prevent files from moving across filesystems, override the default
|
|
|
|
# implementation:
|
|
|
|
# http://github.com/carrierwaveuploader/carrierwave/blob/v1.0.0/lib/carrierwave/uploader/cache.rb#L181-L183
|
|
|
|
def workfile_path(for_file = original_filename)
|
|
|
|
# To be safe, keep this directory outside of the the cache directory
|
|
|
|
# because calling CarrierWave.clean_cache_files! will remove any files in
|
|
|
|
# the cache directory.
|
|
|
|
File.join(work_dir, @cache_id, version_name.to_s, for_file)
|
|
|
|
end
|
2016-08-18 10:31:44 -04:00
|
|
|
end
|