Class: Upload

Inherits:
ApplicationRecord show all
Includes:
Checksummable
Defined in:
app/models/upload.rb

Constant Summary collapse

CHECKSUM_THRESHOLD =

Upper limit for foreground checksum processing

100.megabytes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Checksummable

#crc32

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.begin_fast_destroyObject

FastDestroyAll concerns


43
44
45
46
47
48
# File 'app/models/upload.rb', line 43

def begin_fast_destroy
  {
    Uploads::Local => Uploads::Local.new.keys(with_files_stored_locally),
    Uploads::Fog => Uploads::Fog.new.keys(with_files_stored_remotely)
  }
end

.finalize_fast_destroy(keys) ⇒ Object

FastDestroyAll concerns


52
53
54
55
56
# File 'app/models/upload.rb', line 52

def finalize_fast_destroy(keys)
  keys.each do |store_class, paths|
    store_class.new.delete_keys_async(paths)
  end
end

.inner_join_local_uploads_projectsObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/upload.rb', line 26

def inner_join_local_uploads_projects
  upload_table = Upload.arel_table
  project_table = Project.arel_table

  join_statement = upload_table.project(upload_table[Arel.star])
                     .join(project_table)
                     .on(
                       upload_table[:model_type].eq('Project')
                         .and(upload_table[:model_id].eq(project_table[:id]))
                         .and(upload_table[:store].eq(ObjectStorage::Store::LOCAL))
                     )

  joins(join_statement.join_sources)
end

Instance Method Details

#absolute_pathObject


59
60
61
62
63
64
# File 'app/models/upload.rb', line 59

def absolute_path
  raise ObjectStorage::RemoteStoreError, _("Remote object has no absolute path.") unless local?
  return path unless relative_path?

  uploader_class.absolute_path(self)
end

#build_uploader(mounted_as = nil) ⇒ GitlabUploader

Initialize the associated Uploader class with current model

Parameters:

  • mounted_as (String) (defaults to: nil)

Returns:

  • (GitlabUploader)

    one of the subclasses, defined at the model's uploader attribute


77
78
79
80
81
# File 'app/models/upload.rb', line 77

def build_uploader(mounted_as = nil)
  uploader_class.new(model, mounted_as || mount_point).tap do |uploader|
    uploader.upload = self
  end
end

#calculate_checksum!Object


66
67
68
69
70
71
# File 'app/models/upload.rb', line 66

def calculate_checksum!
  self.checksum = nil
  return unless needs_checksum?

  self.checksum = self.class.hexdigest(absolute_path)
end

#exist?Boolean

This checks for existence of the upload on storage

Returns:

  • (Boolean)

    whether upload exists on storage


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/upload.rb', line 97

def exist?
  exist = if local?
            File.exist?(absolute_path)
          else
            retrieve_uploader.exists?
          end

  # Help sysadmins find missing upload files
  if persisted? && !exist
    exception = RuntimeError.new("Uploaded file does not exist")
    Gitlab::ErrorTracking.track_exception(exception, self.attributes)
    Gitlab::Metrics.counter(:upload_file_does_not_exist_total, _('The number of times an upload record could not find its file')).increment
  end

  exist
end

#local?Boolean

Returns:

  • (Boolean)

121
122
123
# File 'app/models/upload.rb', line 121

def local?
  store == ObjectStorage::Store::LOCAL
end

#needs_checksum?Boolean

Returns whether generating checksum is needed

This takes into account whether file exists, if any checksum exists or if the storage has checksum generation code implemented

Returns:

  • (Boolean)

    whether generating a checksum is needed


131
132
133
# File 'app/models/upload.rb', line 131

def needs_checksum?
  checksum.nil? && local? && exist?
end

#retrieve_uploader(mounted_as = nil) ⇒ GitlabUploader

Initialize the associated Uploader class with current model and retrieve existing file from the store to a local cache

Parameters:

  • mounted_as (String) (defaults to: nil)

Returns:

  • (GitlabUploader)

    one of the subclasses, defined at the model's uploader attribute


88
89
90
91
92
# File 'app/models/upload.rb', line 88

def retrieve_uploader(mounted_as = nil)
  build_uploader(mounted_as).tap do |uploader|
    uploader.retrieve_from_store!(identifier)
  end
end

#uploader_contextObject


114
115
116
117
118
119
# File 'app/models/upload.rb', line 114

def uploader_context
  {
    identifier: identifier,
    secret: secret
  }.compact
end