gitlab-org--gitlab-foss/app/uploaders/gitlab_uploader.rb

89 lines
1.9 KiB
Ruby
Raw Normal View History

2016-08-18 10:31:44 -04:00
class GitlabUploader < CarrierWave::Uploader::Base
2018-01-29 12:57:34 -05:00
class_attribute :options
2018-01-29 12:57:34 -05:00
class << self
# DSL setter
def storage_options(options)
self.options = options
end
2018-01-29 12:57:34 -05:00
def root
options.storage_path
end
2018-01-29 12:57:34 -05:00
# represent the directory namespacing at the class level
def base_dir
options.fetch('base_dir', '')
end
2018-01-29 12:57:34 -05:00
def file_storage?
storage == CarrierWave::Storage::File
end
def absolute_path(upload_record)
File.join(root, upload_record.path)
end
end
2018-01-29 12:57:34 -05:00
storage_options Gitlab.config.uploads
delegate :base_dir, :file_storage?, to: :class
def initialize(model, mounted_as = nil, **uploader_context)
super(model, mounted_as)
end
2017-06-01 03:52:19 -04:00
def file_cache_storage?
cache_storage.is_a?(CarrierWave::Storage::File)
end
2016-08-18 10:31:44 -04:00
# Reduce disk IO
def move_to_cache
2018-02-01 14:24:59 -05:00
file_storage?
2016-08-18 10:31:44 -04:00
end
# Reduce disk IO
def move_to_store
2018-02-01 14:24:59 -05:00
file_storage?
end
def exists?
file.present?
end
2018-01-29 12:57:34 -05:00
def store_dir
File.join(base_dir, dynamic_segment)
end
def cache_dir
File.join(root, base_dir, 'tmp/cache')
end
def work_dir
2018-01-29 12:57:34 -05:00
File.join(root, base_dir, 'tmp/work')
end
def filename
super || file&.filename
end
private
2018-01-29 12:57:34 -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
def dynamic_segment
raise(NotImplementedError)
end
# 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.
2018-01-29 12:57:34 -05:00
File.join(work_dir, cache_id, version_name.to_s, for_file)
end
2016-08-18 10:31:44 -04:00
end