Class: PersonalFileUploader

Inherits:
FileUploader show all
Defined in:
app/uploaders/personal_file_uploader.rb

Constant Summary

Constants inherited from FileUploader

FileUploader::DYNAMIC_PATH_PATTERN, FileUploader::InvalidSecret, FileUploader::MARKDOWN_PATTERN, FileUploader::VALID_SECRET_PATTERN

Constants included from Gitlab::FileTypeDetection

Gitlab::FileTypeDetection::DANGEROUS_AUDIO_EXT, Gitlab::FileTypeDetection::DANGEROUS_IMAGE_EXT, Gitlab::FileTypeDetection::DANGEROUS_VIDEO_EXT, Gitlab::FileTypeDetection::PDF_EXT, Gitlab::FileTypeDetection::SAFE_AUDIO_EXT, Gitlab::FileTypeDetection::SAFE_IMAGE_EXT, Gitlab::FileTypeDetection::SAFE_IMAGE_FOR_SCALING_EXT, Gitlab::FileTypeDetection::SAFE_VIDEO_EXT

Constants inherited from GitlabUploader

GitlabUploader::ObjectNotReadyError, GitlabUploader::PROTECTED_METHODS

Instance Attribute Summary

Attributes inherited from FileUploader

#model

Attributes included from RecordsUploads::Concern

#upload

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileUploader

absolute_base_dir, absolute_path, #absolute_path, #base_dir, #copy_file, copy_to, extract_dynamic_path, generate_secret, #initialize, #initialize_copy, #local_storage_path, relative_path, #remote_storage_path, #secret, #to_h, #upload=, #upload_path

Methods included from ObjectStorage::Concern

#cache!, #delete_migrated_file, #delete_tmp_file_after_storage, #exclusive_lease_key, #exists?, #file_cache_storage?, #file_storage?, #filename, #filename=, #fog_attributes, #fog_credentials, #fog_directory, #fog_public, #migrate!, #object_store=, #persist_object_store!, #persist_object_store?, #retrieve_from_store!, #store!, #store_path, #use_file, #use_open_file

Methods included from Gitlab::Utils::Override

#extended, extensions, #included, #method_added, #override, #prepended, #queue_verification, verify!

Methods included from RecordsUploads::Concern

#filename, #readd_upload, #record_upload, #upload_path

Methods included from Gitlab::FileMarkdownLinkBuilder

#markdown_link, #markdown_name

Methods included from Gitlab::FileTypeDetection

#audio?, #dangerous_audio?, #dangerous_embeddable?, #dangerous_image?, #dangerous_video?, #embeddable?, extension_match?, #image?, #image_safe_for_scaling?, #pdf?, #video?

Methods included from ObjectStorage::Extension::RecordsUploads

#build_upload, #exclusive_lease_key, #retrieve_from_store!, #upload=

Methods inherited from GitlabUploader

absolute_path, #cache_dir, #cached_size, #exists?, #file_cache_storage?, file_storage?, #filename, #initialize, #local_url, #move_to_cache, #move_to_store, #multi_read, #open, #options, options, #relative_path, #replace_file_without_saving!, storage_location, #url_or_file_path, version, #work_dir

Constructor Details

This class inherits a constructor from FileUploader

Class Method Details

.base_dir(model, _store = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'app/uploaders/personal_file_uploader.rb', line 13

def self.base_dir(model, _store = nil)
  # base_dir is the path seen by the user when rendering Markdown, so
  # it should be the same for both local and object storage. It is
  # typically prefaced with uploads/-/system, but that prefix
  # is omitted in the path stored on disk.
  File.join(options.base_dir, model_path_segment(model))
end

.model_path_segment(model) ⇒ Object



21
22
23
24
25
# File 'app/uploaders/personal_file_uploader.rb', line 21

def self.model_path_segment(model)
  return 'temp/' unless model

  File.join(model.class.underscore, model.id.to_s)
end

.rootObject

Re-Override



5
6
7
# File 'app/uploaders/personal_file_uploader.rb', line 5

def self.root
  options.storage_path
end

.workhorse_local_upload_pathObject



9
10
11
# File 'app/uploaders/personal_file_uploader.rb', line 9

def self.workhorse_local_upload_path
  File.join(options.storage_path, 'uploads', TMP_UPLOAD_PATH)
end

Instance Method Details

#model_valid?Boolean

model_path_segment does not require a model to be passed, so we can always generate a path, even when there’s no model.

Returns:

  • (Boolean)


35
36
37
# File 'app/uploaders/personal_file_uploader.rb', line 35

def model_valid?
  true
end

#object_storeObject



27
28
29
30
31
# File 'app/uploaders/personal_file_uploader.rb', line 27

def object_store
  return Store::LOCAL unless model

  super
end

#store_dirObject

Revert-Override



40
41
42
# File 'app/uploaders/personal_file_uploader.rb', line 40

def store_dir
  store_dirs[object_store]
end

#store_dirsObject



83
84
85
86
87
88
# File 'app/uploaders/personal_file_uploader.rb', line 83

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

#upload_paths(identifier) ⇒ Object

A personal snippet path is stored using FileUploader#upload_path.

The format for the path:

Local storage: :random_hex/:filename. Object storage: personal_snippet/:id/:random_hex/:filename.

upload_paths represent the possible paths for a given identifier, which will vary depending on whether the file is stored in local or object storage. upload_path should match an element in upload_paths.

base_dir represents the path seen by the user in Markdown, and it should always be prefixed with uploads/-/system.

store_dirs represent the paths that are actually used on disk. For object storage, this should omit the prefix /uploads/-/system.

For example, consider the requested path /uploads/-/system/personal_snippet/172/ff4ad5c2e40b39ae57cda51577317d20/file.png.

For local storage:

File on disk: /opt/gitlab/embedded/service/gitlab-rails/public/uploads/-/system/personal_snippet/172/ff4ad5c2e40b39ae57cda51577317d20/file.png.

base_dir: uploads/-/system/personal_snippet/172 upload_path: ff4ad5c2e40b39ae57cda51577317d20/file.png upload_paths: [“ff4ad5c2e40b39ae57cda51577317d20/file.png”, “personal_snippet/172/ff4ad5c2e40b39ae57cda51577317d20/file.png”]. store_dirs:

> 2=>“personal_snippet/172/ff4ad5c2e40b39ae57cda51577317d20”

For object storage:

upload_path: personal_snippet/172/ff4ad5c2e40b39ae57cda51577317d20/file.png



76
77
78
79
80
81
# File 'app/uploaders/personal_file_uploader.rb', line 76

def upload_paths(identifier)
  [
    local_storage_path(identifier),
    File.join(remote_storage_base_path, identifier)
  ]
end