diff options
author | Matthew Lemon <y@yulqen.org> | 2024-11-13 21:45:25 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-11-13 21:45:25 +0000 |
commit | 05a673ebafaea9b2f672b9a23884dd51927f71e5 (patch) | |
tree | 5e75115bc57644a3717081b6b310335305542221 | |
parent | 9f6bca8186fb29eb26e9e40670013f6948e1efc3 (diff) |
wip: reducing quality of snapshots
-rw-r--r-- | app/controllers/pdfresources_controller.rb | 56 |
1 files changed, 35 insertions, 21 deletions
diff --git a/app/controllers/pdfresources_controller.rb b/app/controllers/pdfresources_controller.rb index 3c5fe6d..dfcb702 100644 --- a/app/controllers/pdfresources_controller.rb +++ b/app/controllers/pdfresources_controller.rb @@ -35,7 +35,7 @@ class PdfresourcesController < ApplicationController end end end - + def process_pdfs(pdfs) pdfs.each do |pdf| pdf_path = ActiveStorage::Blob.service.send(:path_for, pdf.key) @@ -53,19 +53,32 @@ class PdfresourcesController < ApplicationController 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_blob = ActiveStorage::Blob.create_and_upload!( - io: File.open(image_path), - filename: "page-#{index + 1}.jpg", - content_type: 'image/jpg' - ) - resource.pdf_snapshots.attach(image_blob) + image_reduced = MiniMagick::Tool::Magick.new do |magick| + magick << image_path + magick << "-quality" + magick << "20" + magick << reduced_image_path + end + + if File.exist?(reduced_image_path) + image_blob = ActiveStorage::Blob.create_and_upload!( + io: File.open(reduced_image_path), + filename: "page-#{index + 1}.jpg", + content_type: 'image/jpg' + ) + resource.pdf_snapshots.attach(image_blob) - File.delete(image_path) # Clean up the temporary file + 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 @@ -93,20 +106,21 @@ class PdfresourcesController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_pdfresource - @pdfresource = Pdfresource.find(params.expect(:id)) - end - # Only allow a list of trusted parameters through. - def pdfresource_params - params.expect(pdfresource: [ :name, :stripe_product_id, :price, :age_range, :curriculum, :feature_slot, :description, :card_description, pdfs: [], thumbnails: [] ]) - end + # Use callbacks to share common setup or constraints between actions. + def set_pdfresource + @pdfresource = Pdfresource.find(params.expect(:id)) + end - #must be admin! - def require_admin - unless Current.session.user&.is_admin - redirect_to root_path, notice: "You must be an admin to perform this action." - end + # Only allow a list of trusted parameters through. + def pdfresource_params + params.expect(pdfresource: [:name, :stripe_product_id, :price, :age_range, :curriculum, :feature_slot, :description, :card_description, pdfs: [], thumbnails: []]) + end + + # must be admin! + def require_admin + unless Current.session.user&.is_admin + redirect_to root_path, notice: "You must be an admin to perform this action." end + end end |