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
|
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
|
|
|
|
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
|
|
|
|
##
|
|
|
|
# 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
|
|
|
|
def finalize_fast_destroy(keys)
|
|
|
|
keys.each do |store_class, paths|
|
|
|
|
store_class.new.delete_keys_async(paths)
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
2019-10-08 20:06:06 -04:00
|
|
|
self.checksum = self.class.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
|
|
|
|
if Gitlab::Sentry.enabled?
|
2019-04-12 08:28:07 -04:00
|
|
|
Raven.capture_message(_("Upload file does not exist"), extra: self.attributes)
|
2018-10-30 06:56:47 -04:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Upload.prepend_if_ee('EE::Upload')
|