refactor process doc
This commit is contained in:
@@ -103,7 +103,15 @@ module Templates
|
|||||||
|
|
||||||
promises =
|
promises =
|
||||||
range.map do |page_number|
|
range.map do |page_number|
|
||||||
Concurrent::Promise.execute(executor: pool) { build_and_upload_blob(doc, page_number) }
|
doc_page = doc.get_page(page_number)
|
||||||
|
|
||||||
|
bytes, width, height = doc_page.render_to_bitmap(width: MAX_WIDTH)
|
||||||
|
|
||||||
|
image = Vips::Image.new_from_memory(bytes, width, height, 4, :uchar)
|
||||||
|
|
||||||
|
Concurrent::Promise.execute(executor: pool) { build_and_upload_blob(image, page_number) }
|
||||||
|
ensure
|
||||||
|
doc_page&.close
|
||||||
end
|
end
|
||||||
|
|
||||||
Concurrent::Promise.zip(*promises).value!.each do |blob|
|
Concurrent::Promise.zip(*promises).value!.each do |blob|
|
||||||
@@ -122,39 +130,27 @@ module Templates
|
|||||||
pool&.kill
|
pool&.kill
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_and_upload_blob(doc, page_number, format = FORMAT)
|
def build_and_upload_blob(image, page_number, format = FORMAT)
|
||||||
doc_page = doc.get_page(page_number)
|
image = image.copy(interpretation: :srgb)
|
||||||
|
|
||||||
data, width, height = doc_page.render_to_bitmap(width: MAX_WIDTH)
|
|
||||||
|
|
||||||
page = Vips::Image.new_from_memory(data, width, height, 4, :uchar)
|
|
||||||
|
|
||||||
page = page.copy(interpretation: :srgb)
|
|
||||||
|
|
||||||
data =
|
data =
|
||||||
if format == FORMAT
|
if format == FORMAT
|
||||||
bitdepth = 2**page.stats.to_a[1..3].pluck(2).uniq.size
|
bitdepth = 2**image.stats.to_a[1..3].pluck(2).uniq.size
|
||||||
|
|
||||||
page.write_to_buffer(format, compression: 6, filter: 0, bitdepth:,
|
image.write_to_buffer(format, compression: 6, filter: 0, bitdepth:,
|
||||||
palette: true, Q: Q, dither: 0)
|
palette: true, Q: Q, dither: 0)
|
||||||
else
|
else
|
||||||
page.write_to_buffer(format, interlace: true, Q: JPEG_Q)
|
image.write_to_buffer(format, interlace: true, Q: JPEG_Q)
|
||||||
end
|
end
|
||||||
|
|
||||||
blob = ActiveStorage::Blob.new(
|
blob = ActiveStorage::Blob.new(
|
||||||
filename: "#{page_number}#{format}",
|
filename: "#{page_number}#{format}",
|
||||||
metadata: { analyzed: true, identified: true, width: page.width, height: page.height }
|
metadata: { analyzed: true, identified: true, width: image.width, height: image.height }
|
||||||
)
|
)
|
||||||
|
|
||||||
blob.upload(StringIO.new(data))
|
blob.upload(StringIO.new(data))
|
||||||
|
|
||||||
blob
|
blob
|
||||||
rescue Vips::Error, Pdfium::PdfiumError => e
|
|
||||||
Rollbar.warning(e) if defined?(Rollbar)
|
|
||||||
|
|
||||||
nil
|
|
||||||
ensure
|
|
||||||
doc_page&.close
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def maybe_flatten_form(data, pdf)
|
def maybe_flatten_form(data, pdf)
|
||||||
@@ -199,7 +195,15 @@ module Templates
|
|||||||
def generate_pdf_preview_from_file(attachment, file_path, page_number)
|
def generate_pdf_preview_from_file(attachment, file_path, page_number)
|
||||||
doc = Pdfium::Document.open_file(file_path)
|
doc = Pdfium::Document.open_file(file_path)
|
||||||
|
|
||||||
blob = build_and_upload_blob(doc, page_number, PREVIEW_FORMAT)
|
doc_page = doc.get_page(page_number)
|
||||||
|
|
||||||
|
bytes, width, height = doc_page.render_to_bitmap(width: MAX_WIDTH)
|
||||||
|
|
||||||
|
doc_page.close
|
||||||
|
|
||||||
|
image = Vips::Image.new_from_memory(bytes, width, height, 4, :uchar)
|
||||||
|
|
||||||
|
blob = build_and_upload_blob(image, page_number, PREVIEW_FORMAT)
|
||||||
|
|
||||||
ApplicationRecord.no_touching do
|
ApplicationRecord.no_touching do
|
||||||
ActiveStorage::Attachment.create!(
|
ActiveStorage::Attachment.create!(
|
||||||
|
|||||||
Reference in New Issue
Block a user