From 1b64b0b709c5704de48120e20bdfad32f34b0b5d Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Thu, 14 Nov 2024 12:01:49 +0000 Subject: Adds credits and cleaner handling of page count using ruby --- app/controllers/pdfresources_controller.rb | 14 +++++++++++++- app/services/pdf_processor_service.rb | 20 +++++++------------- app/views/pdfresources/_form.html.erb | 8 +++++--- app/views/pdfresources/show.html.erb | 3 +++ 4 files changed, 28 insertions(+), 17 deletions(-) (limited to 'app') diff --git a/app/controllers/pdfresources_controller.rb b/app/controllers/pdfresources_controller.rb index 4cd2d74..adacc64 100644 --- a/app/controllers/pdfresources_controller.rb +++ b/app/controllers/pdfresources_controller.rb @@ -68,7 +68,19 @@ class PdfresourcesController < ApplicationController # 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: []]) + params.require(:pdfresource).permit( + :name, + :stripe_product_id, + :price, + :age_range, + :curriculum, + :feature_slot, + :description, + :card_description, + :credits, + pdfs: [], + thumbnails: [] + ) end # must be admin! diff --git a/app/services/pdf_processor_service.rb b/app/services/pdf_processor_service.rb index 3f97611..dd3718a 100644 --- a/app/services/pdf_processor_service.rb +++ b/app/services/pdf_processor_service.rb @@ -1,3 +1,5 @@ +require 'pdf-reader' + class PdfProcessorService DEFAULT_OPTIONS = { quality: 40, @@ -37,24 +39,16 @@ class PdfProcessorService def get_page_count(pdf_path) Rails.logger.debug "Checking PDF: #{pdf_path}" - Rails.logger.debug "File exists: #{File.exist?(pdf_path)}" - Rails.logger.debug "File size: #{File.size(pdf_path)}" - Rails.logger.debug "File type: #{`file -b #{pdf_path}`}" - # Try multiple methods to get page count - identify_output = `identify -format %n "#{pdf_path}" 2>&1` - gs_output = `gs -q -dNODISPLAY -c "#{pdf_path} (r) file runpdfbegin pdfpagecount = quit" 2>&1` + reader = PDF::Reader.new(pdf_path) + count = reader.page_count - Rails.logger.debug "Identify output: #{identify_output}" - Rails.logger.debug "Ghostscript output: #{gs_output}" + Rails.logger.debug "PDF page count: #{count}" - count = identify_output.to_i - if count <= 0 - count = gs_output.to_i + if count <= 0 || count > 1000 # Reasonable maximum page limit + raise "Invalid page count: #{count}" end - Rails.logger.debug "Final page count: #{count}" - raise "Invalid page count: #{count}" unless count.positive? count rescue StandardError => e Rails.logger.error "Failed to get page count: #{e.message}" diff --git a/app/views/pdfresources/_form.html.erb b/app/views/pdfresources/_form.html.erb index 7b1ce60..1ad8fb1 100644 --- a/app/views/pdfresources/_form.html.erb +++ b/app/views/pdfresources/_form.html.erb @@ -53,14 +53,16 @@
<%= required_label_tag(form, :thumbnails) %> - <% if pdfresource.thumbnails.any? %> + <% if pdfresource.thumbnails.any? && pdfresource.persisted? %>

Current Thumbnails:

<% pdfresource.thumbnails.each do |thumbnail| %>
- <%= image_tag thumbnail, class: "rounded-lg shadow-sm" %> -

<%= thumbnail.filename %>

+ <% if thumbnail.persisted? %> + <%= image_tag url_for(thumbnail), class: "rounded-lg shadow-sm" %> +

<%= thumbnail.filename %>

+ <% end %>
<% end %>
diff --git a/app/views/pdfresources/show.html.erb b/app/views/pdfresources/show.html.erb index 525e5dd..dc6dcd7 100644 --- a/app/views/pdfresources/show.html.erb +++ b/app/views/pdfresources/show.html.erb @@ -22,6 +22,9 @@
£<%= number_with_precision(@pdfresource.price || 0.00, precision: 2) %> + <% if @pdfresource.credits %> + (or <%= @pdfresource.credits %> credit<%= 's' if @pdfresource.credits != 1 %>) + <% end %>
-- cgit v1.2.3