aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/pdfresources_controller.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/pdfresources_controller.rb')
-rw-r--r--app/controllers/pdfresources_controller.rb36
1 files changed, 35 insertions, 1 deletions
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