db(task): create variants for pjs in one week

This commit is contained in:
Eric Leroy-Terquem 2024-07-10 14:54:17 +02:00
parent 72f840e393
commit c9d74c46d1
No known key found for this signature in database
GPG key ID: 53D8FAECEF207605
2 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,39 @@
# frozen_string_literal: true
module Maintenance
class CreateVariantsForPjOfLatestDossiersTask < MaintenanceTasks::Task
attribute :start_text, :string
validates :start_text, presence: true
attribute :end_text, :string
validates :end_text, presence: true
def collection
start_date = DateTime.parse(start_text)
end_date = DateTime.parse(end_text)
Dossier
.state_en_construction_ou_instruction
.where(depose_at: start_date..end_date)
end
def process(dossier)
champ_ids = Champ
.where(dossier_id: dossier)
.where(type: ["Champs::PieceJustificativeChamp", 'Champs::TitreIdentiteChamp'])
.ids
attachments = ActiveStorage::Attachment
.where(record_id: champ_ids)
attachments.each do |attachment|
next if !(attachment.variable? && attachment.representation_required?)
attachment.variant(resize_to_limit: [400, 400]).processed if attachment.variant(resize_to_limit: [400, 400]).key.nil?
if attachment.blob.content_type.in?(RARE_IMAGE_TYPES) && attachment.variant(resize_to_limit: [2000, 2000]).key.nil?
attachment.variant(resize_to_limit: [2000, 2000]).processed
end
rescue MiniMagick::Error
end
end
end
end

View file

@ -0,0 +1,62 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe CreateVariantsForPjOfLatestDossiersTask do
describe "#process" do
let(:procedure) { create(:procedure_with_dossiers) }
let(:dossier) { procedure.dossiers.first }
let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) }
let(:champ_pj) { create(:champ_piece_justificative, type_de_champ: type_de_champ_pj, dossier:) }
let(:blob_info) do
{
filename: file.original_filename,
byte_size: file.size,
checksum: Digest::SHA256.file(file.path),
content_type: file.content_type,
# we don't want to run virus scanner on this file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
}
end
let(:blob) do
blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_info)
blob.upload(file)
blob
end
let(:attachment) { ActiveStorage::Attachment.create(name: "test", blob: blob, record: champ_pj) }
before do
dossier.update(
depose_at: Date.new(2024, 05, 23),
state: "en_construction"
)
end
subject(:process) { described_class.process(dossier) }
context "when pj is a classical format image" do
let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') }
it "creates a variant" do
expect(attachment.variant(resize_to_limit: [400, 400]).key).to be_nil
expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1)
expect(attachment.variant(resize_to_limit: [400, 400]).key).not_to be_nil
expect(attachment.variant(resize_to_limit: [2000, 2000]).key).to be_nil
end
end
context "when pj is a rare format image" do
let(:file) { fixture_file_upload('spec/fixtures/files/pencil.tiff', 'image/tiff') }
it "creates a variant" do
expect(attachment.variant(resize_to_limit: [400, 400]).key).to be_nil
expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(2)
expect(attachment.variant(resize_to_limit: [400, 400]).key).not_to be_nil
expect(attachment.variant(resize_to_limit: [2000, 2000]).key).not_to be_nil
end
end
end
end
end