refactor(watermark): use column instead of metadata on blob

This commit is contained in:
Paul Chavard 2022-12-22 17:57:08 +01:00
parent 456be420fa
commit d469bca0ae
7 changed files with 31 additions and 13 deletions

View file

@ -15,8 +15,8 @@ class TitreIdentiteWatermarkJob < ApplicationJob
WATERMARK = URI.parse(WATERMARK_FILE).is_a?(URI::HTTP) ? WATERMARK_FILE : Rails.root.join("app/assets/images/#{WATERMARK_FILE}") WATERMARK = URI.parse(WATERMARK_FILE).is_a?(URI::HTTP) ? WATERMARK_FILE : Rails.root.join("app/assets/images/#{WATERMARK_FILE}")
def perform(blob) def perform(blob)
if blob.virus_scanner.pending? then raise FileNotScannedYetError end return if blob.watermark_done?
if blob.watermark_done? then return end raise FileNotScannedYetError if blob.virus_scanner.pending?
blob.open do |file| blob.open do |file|
watermark = resize_watermark(file) watermark = resize_watermark(file)
@ -24,12 +24,8 @@ class TitreIdentiteWatermarkJob < ApplicationJob
if watermark.present? if watermark.present?
processed = watermark_image(file, watermark) processed = watermark_image(file, watermark)
blob.metadata[:watermark] = true
blob.upload(processed) blob.upload(processed)
blob.save blob.touch(:watermarked_at)
else
blob.metadata[:watermark_invalid] = true
blob.save
end end
end end
end end

View file

@ -4,11 +4,11 @@ module BlobTitreIdentiteWatermarkConcern
end end
def watermark_done? def watermark_done?
metadata[:watermark] watermarked_at.present?
end end
def watermark_later def watermark_later
if watermark_required? if watermark_pending?
TitreIdentiteWatermarkJob.perform_later(self) TitreIdentiteWatermarkJob.perform_later(self)
end end
end end
@ -16,6 +16,6 @@ module BlobTitreIdentiteWatermarkConcern
private private
def watermark_required? def watermark_required?
attachments.any? { |attachment| attachment.record.class.name == 'Champs::TitreIdentiteChamp' } attachments.any? { _1.record.class == Champs::TitreIdentiteChamp }
end end
end end

View file

@ -0,0 +1,5 @@
class AddWatermarkedAtActiveStorageBlobs < ActiveRecord::Migration[6.1]
def change
add_column :active_storage_blobs, :watermarked_at, :datetime
end
end

View file

@ -47,6 +47,7 @@ ActiveRecord::Schema.define(version: 2022_12_13_084442) do
t.integer "lock_version" t.integer "lock_version"
t.text "metadata" t.text "metadata"
t.string "service_name", null: false t.string "service_name", null: false
t.datetime "watermarked_at"
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end end

View file

@ -0,0 +1,16 @@
namespace :after_party do
desc 'Deployment task: backfill_watermarked_blobs'
task backfill_watermarked_blobs: :environment do
puts "Running deploy task 'backfill_watermarked_blobs'"
ActiveStorage::Blob.where("metadata like '%\"watermark\":true%'")
.where(watermarked_at: nil)
.in_batches
.update_all('watermarked_at = created_at')
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -88,7 +88,7 @@ RSpec.describe Attachment::EditComponent, type: :component do
context 'when watermarking is done' do context 'when watermarking is done' do
before do before do
attachment.metadata['watermark'] = true attachment.blob.touch(:watermarked_at)
end end
it 'renders a complete downlaod interface with details to download the file' do it 'renders a complete downlaod interface with details to download the file' do
@ -115,7 +115,7 @@ RSpec.describe Attachment::EditComponent, type: :component do
context 'when watermarking is done' do context 'when watermarking is done' do
before do before do
attachment.metadata['watermark'] = true attachment.blob.touch(:watermarked_at)
end end
it 'renders a simple link to view file' do it 'renders a simple link to view file' do

View file

@ -39,7 +39,7 @@ RSpec.describe Attachment::PendingPollComponent, type: :component do
context "when waterkmark is done" do context "when waterkmark is done" do
before do before do
attachment.blob[:metadata] = { watermark: true } attachment.blob.touch(:watermarked_at)
end end
it "does not render" do it "does not render" do