feat(gallery): do not create representations in web machines

This commit is contained in:
Eric Leroy-Terquem 2024-06-11 16:17:09 +02:00
parent 5ac2315b5a
commit 0c95a098a5
No known key found for this signature in database
GPG key ID: ECE60B4C1FA2ABB3
2 changed files with 26 additions and 7 deletions

View file

@ -8,19 +8,22 @@ module GalleryHelper
end end
def preview_url_for(attachment) def preview_url_for(attachment)
attachment.preview(resize_to_limit: [400, 400]).processed.url preview = attachment.preview(resize_to_limit: [400, 400])
preview.image.attached? ? preview.processed.url : 'pdf-placeholder.png'
rescue StandardError rescue StandardError
'pdf-placeholder.png' 'pdf-placeholder.png'
end end
def variant_url_for(attachment) def variant_url_for(attachment)
attachment.variant(resize_to_limit: [400, 400]).processed.url variant = attachment.variant(resize_to_limit: [400, 400])
variant.key.present? ? variant.processed.url : 'apercu-indisponible.png'
rescue StandardError rescue StandardError
'apercu-indisponible.png' 'apercu-indisponible.png'
end end
def blob_url(attachment) def blob_url(attachment)
attachment.blob.content_type.in?(RARE_IMAGE_TYPES) ? attachment.variant(resize_to_limit: [2000, 2000]).processed.url : attachment.blob.url variant = attachment.variant(resize_to_limit: [2000, 2000])
attachment.blob.content_type.in?(RARE_IMAGE_TYPES) && variant.key.present? ? variant.processed.url : attachment.blob.url
rescue StandardError rescue StandardError
attachment.blob.url attachment.blob.url
end end

View file

@ -22,13 +22,21 @@ RSpec.describe GalleryHelper, type: :helper do
describe ".variant_url_for" do describe ".variant_url_for" do
subject { variant_url_for(attachment) } subject { variant_url_for(attachment) }
context "when attachment can be represented with a variant" do context "when image attachment has a variant" do
let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') } let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') }
it { expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1) } before { attachment.variant(resize_to_limit: [400, 400]).processed }
it { is_expected.not_to eq("apercu-indisponible.png") } it { is_expected.not_to eq("apercu-indisponible.png") }
end end
context "when image attachment has no variant" do
let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') }
it { expect { subject }.not_to change { ActiveStorage::VariantRecord.count } }
it { is_expected.to eq("apercu-indisponible.png") }
end
context "when attachment cannot be represented with a variant" do context "when attachment cannot be represented with a variant" do
let(:file) { fixture_file_upload('spec/fixtures/files/instructeurs-file.csv', 'text/csv') } let(:file) { fixture_file_upload('spec/fixtures/files/instructeurs-file.csv', 'text/csv') }
@ -40,13 +48,21 @@ RSpec.describe GalleryHelper, type: :helper do
describe ".preview_url_for" do describe ".preview_url_for" do
subject { preview_url_for(attachment) } subject { preview_url_for(attachment) }
context "when attachment can be represented with a preview" do context "when pdf attachment has a preview" do
let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') } let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') }
it { expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1) } before { attachment.preview(resize_to_limit: [400, 400]).processed }
it { is_expected.not_to eq("pdf-placeholder.png") } it { is_expected.not_to eq("pdf-placeholder.png") }
end end
context "when pdf attachment has no preview" do
let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') }
it { expect { subject }.not_to change { ActiveStorage::VariantRecord.count } }
it { is_expected.to eq("pdf-placeholder.png") }
end
context "when attachment cannot be represented with a preview" do context "when attachment cannot be represented with a preview" do
let(:file) { fixture_file_upload('spec/fixtures/files/instructeurs-file.csv', 'text/csv') } let(:file) { fixture_file_upload('spec/fixtures/files/instructeurs-file.csv', 'text/csv') }