2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Upload < ApplicationRecord
|
2019-10-08 20:06:06 -04:00
|
|
|
include Checksummable
|
2021-11-01 17:10:32 -04:00
|
|
|
|
2017-02-22 14:25:06 -05:00
|
|
|
# Upper limit for foreground checksum processing
|
|
|
|
CHECKSUM_THRESHOLD = 100.megabytes
|
|
|
|
|
2017-06-02 08:29:30 -04:00
|
|
|
belongs_to :model, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2017-02-22 14:25:06 -05:00
|
|
|
|
|
|
|
validates :size, presence: true
|
|
|
|
validates :path, presence: true
|
|
|
|
validates :model, presence: true
|
|
|
|
validates :uploader, presence: true
|
|
|
|
|
2018-10-05 09:59:58 -04:00
|
|
|
scope :with_files_stored_locally, -> { where(store: ObjectStorage::Store::LOCAL) }
|
2018-10-30 06:56:47 -04:00
|
|
|
scope :with_files_stored_remotely, -> { where(store: ObjectStorage::Store::REMOTE) }
|
2018-02-21 11:43:21 -05:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
before_save :calculate_checksum!, if: :foreground_checksummable?
|
2019-10-02 20:05:59 -04:00
|
|
|
after_commit :schedule_checksum, if: :needs_checksum?
|
2017-02-22 14:25:06 -05:00
|
|
|
|
2021-10-04 14:12:46 -04:00
|
|
|
after_commit :update_project_statistics, on: [:create, :destroy], if: :project?
|
|
|
|
|
2018-01-30 09:21:28 -05:00
|
|
|
# as the FileUploader is not mounted, the default CarrierWave ActiveRecord
|
|
|
|
# hooks are not executed and the file will not be deleted
|
2018-01-31 10:59:35 -05:00
|
|
|
after_destroy :delete_file!, if: -> { uploader_class <= FileUploader }
|
2018-01-30 09:21:28 -05:00
|
|
|
|
2018-10-07 14:31:08 -04:00
|
|
|
class << self
|
2019-12-05 22:08:02 -05:00
|
|
|
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
|
|
|
|
|
2018-10-07 14:31:08 -04:00
|
|
|
##
|
|
|
|
# FastDestroyAll concerns
|
|
|
|
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
|
|
|
|
|
|
|
|
##
|
|
|
|
# FastDestroyAll concerns
|
2021-11-01 17:10:32 -04:00
|
|
|
def finalize_fast_destroy(items_to_remove)
|
|
|
|
items_to_remove.each do |store_class, keys|
|
|
|
|
store_class.new.delete_keys_async(keys)
|
2018-10-07 14:31:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-22 14:25:06 -05:00
|
|
|
def absolute_path
|
2019-04-12 08:28:07 -04:00
|
|
|
raise ObjectStorage::RemoteStoreError, _("Remote object has no absolute path.") unless local?
|
2017-02-22 14:25:06 -05:00
|
|
|
return path unless relative_path?
|
|
|
|
|
|
|
|
uploader_class.absolute_path(self)
|
|
|
|
end
|
|
|
|
|
2021-11-01 17:10:32 -04:00
|
|
|
def relative_path
|
|
|
|
uploader_class.relative_path(self)
|
|
|
|
end
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
def calculate_checksum!
|
|
|
|
self.checksum = nil
|
2019-10-02 20:05:59 -04:00
|
|
|
return unless needs_checksum?
|
2017-02-22 14:25:06 -05:00
|
|
|
|
2021-09-27 11:10:13 -04:00
|
|
|
self.checksum = self.class.sha256_hexdigest(absolute_path)
|
2017-02-22 14:25:06 -05:00
|
|
|
end
|
|
|
|
|
2019-10-02 20:05:59 -04:00
|
|
|
# Initialize the associated Uploader class with current model
|
|
|
|
#
|
|
|
|
# @param [String] mounted_as
|
|
|
|
# @return [GitlabUploader] one of the subclasses, defined at the model's uploader attribute
|
2018-02-21 11:43:21 -05:00
|
|
|
def build_uploader(mounted_as = nil)
|
|
|
|
uploader_class.new(model, mounted_as || mount_point).tap do |uploader|
|
2018-02-02 08:59:43 -05:00
|
|
|
uploader.upload = self
|
2019-10-02 20:05:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Initialize the associated Uploader class with current model and
|
|
|
|
# retrieve existing file from the store to a local cache
|
|
|
|
#
|
|
|
|
# @param [String] mounted_as
|
|
|
|
# @return [GitlabUploader] one of the subclasses, defined at the model's uploader attribute
|
|
|
|
def retrieve_uploader(mounted_as = nil)
|
|
|
|
build_uploader(mounted_as).tap do |uploader|
|
2018-02-02 08:59:43 -05:00
|
|
|
uploader.retrieve_from_store!(identifier)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-02 20:05:59 -04:00
|
|
|
# This checks for existence of the upload on storage
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether upload exists on storage
|
2017-02-22 14:25:06 -05:00
|
|
|
def exist?
|
2019-10-02 20:05:59 -04:00
|
|
|
exist = if local?
|
|
|
|
File.exist?(absolute_path)
|
|
|
|
else
|
|
|
|
retrieve_uploader.exists?
|
|
|
|
end
|
2018-10-30 06:56:47 -04:00
|
|
|
|
|
|
|
# Help sysadmins find missing upload files
|
|
|
|
if persisted? && !exist
|
2019-12-13 07:07:41 -05:00
|
|
|
exception = RuntimeError.new("Uploaded file does not exist")
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(exception, self.attributes)
|
2019-04-12 08:28:07 -04:00
|
|
|
Gitlab::Metrics.counter(:upload_file_does_not_exist_total, _('The number of times an upload record could not find its file')).increment
|
2018-10-30 06:56:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
exist
|
2017-02-22 14:25:06 -05:00
|
|
|
end
|
|
|
|
|
2018-01-29 16:06:17 -05:00
|
|
|
def uploader_context
|
|
|
|
{
|
|
|
|
identifier: identifier,
|
|
|
|
secret: secret
|
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
|
2018-02-21 11:43:21 -05:00
|
|
|
def local?
|
|
|
|
store == ObjectStorage::Store::LOCAL
|
|
|
|
end
|
|
|
|
|
2019-10-02 20:05:59 -04:00
|
|
|
# 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
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether generating a checksum is needed
|
|
|
|
def needs_checksum?
|
|
|
|
checksum.nil? && local? && exist?
|
|
|
|
end
|
|
|
|
|
2017-02-22 14:25:06 -05:00
|
|
|
private
|
|
|
|
|
2018-01-30 09:21:28 -05:00
|
|
|
def delete_file!
|
2019-10-02 20:05:59 -04:00
|
|
|
retrieve_uploader.remove!
|
2018-02-02 08:59:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def foreground_checksummable?
|
2019-10-02 20:05:59 -04:00
|
|
|
needs_checksum? && size <= CHECKSUM_THRESHOLD
|
2017-02-22 14:25:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def schedule_checksum
|
|
|
|
UploadChecksumWorker.perform_async(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def relative_path?
|
|
|
|
!path.start_with?('/')
|
|
|
|
end
|
|
|
|
|
2018-01-29 16:06:17 -05:00
|
|
|
def uploader_class
|
2019-10-15 23:06:12 -04:00
|
|
|
Object.const_get(uploader, false)
|
2018-01-29 16:06:17 -05:00
|
|
|
end
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
def identifier
|
|
|
|
File.basename(path)
|
|
|
|
end
|
|
|
|
|
2018-01-29 16:06:17 -05:00
|
|
|
def mount_point
|
|
|
|
super&.to_sym
|
2017-02-22 14:25:06 -05:00
|
|
|
end
|
2021-10-04 14:12:46 -04:00
|
|
|
|
|
|
|
def project?
|
|
|
|
model_type == "Project"
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_project_statistics
|
|
|
|
ProjectCacheWorker.perform_async(model_id, [], [:uploads_size])
|
|
|
|
end
|
2017-02-22 14:25:06 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Upload.prepend_mod_with('Upload')
|