aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-11-13 20:22:52 +0000
committerMatthew Lemon <y@yulqen.org>2024-11-13 20:22:52 +0000
commit9f6bca8186fb29eb26e9e40670013f6948e1efc3 (patch)
treebfae43dbc4fcb52ff6ce74f7cd32c0203adf042f
parent25afd4b7ef65ea7164b43d0599d901aedf252536 (diff)
Rough rendition of pdf snapshotter works
-rw-r--r--Gemfile2
-rw-r--r--Gemfile.lock1
-rw-r--r--app/controllers/pdfresources_controller.rb36
-rw-r--r--app/models/pdfresource.rb9
-rw-r--r--app/views/pdfresources/_pdfresource.html.erb94
-rw-r--r--app/views/pdfresources/new.html.erb3
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" %>
</div>
- <div class="ps-2">
- <p class="my-5">
- <strong class="block font-bold mb-2"><%= pdfresource.name %></strong>
- </p>
-
- <div class="px-6 pt-4 pb-2">
- <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#photography</span>
- <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#travel</span>
- <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#winter</span>
- </div>
-
- <p class="my-5 text-gray-500 text-base">
- <%= pdfresource.card_description %>
- </p>
-
- <p class="my-5">
- <strong class="block font-medium mb-1">Stripe product:</strong>
- <%= pdfresource.stripe_product_id %>
- </p>
-
- <p class="my-5">
- <strong class="block font-medium mb-1">Pdfs:</strong>
- <% pdfresource.pdfs.each do |pdf| %>
+ <p class="my-5">
+ <strong class="block font-bold mb-2"><%= pdfresource.name %></strong>
+ </p>
+
+ <div class="px-6 pt-4 pb-2">
+ <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#photography</span>
+ <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#travel</span>
+ <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#winter</span>
+ </div>
+
+ <p class="my-5 text-gray-500 text-base">
+ <%= pdfresource.card_description %>
+ </p>
+
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Stripe product:</strong>
+ <%= pdfresource.stripe_product_id %>
+ </p>
+
+ <div class="my-5">
+ <strong class="block font-medium mb-1">PDFs:</strong>
+ <% pdfresource.pdfs.each do |pdf| %>
<div><%= link_to pdf.filename, pdf %></div>
<% end %>
- </p>
+ </div>
+ <div class="my-5">
+ <strong class="block font-medium mb-1">Snapshots:</strong>
+ <% pdfresource.pdf_snapshots.each do |image| %>
+ <div><%= link_to image.filename, image %></div>
+ <% end %>
+ </div>
- <p class="my-5">
- <strong class="block font-medium mb-1">Price:</strong>
- <%= pdfresource.price %>
- </p>
- <p class="my-5">
- <strong class="block font-medium mb-1">Age range:</strong>
- <%= pdfresource.age_range %>
- </p>
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Price:</strong>
+ <%= pdfresource.price %>
+ </p>
+
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Age range:</strong>
+ <%= pdfresource.age_range %>
+ </p>
- <p class="my-5">
- <strong class="block font-medium mb-1">Curriculum:</strong>
- <%= pdfresource.curriculum %>
- </p>
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Curriculum:</strong>
+ <%= pdfresource.curriculum %>
+ </p>
- <p class="my-5">
- <strong class="block font-medium mb-1">Feature slot:</strong>
- <%= pdfresource.feature_slot %>
- </p>
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Feature slot:</strong>
+ <%= pdfresource.feature_slot %>
+ </p>
- <p class="my-5">
- <strong class="block font-medium mb-1">Description:</strong>
- <%= pdfresource.description %>
- </p></div>
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Description:</strong>
+ <%= pdfresource.description %>
+ </p>
<p>
<%= 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 @@
-<div class="mx-auto md:w-1/2 w-full">
- <h1 class="font-bold text-4xl">Add a new Resource</h1>
+<div class="mx-auto md:w-1/2 w-full"> <h1 class="font-bold text-4xl">Add a new Resource</h1>
<%= render "form", pdfresource: @pdfresource %>