Module: ObjectStorage::Concern

Instance Method Summary collapse

Instance Method Details

#cache!(new_file = sanitized_file) ⇒ Object


328
329
330
331
332
333
334
335
336
# File 'app/uploaders/object_storage.rb', line 328

def cache!(new_file = sanitized_file)
  # We intercept ::UploadedFile which might be stored on remote storage
  # We use that for "accelerated" uploads, where we store result on remote storage
  if new_file.is_a?(::UploadedFile) && new_file.remote_id.present?
    return cache_remote_file!(new_file.remote_id, new_file.original_filename)
  end

  super
end

#delete_migrated_file(migrated_file) ⇒ Object


302
303
304
# File 'app/uploaders/object_storage.rb', line 302

def delete_migrated_file(migrated_file)
  migrated_file.delete
end

#exclusive_lease_keyObject


347
348
349
# File 'app/uploaders/object_storage.rb', line 347

def exclusive_lease_key
  "object_storage_migrate:#{model.class}:#{model.id}"
end

#exists?Boolean

Returns:

  • (Boolean)

306
307
308
# File 'app/uploaders/object_storage.rb', line 306

def exists?
  file.present?
end

#file_cache_storage?Boolean

Returns:

  • (Boolean)

226
227
228
# File 'app/uploaders/object_storage.rb', line 226

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

#file_storage?Boolean

Returns:

  • (Boolean)

222
223
224
# File 'app/uploaders/object_storage.rb', line 222

def file_storage?
  storage.is_a?(CarrierWave::Storage::File)
end

#filenameObject

allow to configure and overwrite the filename


214
215
216
# File 'app/uploaders/object_storage.rb', line 214

def filename
  @filename || super || file&.filename # rubocop:disable Gitlab/ModuleWithInstanceVariables
end

#filename=(filename) ⇒ Object


218
219
220
# File 'app/uploaders/object_storage.rb', line 218

def filename=(filename)
  @filename = filename # rubocop:disable Gitlab/ModuleWithInstanceVariables
end

#fog_attributesObject


290
291
292
# File 'app/uploaders/object_storage.rb', line 290

def fog_attributes
  @fog_attributes ||= self.class.object_store_config.fog_attributes
end

#fog_credentialsObject


286
287
288
# File 'app/uploaders/object_storage.rb', line 286

def fog_credentials
  self.class.object_store_credentials
end

#fog_directoryObject


282
283
284
# File 'app/uploaders/object_storage.rb', line 282

def fog_directory
  self.class.remote_store_path
end

#fog_publicObject

Set ACL of uploaded objects to not-public (fog-aws) or no ACL at all (fog-google). Value is ignored by other supported backends (fog-aliyun, fog-openstack, fog-rackspace) [1]: github.com/fog/fog-aws/blob/daa50bb3717a462baf4d04d0e0cbfc18baacb541/lib/fog/aws/models/storage/file.rb#L152-L159


298
299
300
# File 'app/uploaders/object_storage.rb', line 298

def fog_public
  nil
end

#migrate!(new_store) ⇒ Object

Move the file to another store

new_store: Enum (Store::LOCAL, Store::REMOTE)

267
268
269
270
271
# File 'app/uploaders/object_storage.rb', line 267

def migrate!(new_store)
  with_exclusive_lease do
    unsafe_migrate!(new_store)
  end
end

#object_storeObject


230
231
232
233
# File 'app/uploaders/object_storage.rb', line 230

def object_store
  # We use Store::LOCAL as null value indicates the local storage
  @object_store ||= model.try(store_serialization_column) || Store::LOCAL
end

#object_store=(value) ⇒ Object

rubocop:disable Gitlab/ModuleWithInstanceVariables


236
237
238
239
# File 'app/uploaders/object_storage.rb', line 236

def object_store=(value)
  @object_store = value || Store::LOCAL
  @storage = storage_for(object_store)
end

#persist_object_store!Object

Save the current @object_store to the model <mounted_as>_store column


249
250
251
252
253
254
# File 'app/uploaders/object_storage.rb', line 249

def persist_object_store!
  return unless persist_object_store?

  updated = model.update_column(store_serialization_column, object_store)
  raise 'Failed to update object store' unless updated
end

#persist_object_store?Boolean

Return true if the current file is part or the model (i.e. is mounted in the model)

Returns:

  • (Boolean)

244
245
246
# File 'app/uploaders/object_storage.rb', line 244

def persist_object_store?
  model.respond_to?(:"#{store_serialization_column}=")
end

#schedule_background_upload(*args) ⇒ Object


273
274
275
276
277
278
279
280
# File 'app/uploaders/object_storage.rb', line 273

def schedule_background_upload(*args)
  return unless schedule_background_upload?

  ObjectStorage::BackgroundMoveWorker.perform_async(self.class.name,
                                                      model.class.name,
                                                      mounted_as,
                                                      model.id)
end

#store!(new_file = nil) ⇒ Object


338
339
340
341
342
343
344
345
# File 'app/uploaders/object_storage.rb', line 338

def store!(new_file = nil)
  # when direct upload is enabled, always store on remote storage
  if self.class.object_store_enabled? && self.class.direct_upload_enabled?
    self.object_store = Store::REMOTE
  end

  super
end

#store_dir(store = nil) ⇒ Object


310
311
312
# File 'app/uploaders/object_storage.rb', line 310

def store_dir(store = nil)
  store_dirs[store || object_store]
end

#store_dirsObject


314
315
316
317
318
319
# File 'app/uploaders/object_storage.rb', line 314

def store_dirs
  {
    Store::LOCAL => File.join(base_dir, dynamic_segment),
    Store::REMOTE => File.join(dynamic_segment)
  }
end

#upload_paths(identifier) ⇒ Object

Returns all the possible paths for an upload. the `upload.path` is a lookup parameter, and it may change depending on the `store` param.


324
325
326
# File 'app/uploaders/object_storage.rb', line 324

def upload_paths(identifier)
  store_dirs.map { |store, path| File.join(path, identifier) }
end

#use_file(&blk) ⇒ Object


256
257
258
259
260
# File 'app/uploaders/object_storage.rb', line 256

def use_file(&blk)
  with_exclusive_lease do
    unsafe_use_file(&blk)
  end
end