aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-11-14 12:01:49 +0000
committerMatthew Lemon <y@yulqen.org>2024-11-14 12:01:49 +0000
commit1b64b0b709c5704de48120e20bdfad32f34b0b5d (patch)
tree8cbb68c52a79d9cf12dfd861fee5fc3d0ac74b99
parent06222c54c248a03d0b019db12bc365ab187e317a (diff)
Adds credits and cleaner handling of page count using ruby
-rw-r--r--Gemfile2
-rw-r--r--Gemfile.lock13
-rw-r--r--app/controllers/pdfresources_controller.rb14
-rw-r--r--app/services/pdf_processor_service.rb20
-rw-r--r--app/views/pdfresources/_form.html.erb8
-rw-r--r--app/views/pdfresources/show.html.erb3
6 files changed, 43 insertions, 17 deletions
diff --git a/Gemfile b/Gemfile
index 628cf20..daac6b2 100644
--- a/Gemfile
+++ b/Gemfile
@@ -65,3 +65,5 @@ group :test do
gem "capybara"
gem "selenium-webdriver"
end
+
+gem "pdf-reader", "~> 2.13"
diff --git a/Gemfile.lock b/Gemfile.lock
index 0feec29..608f2d5 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
+ Ascii85 (2.0.1)
actioncable (8.0.0)
actionpack (= 8.0.0)
activesupport (= 8.0.0)
@@ -74,6 +75,7 @@ GEM
uri (>= 0.13.1)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
+ afm (0.2.2)
ast (2.4.2)
base64 (0.2.0)
bcrypt (3.1.20)
@@ -125,6 +127,7 @@ GEM
raabro (~> 1.4)
globalid (1.2.1)
activesupport (>= 6.1)
+ hashery (2.1.2)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
image_processing (1.13.0)
@@ -201,6 +204,12 @@ GEM
parser (3.3.6.0)
ast (~> 2.4.1)
racc
+ pdf-reader (2.13.0)
+ Ascii85 (>= 1.0, < 3.0, != 2.0.0)
+ afm (~> 0.2.1)
+ hashery (~> 2.0)
+ ruby-rc4
+ ttfunk
propshaft (1.1.0)
actionpack (>= 7.0.0)
activesupport (>= 7.0.0)
@@ -286,6 +295,7 @@ GEM
rubocop-performance
rubocop-rails
ruby-progressbar (1.13.0)
+ ruby-rc4 (0.1.5)
ruby-vips (2.2.2)
ffi (~> 1.12)
logger
@@ -348,6 +358,8 @@ GEM
thruster (0.1.8-x86_64-darwin)
thruster (0.1.8-x86_64-linux)
timeout (0.4.2)
+ ttfunk (1.8.0)
+ bigdecimal (~> 3.1)
turbo-rails (2.0.11)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
@@ -396,6 +408,7 @@ DEPENDENCIES
jbuilder
kamal
mini_magick
+ pdf-reader (~> 2.13)
propshaft
puma (>= 5.0)
rails (~> 8.0.0)
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 @@
<div class="my-5">
<%= required_label_tag(form, :thumbnails) %>
- <% if pdfresource.thumbnails.any? %>
+ <% if pdfresource.thumbnails.any? && pdfresource.persisted? %>
<div class="mb-4 p-4 bg-gray-50 rounded-lg">
<h3 class="font-medium text-gray-700">Current Thumbnails:</h3>
<div class="grid grid-cols-2 md:grid-cols-3 gap-4 mt-2">
<% pdfresource.thumbnails.each do |thumbnail| %>
<div class="relative">
- <%= image_tag thumbnail, class: "rounded-lg shadow-sm" %>
- <p class="text-xs text-gray-500 mt-1"><%= thumbnail.filename %></p>
+ <% if thumbnail.persisted? %>
+ <%= image_tag url_for(thumbnail), class: "rounded-lg shadow-sm" %>
+ <p class="text-xs text-gray-500 mt-1"><%= thumbnail.filename %></p>
+ <% end %>
</div>
<% end %>
</div>
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 @@
<div class="text-2xl font-bold mb-6">
£<%= number_with_precision(@pdfresource.price || 0.00, precision: 2) %>
+ <% if @pdfresource.credits %>
+ (or <%= @pdfresource.credits %> credit<%= 's' if @pdfresource.credits != 1 %>)
+ <% end %>
</div>
<div class="mb-8">