From 781ccc701d6ceefa30591b2547026678e067d0f2 Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sun, 16 Jun 2024 11:27:24 +0300 Subject: [PATCH] optimize large files upload --- lib/templates/process_document.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/templates/process_document.rb b/lib/templates/process_document.rb index 9fccb7d3f..06cd07382 100644 --- a/lib/templates/process_document.rb +++ b/lib/templates/process_document.rb @@ -11,6 +11,7 @@ module ProcessDocument MAX_WIDTH = 1400 MAX_NUMBER_OF_PAGES_PROCESSED = 15 MAX_FLATTEN_FILE_SIZE = 15.megabytes + GENERATE_PREVIEW_SIZE_LIMIT = 50.megabytes module_function @@ -55,7 +56,9 @@ def generate_pdf_preview_images(attachment, data) attachment.save! - (0..[number_of_pages - 1, MAX_NUMBER_OF_PAGES_PROCESSED].min).each do |page_number| + max_pages_to_process = data.size < GENERATE_PREVIEW_SIZE_LIMIT ? MAX_NUMBER_OF_PAGES_PROCESSED : 1 + + (0..[number_of_pages - 1, max_pages_to_process].min).each do |page_number| page = Vips::Image.new_from_buffer(data, '', dpi: DPI, page: page_number) page = page.resize(MAX_WIDTH / page.width.to_f)