diff options
author | Matthew Lemon <y@yulqen.org> | 2024-11-14 09:50:29 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-11-14 09:50:29 +0000 |
commit | ed4a5a07b70f1d3e23dd74bc217477694200e9cb (patch) | |
tree | 90784f7f01c476ba5a8ccfdf648ddf8a9634dbf9 /app/controllers | |
parent | e2c2aaedece3a971e45072b11ca5e514b12715f6 (diff) |
Big refactoring of the PDF snapshotting - into service file
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/pdfresources_controller.rb | 52 |
1 files changed, 1 insertions, 51 deletions
diff --git a/app/controllers/pdfresources_controller.rb b/app/controllers/pdfresources_controller.rb index f1634a2..4cd2d74 100644 --- a/app/controllers/pdfresources_controller.rb +++ b/app/controllers/pdfresources_controller.rb @@ -26,7 +26,7 @@ class PdfresourcesController < ApplicationController respond_to do |format| if @pdfresource.save - process_pdfs(@pdfresource.pdfs) + PdfProcessorService.new(@pdfresource).process_pdfs format.html { redirect_to @pdfresource, notice: "Resource was successfully created." } format.json { render :show, status: :created, location: @pdfresource } else @@ -36,56 +36,6 @@ class PdfresourcesController < ApplicationController end end - def process_pdfs(pdfs) - pdfs.each do |pdf| - pdf_path = ActiveStorage::Blob.service.send(:path_for, pdf.key) - convert_pdf_to_images(pdf_path, @pdfresource) - end - end - - def convert_pdf_to_images(pdf_path, resource) - page_count_output = MiniMagick::Tool::Identify.new do |identify| - identify.format '%n' - identify << pdf_path - end - - page_count = page_count_output.size - - page_count.times do |index| - image_path = "page-#{index + 1}.jpg" - reduced_image_path = "reduced-page-#{index + 1}.jpg" - MiniMagick::Tool::Magick.new do |magick| - magick << "#{pdf_path}[#{index}]" # Process each PDF page individually - magick << image_path - end - - image_reduced = MiniMagick::Tool::Magick.new do |magick| - magick << image_path - magick << "-quality" - magick << "40" - magick.strip - magick << "-resize" - magick << "50%" - magick << reduced_image_path - end - - if File.exist?(reduced_image_path) - fn = resource.name.strip.downcase.gsub("\s", "_") << "_page-" - image_blob = ActiveStorage::Blob.create_and_upload!( - io: File.open(reduced_image_path), - filename: "#{fn}#{index + 1}.jpg", - content_type: 'image/jpg' - ) - resource.pdf_snapshots.attach(image_blob) - - File.delete(image_path) if File.exist?(image_path) - File.delete(reduced_image_path) if File.exist?(reduced_image_path) - else - raise "Reduced image file wasn't created successfully: #{image_reduced}" - end - end - end - # PATCH/PUT /pdfresources/1 or /pdfresources/1.json def update respond_to do |format| |