-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10449 from demarches-simplifiees/fix-gallery-with…
…-pdf-previews-and-variants-for-exotic-formats ETQ instructeur, dans la galerie, je peux prévisualiser des pdfs et voir des images au format rare
- Loading branch information
Showing
9 changed files
with
139 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module GalleryHelper | ||
def displayable_pdf?(blob) | ||
blob.previewable? && blob.content_type.in?(AUTHORIZED_PDF_TYPES) | ||
end | ||
|
||
def displayable_image?(blob) | ||
blob.variable? && blob.content_type.in?(AUTHORIZED_IMAGE_TYPES) | ||
end | ||
|
||
def preview_url_for(attachment) | ||
attachment.preview(resize_to_limit: [400, 400]).processed.url | ||
rescue ActiveStorage::Error | ||
'pdf-placeholder.png' | ||
end | ||
|
||
def variant_url_for(attachment) | ||
attachment.variant(resize_to_limit: [400, 400]).processed.url | ||
rescue ActiveStorage::Error | ||
'apercu-indisponible.png' | ||
end | ||
|
||
def blob_url(attachment) | ||
attachment.blob.content_type.in?(RARE_IMAGE_TYPES) ? attachment.variant(resize_to_limit: [2000, 2000]).processed.url : attachment.blob.url | ||
rescue ActiveStorage::Error | ||
attachment.blob.url | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
RSpec.describe GalleryHelper, type: :helper do | ||
let(:procedure) { create(:procedure_with_dossiers) } | ||
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) } | ||
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) } | ||
|
||
describe ".variant_url_for" do | ||
subject { variant_url_for(attachment) } | ||
|
||
context "when attachment can be represented with a variant" do | ||
let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') } | ||
|
||
it { expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1) } | ||
it { is_expected.not_to eq("apercu-indisponible.png") } | ||
end | ||
|
||
context "when attachment cannot be represented with a variant" do | ||
let(:file) { fixture_file_upload('spec/fixtures/files/instructeurs-file.csv', 'text/csv') } | ||
|
||
it { expect { subject }.not_to change { ActiveStorage::VariantRecord.count } } | ||
it { is_expected.to eq("apercu-indisponible.png") } | ||
end | ||
end | ||
|
||
describe ".preview_url_for" do | ||
subject { preview_url_for(attachment) } | ||
|
||
context "when attachment can be represented with a preview" do | ||
let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') } | ||
|
||
it { expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1) } | ||
it { is_expected.not_to eq("pdf-placeholder.png") } | ||
end | ||
|
||
context "when attachment cannot be represented with a preview" do | ||
let(:file) { fixture_file_upload('spec/fixtures/files/instructeurs-file.csv', 'text/csv') } | ||
|
||
it { expect { subject }.not_to change { ActiveStorage::VariantRecord.count } } | ||
it { is_expected.to eq("pdf-placeholder.png") } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters