2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-22 14:25:06 -05:00
|
|
|
class Upload < ActiveRecord::Base
|
|
|
|
# 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?
|
|
|
|
after_commit :schedule_checksum, if: :checksummable?
|
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-02-02 08:59:43 -05:00
|
|
|
def self.hexdigest(path)
|
|
|
|
Digest::SHA256.file(path).hexdigest
|
2017-02-22 14:25:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def absolute_path
|
2018-02-02 08:59:43 -05: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
|
|
|
|
return unless checksummable?
|
2017-02-22 14:25:06 -05:00
|
|
|
|
|
|
|
self.checksum = Digest::SHA256.file(absolute_path).hexdigest
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
uploader.retrieve_from_store!(identifier)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-22 14:25:06 -05:00
|
|
|
def exist?
|
2018-10-30 06:56:47 -04:00
|
|
|
exist = File.exist?(absolute_path)
|
|
|
|
|
|
|
|
# Help sysadmins find missing upload files
|
|
|
|
if persisted? && !exist
|
|
|
|
if Gitlab::Sentry.enabled?
|
|
|
|
Raven.capture_message("Upload file does not exist", extra: self.attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Metrics.counter(:upload_file_does_not_exist_total, 'The number of times an upload record could not find its file').increment
|
|
|
|
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
|
|
|
|
|
2017-02-22 14:25:06 -05:00
|
|
|
private
|
|
|
|
|
2018-01-30 09:21:28 -05:00
|
|
|
def delete_file!
|
|
|
|
build_uploader.remove!
|
|
|
|
end
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
def checksummable?
|
|
|
|
checksum.nil? && local? && exist?
|
|
|
|
end
|
|
|
|
|
|
|
|
def foreground_checksummable?
|
|
|
|
checksummable? && 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
|
|
|
|
Object.const_get(uploader)
|
|
|
|
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
|