2020-11-17 16:34:24 +01:00
|
|
|
class TitreIdentiteWatermarkJob < ApplicationJob
|
2021-03-11 14:42:57 +01:00
|
|
|
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
|
|
|
|
|
2020-11-17 16:34:24 +01:00
|
|
|
def perform(blob)
|
2022-12-22 17:57:08 +01:00
|
|
|
return if blob.watermark_done?
|
|
|
|
raise FileNotScannedYetError if blob.virus_scanner.pending?
|
2021-03-11 14:42:57 +01:00
|
|
|
|
2020-11-17 16:34:24 +01:00
|
|
|
blob.open do |file|
|
2023-09-07 18:10:26 +02:00
|
|
|
Tempfile.create(["watermarked", File.extname(file)]) do |output|
|
|
|
|
processed = WatermarkService.new.process(file, output)
|
|
|
|
return if processed.blank?
|
2021-01-27 15:58:49 +01:00
|
|
|
|
2024-02-12 16:30:08 +01:00
|
|
|
blob.upload(processed) # also update checksum & byte_size accordingly
|
|
|
|
blob.watermarked_at = Time.current
|
|
|
|
blob.save!
|
2021-01-27 15:58:49 +01:00
|
|
|
end
|
2020-11-17 16:34:24 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|