BigW Consortium Gitlab

upload.rb 1.3 KB
Newer Older
1 2 3 4
class Upload < ActiveRecord::Base
  # Upper limit for foreground checksum processing
  CHECKSUM_THRESHOLD = 100.megabytes

5
  belongs_to :model, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

  validates :size, presence: true
  validates :path, presence: true
  validates :model, presence: true
  validates :uploader, presence: true

  before_save  :calculate_checksum, if:     :foreground_checksum?
  after_commit :schedule_checksum,  unless: :foreground_checksum?

  def self.remove_path(path)
    where(path: path).destroy_all
  end

  def self.record(uploader)
    remove_path(uploader.relative_path)

    create(
      size: uploader.file.size,
      path: uploader.relative_path,
      model: uploader.model,
      uploader: uploader.class.to_s
    )
  end

  def absolute_path
    return path unless relative_path?

    uploader_class.absolute_path(self)
  end

  def calculate_checksum
    return unless exist?

    self.checksum = Digest::SHA256.file(absolute_path).hexdigest
  end

  def exist?
    File.exist?(absolute_path)
  end

  private

  def foreground_checksum?
    size <= CHECKSUM_THRESHOLD
  end

  def schedule_checksum
    UploadChecksumWorker.perform_async(id)
  end

  def relative_path?
    !path.start_with?('/')
  end

  def uploader_class
    Object.const_get(uploader)
  end
end