debian-mirror-gitlab/app/models/upload.rb

99 lines
2.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class Upload < ActiveRecord::Base
# Upper limit for foreground checksum processing
CHECKSUM_THRESHOLD = 100.megabytes
2017-09-10 17:25:29 +05:30
belongs_to :model, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
2017-08-17 22:00:37 +05:30
validates :size, presence: true
validates :path, presence: true
validates :model, presence: true
validates :uploader, presence: true
2018-05-09 12:01:36 +05:30
scope :with_files_stored_locally, -> { where(store: [nil, ObjectStorage::Store::LOCAL]) }
2018-03-17 18:26:18 +05:30
before_save :calculate_checksum!, if: :foreground_checksummable?
after_commit :schedule_checksum, if: :checksummable?
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
# as the FileUploader is not mounted, the default CarrierWave ActiveRecord
# hooks are not executed and the file will not be deleted
after_destroy :delete_file!, if: -> { uploader_class <= FileUploader }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def self.hexdigest(path)
Digest::SHA256.file(path).hexdigest
2017-08-17 22:00:37 +05:30
end
def absolute_path
2018-05-09 12:01:36 +05:30
raise ObjectStorage::RemoteStoreError, "Remote object has no absolute path." unless local?
2017-08-17 22:00:37 +05:30
return path unless relative_path?
uploader_class.absolute_path(self)
end
2018-03-17 18:26:18 +05:30
def calculate_checksum!
self.checksum = nil
return unless checksummable?
2018-05-09 12:01:36 +05:30
self.checksum = Digest::SHA256.file(absolute_path).hexdigest
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
2018-05-09 12:01:36 +05:30
def build_uploader(mounted_as = nil)
uploader_class.new(model, mounted_as || mount_point).tap do |uploader|
2018-03-17 18:26:18 +05:30
uploader.upload = self
uploader.retrieve_from_store!(identifier)
end
2017-08-17 22:00:37 +05:30
end
def exist?
File.exist?(absolute_path)
end
2018-03-17 18:26:18 +05:30
def uploader_context
{
identifier: identifier,
secret: secret
}.compact
end
2018-05-09 12:01:36 +05:30
def local?
return true if store.nil?
store == ObjectStorage::Store::LOCAL
end
2017-08-17 22:00:37 +05:30
private
2018-03-17 18:26:18 +05:30
def delete_file!
build_uploader.remove!
end
def checksummable?
checksum.nil? && local? && exist?
end
def foreground_checksummable?
checksummable? && size <= CHECKSUM_THRESHOLD
2017-08-17 22:00:37 +05:30
end
def schedule_checksum
UploadChecksumWorker.perform_async(id)
end
def relative_path?
!path.start_with?('/')
end
def uploader_class
Object.const_get(uploader)
end
2018-03-17 18:26:18 +05:30
def identifier
File.basename(path)
end
def mount_point
super&.to_sym
end
2017-08-17 22:00:37 +05:30
end