BigW Consortium Gitlab

gitlab_uploader.rb 2.22 KB
Newer Older
1
class GitlabUploader < CarrierWave::Uploader::Base
2 3 4 5
  def self.absolute_path(upload_record)
    File.join(CarrierWave.root, upload_record.path)
  end

6
  def self.root_dir
7 8 9
    'uploads'
  end

10 11 12 13 14 15 16 17
  # 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?
18 19

    File.join(root_dir, 'system')
20
  end
21

22
  def self.file_storage?
23
    self.storage == CarrierWave::Storage::File
24 25
  end

26 27
  delegate :base_dir, :file_storage?, to: :class

Kamil Trzcinski committed
28
  def file_cache_storage?
29
    cache_storage.is_a?(CarrierWave::Storage::File)
30 31
  end

32 33 34 35 36 37 38 39 40
  # Reduce disk IO
  def move_to_cache
    true
  end

  # Reduce disk IO
  def move_to_store
    true
  end
41 42 43 44 45 46 47 48 49 50 51

  # 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
52 53 54 55

  def exists?
    file.try(:exists?)
  end
56 57 58 59 60 61 62 63

  # 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

64 65 66 67
  def filename
    super || file&.filename
  end

68 69 70 71 72 73 74 75 76 77 78
  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
79
end