From 9f6bca8186fb29eb26e9e40670013f6948e1efc3 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Wed, 13 Nov 2024 20:22:52 +0000 Subject: Rough rendition of pdf snapshotter works --- Gemfile | 2 + Gemfile.lock | 1 + app/controllers/pdfresources_controller.rb | 36 ++++++++++- app/models/pdfresource.rb | 9 +++ app/views/pdfresources/_pdfresource.html.erb | 94 +++++++++++++++------------- app/views/pdfresources/new.html.erb | 3 +- 6 files changed, 98 insertions(+), 47 deletions(-) diff --git a/Gemfile b/Gemfile index 8108483..628cf20 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,8 @@ gem "thruster", require: false # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] gem "image_processing", "~> 1.2" +gem "mini_magick" + group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem gem "debug", platforms: %i[ mri windows ], require: "debug/prelude" diff --git a/Gemfile.lock b/Gemfile.lock index c3948f6..0feec29 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -395,6 +395,7 @@ DEPENDENCIES importmap-rails jbuilder kamal + mini_magick propshaft puma (>= 5.0) rails (~> 8.0.0) diff --git a/app/controllers/pdfresources_controller.rb b/app/controllers/pdfresources_controller.rb index 1f8a6af..3c5fe6d 100644 --- a/app/controllers/pdfresources_controller.rb +++ b/app/controllers/pdfresources_controller.rb @@ -26,7 +26,8 @@ class PdfresourcesController < ApplicationController respond_to do |format| if @pdfresource.save - format.html { redirect_to @pdfresource, notice: "Pdfresource was successfully created." } + process_pdfs(@pdfresource.pdfs) + format.html { redirect_to @pdfresource, notice: "Resource was successfully created." } format.json { render :show, status: :created, location: @pdfresource } else format.html { render :new, status: :unprocessable_entity } @@ -34,6 +35,39 @@ class PdfresourcesController < ApplicationController end 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" + 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) + + File.delete(image_path) # Clean up the temporary file + end + end # PATCH/PUT /pdfresources/1 or /pdfresources/1.json def update diff --git a/app/models/pdfresource.rb b/app/models/pdfresource.rb index 6614f66..110a3c7 100644 --- a/app/models/pdfresource.rb +++ b/app/models/pdfresource.rb @@ -1,7 +1,16 @@ class Pdfresource < ApplicationRecord has_many_attached :pdfs has_many_attached :thumbnails + has_many_attached :pdf_snapshots validates :feature_slot, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 3, allow_nil: true }, allow_nil: true + validate :validate_pdf_count + validates :name, presence: true + + def validate_pdf_count + if pdfs.size > 10 + errors.add(:pdfs, "You can upload up to 10 PDFs per resource.") + end + end end diff --git a/app/views/pdfresources/_pdfresource.html.erb b/app/views/pdfresources/_pdfresource.html.erb index 7a2409e..3c8b3a8 100644 --- a/app/views/pdfresources/_pdfresource.html.erb +++ b/app/views/pdfresources/_pdfresource.html.erb @@ -3,58 +3,64 @@ <%= image_tag url_for(pdfresource.thumbnails.first), size: "500x100", class: "rounded-t-lg" %> -
-

- <%= pdfresource.name %> -

- -
- #photography - #travel - #winter -
- -

- <%= pdfresource.card_description %> -

- -

- Stripe product: - <%= pdfresource.stripe_product_id %> -

- -

- Pdfs: - <% pdfresource.pdfs.each do |pdf| %> +

+ <%= pdfresource.name %> +

+ +
+ #photography + #travel + #winter +
+ +

+ <%= pdfresource.card_description %> +

+ +

+ Stripe product: + <%= pdfresource.stripe_product_id %> +

+ +
+ PDFs: + <% pdfresource.pdfs.each do |pdf| %>
<%= link_to pdf.filename, pdf %>
<% end %> -

+
+
+ Snapshots: + <% pdfresource.pdf_snapshots.each do |image| %> +
<%= link_to image.filename, image %>
+ <% end %> +
-

- Price: - <%= pdfresource.price %> -

-

- Age range: - <%= pdfresource.age_range %> -

+

+ Price: + <%= pdfresource.price %> +

+ +

+ Age range: + <%= pdfresource.age_range %> +

-

- Curriculum: - <%= pdfresource.curriculum %> -

+

+ Curriculum: + <%= pdfresource.curriculum %> +

-

- Feature slot: - <%= pdfresource.feature_slot %> -

+

+ Feature slot: + <%= pdfresource.feature_slot %> +

-

- Description: - <%= pdfresource.description %> -

+

+ Description: + <%= pdfresource.description %> +

<%= link_to "Show this pdfresource", pdfresource, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> diff --git a/app/views/pdfresources/new.html.erb b/app/views/pdfresources/new.html.erb index 73f1a01..5fa06d1 100644 --- a/app/views/pdfresources/new.html.erb +++ b/app/views/pdfresources/new.html.erb @@ -1,5 +1,4 @@ -

-

Add a new Resource

+

Add a new Resource

<%= render "form", pdfresource: @pdfresource %> -- cgit v1.2.3