From 897f4a2291d3ac53665af1b20edb3c9113710231 Mon Sep 17 00:00:00 2001 From: Alex Turchyn Date: Fri, 9 Jun 2023 01:43:43 +0300 Subject: [PATCH] use progressive jpeg for previews --- app/javascript/template_builder/upload.vue | 2 +- config/initializers/active_storage.rb | 4 +++- lib/templates/process_document.rb | 9 ++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/javascript/template_builder/upload.vue b/app/javascript/template_builder/upload.vue index 5b68d3c8..72e886c6 100644 --- a/app/javascript/template_builder/upload.vue +++ b/app/javascript/template_builder/upload.vue @@ -16,7 +16,7 @@ ref="input" type="file" class="hidden" - accept=".pdf" + accept="image/*, application/pdf" multiple @change="upload" > diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb index ccbc18ae..bf7bf83d 100644 --- a/config/initializers/active_storage.rb +++ b/config/initializers/active_storage.rb @@ -12,6 +12,8 @@ Rails.configuration.to_prepare do end LoadActiveStorageConfigs.call -rescue StandardError +rescue StandardError => e + Rails.logger.debug(e) + nil end diff --git a/lib/templates/process_document.rb b/lib/templates/process_document.rb index 0d76c7d3..7d9764a8 100644 --- a/lib/templates/process_document.rb +++ b/lib/templates/process_document.rb @@ -30,7 +30,7 @@ module Templates image = Vips::Image.new_from_buffer(binary, '') image = image.resize(MAX_WIDTH / image.width.to_f) - io = StringIO.new(image.write_to_buffer(FORMAT, Q: Q)) + io = StringIO.new(image.write_to_buffer(FORMAT, Q: Q, interlace: true)) ActiveStorage::Attachment.create!( blob: ActiveStorage::Blob.create_and_upload!( @@ -46,12 +46,13 @@ module Templates binary = attachment.download ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all + number_of_pages = HexaPDF::Document.new(io: StringIO.new(binary)).pages.size - 1 - (0..).each do |page_number| + (0..number_of_pages).each do |page_number| page = Vips::Image.new_from_buffer(binary, '', dpi: DPI, page: page_number) page = page.resize(MAX_WIDTH / page.width.to_f) - io = StringIO.new(page.write_to_buffer(FORMAT, Q: Q)) + io = StringIO.new(page.write_to_buffer(FORMAT, Q: Q, interlace: true)) ActiveStorage::Attachment.create!( blob: ActiveStorage::Blob.create_and_upload!( @@ -61,8 +62,6 @@ module Templates name: ATTACHMENT_NAME, record: attachment ) - rescue Vips::Error - break end end end