Class: GitlabUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
ContentTypeWhitelist::Concern
Defined in:
app/uploaders/gitlab_uploader.rb

Constant Summary collapse

PROTECTED_METHODS =
%i(filename cache_dir work_dir store_dir).freeze
ObjectNotReadyError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, mounted_as = nil, **uploader_context) ⇒ GitlabUploader

Returns a new instance of GitlabUploader.


42
43
44
# File 'app/uploaders/gitlab_uploader.rb', line 42

def initialize(model, mounted_as = nil, **uploader_context)
  super(model, mounted_as)
end

Class Method Details

.absolute_path(upload_record) ⇒ Object


31
32
33
# File 'app/uploaders/gitlab_uploader.rb', line 31

def absolute_path(upload_record)
  File.join(root, upload_record.path)
end

.base_dirObject

represent the directory namespacing at the class level


23
24
25
# File 'app/uploaders/gitlab_uploader.rb', line 23

def base_dir
  options.fetch('base_dir', '')
end

.file_storage?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'app/uploaders/gitlab_uploader.rb', line 27

def file_storage?
  storage == CarrierWave::Storage::File
end

.rootObject


18
19
20
# File 'app/uploaders/gitlab_uploader.rb', line 18

def root
  options.storage_path
end

.storage_options(options) ⇒ Object

DSL setter


14
15
16
# File 'app/uploaders/gitlab_uploader.rb', line 14

def storage_options(options)
  self.options = options
end

Instance Method Details

#cache_dirObject


62
63
64
# File 'app/uploaders/gitlab_uploader.rb', line 62

def cache_dir
  File.join(root, base_dir, 'tmp/cache')
end

#cached_sizeObject


88
89
90
# File 'app/uploaders/gitlab_uploader.rb', line 88

def cached_size
  size
end

#exists?Boolean

Returns:

  • (Boolean)

58
59
60
# File 'app/uploaders/gitlab_uploader.rb', line 58

def exists?
  file.present?
end

#file_cache_storage?Boolean

Returns:

  • (Boolean)

46
47
48
# File 'app/uploaders/gitlab_uploader.rb', line 46

def file_cache_storage?
  cache_storage.is_a?(CarrierWave::Storage::File)
end

#filenameObject


70
71
72
# File 'app/uploaders/gitlab_uploader.rb', line 70

def filename
  super || file&.filename
end

#local_urlObject


84
85
86
# File 'app/uploaders/gitlab_uploader.rb', line 84

def local_url
  File.join('/', self.class.base_dir, dynamic_segment, filename)
end

#model_valid?Boolean

Returns:

  • (Boolean)

80
81
82
# File 'app/uploaders/gitlab_uploader.rb', line 80

def model_valid?
  !!model
end

#move_to_cacheObject


50
51
52
# File 'app/uploaders/gitlab_uploader.rb', line 50

def move_to_cache
  file_storage?
end

#move_to_storeObject


54
55
56
# File 'app/uploaders/gitlab_uploader.rb', line 54

def move_to_store
  file_storage?
end

#openObject


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/uploaders/gitlab_uploader.rb', line 92

def open
  stream =
    if file_storage?
      File.open(path, "rb") if path
    else
      ::Gitlab::HttpIO.new(url, cached_size) if url
    end

  return unless stream
  return stream unless block_given?

  begin
    yield(stream)
  ensure
    stream.close
  end
end

#relative_pathObject


74
75
76
77
78
# File 'app/uploaders/gitlab_uploader.rb', line 74

def relative_path
  return path if pathname.relative?

  pathname.relative_path_from(Pathname.new(root))
end

#replace_file_without_saving!(file) ⇒ Object

Used to replace an existing upload with another file without modifying stored metadata Use this method only to repair/replace an existing upload, or to upload to a Geo secondary node

Parameters:

  • file (CarrierWave::SanitizedFile)

    that will replace existing upload

Returns:

  • CarrierWave::SanitizedFile

Raises:

  • (ArgumentError)

115
116
117
118
119
# File 'app/uploaders/gitlab_uploader.rb', line 115

def replace_file_without_saving!(file)
  raise ArgumentError, 'should be a CarrierWave::SanitizedFile' unless file.is_a? CarrierWave::SanitizedFile

  storage.store!(file)
end

#url_or_file_path(url_options = {}) ⇒ Object


121
122
123
124
125
126
127
# File 'app/uploaders/gitlab_uploader.rb', line 121

def url_or_file_path(url_options = {})
  if file_storage?
    'file://' + path
  else
    url(url_options)
  end
end

#work_dirObject


66
67
68
# File 'app/uploaders/gitlab_uploader.rb', line 66

def work_dir
  File.join(root, base_dir, 'tmp/work')
end