From 936fdfce2c3306479b515e038e5352b1c941dad2 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Fri, 13 Dec 2024 13:00:40 +0000 Subject: Adds guard before deleting temp files --- app/services/pdf_processor_service.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/services/pdf_processor_service.rb b/app/services/pdf_processor_service.rb index dd3718a..70133b4 100644 --- a/app/services/pdf_processor_service.rb +++ b/app/services/pdf_processor_service.rb @@ -56,10 +56,11 @@ class PdfProcessorService end def process_page(pdf_path, page_index) - temp_files = create_page_images(pdf_path, page_index) - attach_processed_image(temp_files[:reduced], page_index) + temp_files = nil + temp_files = create_page_images(pdf_path, page_index) # Generate temp files + attach_processed_image(temp_files[:reduced], page_index) # Attach image ensure - cleanup_temp_files(temp_files) + cleanup_temp_files(temp_files) # Cleanup, even if an error occurs end def create_page_images(pdf_path, index) @@ -119,8 +120,9 @@ class PdfProcessorService end def cleanup_temp_files(temp_files) + return if temp_files.nil? # Safeguard in case temp_files is nil temp_files.values.each do |path| - File.delete(path) if File.exist?(path) + File.delete(path) if File.exist?(path) # Safely delete files end end -end \ No newline at end of file +end \ No newline at end of file -- cgit v1.2.3