Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/templates/process_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def normalize_attachment_fields(template, attachments = template.documents)
def generate_pdf_preview_from_file(attachment, file_path, page_number)
doc = Pdfium::Document.open_file(file_path)

blob = build_and_upload_blob(doc, page_number, '.jpeg')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-blocking] Do we need to worry about any existing .jpeg blobs in production?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call out, probably not since this still still in early labs, but I'll double check!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we don't since nothing will break, we'll have a small amount of duplicate .jpeg files sitting around in prod but since the number of people using this product is so low I don't mind letting those orphaned dupe images sit in S3, the number is probably insignificant and not worth the clean-up.

Great question, and thanks for the sanity check!

blob = build_and_upload_blob(doc, page_number, '.jpg')

ApplicationRecord.no_touching do
ActiveStorage::Attachment.create!(
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/templates/process_document_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

describe Templates::ProcessDocument do
describe '.generate_pdf_preview_from_file' do
let(:account) { create(:account) }
let(:user) { create(:user, account:) }
let(:template) { create(:template, account:, author: user, attachment_count: 0) }
let(:attachment) do
blob = ActiveStorage::Blob.create_and_upload!(
io: Rails.root.join('spec/fixtures/sample-document.pdf').open,
filename: 'sample-document.pdf',
content_type: 'application/pdf'
)
ActiveStorage::Attachment.create!(blob: blob, name: :documents, record: template)
end
let(:file_path) { ActiveStorage::Blob.service.path_for(attachment.key) }

it 'saves the preview blob under a filename the PreviewDocumentPageController cache lookup matches' do
described_class.generate_pdf_preview_from_file(attachment, file_path, 0)

cached = attachment.preview_images.joins(:blob)
.find_by(blob: { filename: ['0.png', '0.jpg'] })

expect(cached).not_to be_nil
end
end
end
Loading