demarches-normaliennes/app/jobs/titre_identite_watermark_job.rb
Colin Darie bfb98554ff
fix(active-storage): don't fail on analyzing a blob derivated by watermark
Quand l'analyse du job se produit après le watermark, elle doit comparer
le checksum avec celui du fichier filigrané.
2024-02-12 16:30:58 +01:00

28 lines
1,021 B
Ruby

class TitreIdentiteWatermarkJob < ApplicationJob
class FileNotScannedYetError < StandardError
end
# If by the time the job runs the blob has been deleted, ignore the error
discard_on ActiveRecord::RecordNotFound
# If the file is deleted during the scan, ignore the error
discard_on ActiveStorage::FileNotFoundError
# If the file is not analyzed or scanned for viruses yet, retry later
# (to avoid modifying the file while it is being scanned).
retry_on FileNotScannedYetError, wait: :exponentially_longer, attempts: 10
def perform(blob)
return if blob.watermark_done?
raise FileNotScannedYetError if blob.virus_scanner.pending?
blob.open do |file|
Tempfile.create(["watermarked", File.extname(file)]) do |output|
processed = WatermarkService.new.process(file, output)
return if processed.blank?
blob.upload(processed) # also update checksum & byte_size accordingly
blob.watermarked_at = Time.current
blob.save!
end
end
end
end