use png for previews
This commit is contained in:
committed by
Oleksandr Turchyn
parent
26ae5f1d80
commit
bc4414a368
@@ -18,7 +18,7 @@ module Api
|
|||||||
|
|
||||||
preview_image_attachments =
|
preview_image_attachments =
|
||||||
ActiveStorage::Attachment.joins(:blob)
|
ActiveStorage::Attachment.joins(:blob)
|
||||||
.where(blob: { filename: '0.jpg' })
|
.where(blob: { filename: '0.png' })
|
||||||
.where(record_id: schema_documents.map(&:id),
|
.where(record_id: schema_documents.map(&:id),
|
||||||
record_type: 'ActiveStorage::Attachment',
|
record_type: 'ActiveStorage::Attachment',
|
||||||
name: :preview_images)
|
name: :preview_images)
|
||||||
|
|||||||
@@ -3,11 +3,12 @@
|
|||||||
module Templates
|
module Templates
|
||||||
module ProcessDocument
|
module ProcessDocument
|
||||||
DPI = 200
|
DPI = 200
|
||||||
FORMAT = '.jpg'
|
FORMAT = '.png'
|
||||||
ATTACHMENT_NAME = 'preview_images'
|
ATTACHMENT_NAME = 'preview_images'
|
||||||
|
|
||||||
PDF_CONTENT_TYPE = 'application/pdf'
|
PDF_CONTENT_TYPE = 'application/pdf'
|
||||||
Q = ENV.fetch('PAGE_QUALITY', '35').to_i
|
Q = 95
|
||||||
|
JPEG_Q = ENV.fetch('PAGE_QUALITY', '35').to_i
|
||||||
MAX_WIDTH = 1400
|
MAX_WIDTH = 1400
|
||||||
MAX_NUMBER_OF_PAGES_PROCESSED = 15
|
MAX_NUMBER_OF_PAGES_PROCESSED = 15
|
||||||
MAX_FLATTEN_FILE_SIZE = 20.megabytes
|
MAX_FLATTEN_FILE_SIZE = 20.megabytes
|
||||||
@@ -15,7 +16,7 @@ module Templates
|
|||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def call(attachment, data, extract_fields: false, image_quality: nil)
|
def call(attachment, data, extract_fields: false)
|
||||||
if attachment.content_type == PDF_CONTENT_TYPE
|
if attachment.content_type == PDF_CONTENT_TYPE
|
||||||
if extract_fields && data.size < MAX_FLATTEN_FILE_SIZE
|
if extract_fields && data.size < MAX_FLATTEN_FILE_SIZE
|
||||||
pdf = HexaPDF::Document.new(io: StringIO.new(data))
|
pdf = HexaPDF::Document.new(io: StringIO.new(data))
|
||||||
@@ -23,23 +24,26 @@ module Templates
|
|||||||
fields = Templates::FindAcroFields.call(pdf, attachment)
|
fields = Templates::FindAcroFields.call(pdf, attachment)
|
||||||
end
|
end
|
||||||
|
|
||||||
generate_pdf_preview_images(attachment, data, pdf, image_quality:)
|
generate_pdf_preview_images(attachment, data, pdf)
|
||||||
|
|
||||||
attachment.metadata['pdf']['fields'] = fields if fields
|
attachment.metadata['pdf']['fields'] = fields if fields
|
||||||
elsif attachment.image?
|
elsif attachment.image?
|
||||||
generate_preview_image(attachment, data, image_quality:)
|
generate_preview_image(attachment, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
attachment
|
attachment
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_preview_image(attachment, data, image_quality: nil)
|
def generate_preview_image(attachment, data)
|
||||||
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
|
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
|
||||||
|
|
||||||
image = Vips::Image.new_from_buffer(data, '')
|
image = Vips::Image.new_from_buffer(data, '')
|
||||||
image = image.autorot.resize(MAX_WIDTH / image.width.to_f)
|
image = image.autorot.resize(MAX_WIDTH / image.width.to_f)
|
||||||
|
|
||||||
io = StringIO.new(image.write_to_buffer(FORMAT, Q: image_quality || Q, interlace: true))
|
bitdepth = 2**image.stats.to_a[1..3].pluck(2).uniq.size
|
||||||
|
|
||||||
|
io = StringIO.new(image.write_to_buffer(FORMAT, compression: 7, filter: 0, bitdepth:,
|
||||||
|
palette: true, Q: Q, dither: 0))
|
||||||
|
|
||||||
ActiveStorage::Attachment.create!(
|
ActiveStorage::Attachment.create!(
|
||||||
blob: ActiveStorage::Blob.create_and_upload!(
|
blob: ActiveStorage::Blob.create_and_upload!(
|
||||||
@@ -51,7 +55,7 @@ module Templates
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_pdf_preview_images(attachment, data, pdf = nil, image_quality: nil)
|
def generate_pdf_preview_images(attachment, data, pdf = nil)
|
||||||
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
|
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
|
||||||
|
|
||||||
pdf ||= HexaPDF::Document.new(io: StringIO.new(data))
|
pdf ||= HexaPDF::Document.new(io: StringIO.new(data))
|
||||||
@@ -70,7 +74,10 @@ module Templates
|
|||||||
page = Vips::Image.new_from_buffer(data, '', dpi: DPI, page: page_number)
|
page = Vips::Image.new_from_buffer(data, '', dpi: DPI, page: page_number)
|
||||||
page = page.resize(MAX_WIDTH / page.width.to_f)
|
page = page.resize(MAX_WIDTH / page.width.to_f)
|
||||||
|
|
||||||
io = StringIO.new(page.write_to_buffer(FORMAT, Q: image_quality || Q, interlace: true))
|
bitdepth = 2**page.stats.to_a[1..3].pluck(2).uniq.size
|
||||||
|
|
||||||
|
io = StringIO.new(page.write_to_buffer(FORMAT, compression: 7, filter: 0, bitdepth:,
|
||||||
|
palette: true, Q: Q, dither: 0))
|
||||||
|
|
||||||
ApplicationRecord.no_touching do
|
ApplicationRecord.no_touching do
|
||||||
ActiveStorage::Attachment.create!(
|
ActiveStorage::Attachment.create!(
|
||||||
@@ -115,11 +122,11 @@ module Templates
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_pdf_preview_from_file(attachment, file_path, page_number, image_quality: nil)
|
def generate_pdf_preview_from_file(attachment, file_path, page_number)
|
||||||
io = StringIO.new
|
io = StringIO.new
|
||||||
|
|
||||||
command = [
|
command = [
|
||||||
'pdftocairo', '-jpeg', '-jpegopt', "progressive=y,quality=#{image_quality || Q},optimize=y",
|
'pdftocairo', '-jpeg', '-jpegopt', "progressive=y,quality=#{JPEG_Q},optimize=y",
|
||||||
'-scale-to-x', MAX_WIDTH, '-scale-to-y', '-1',
|
'-scale-to-x', MAX_WIDTH, '-scale-to-y', '-1',
|
||||||
'-r', DPI, '-f', page_number + 1, '-l', page_number + 1,
|
'-r', DPI, '-f', page_number + 1, '-l', page_number + 1,
|
||||||
'-singlefile', Shellwords.escape(file_path), '-'
|
'-singlefile', Shellwords.escape(file_path), '-'
|
||||||
@@ -138,7 +145,7 @@ module Templates
|
|||||||
ApplicationRecord.no_touching do
|
ApplicationRecord.no_touching do
|
||||||
ActiveStorage::Attachment.create!(
|
ActiveStorage::Attachment.create!(
|
||||||
blob: ActiveStorage::Blob.create_and_upload!(
|
blob: ActiveStorage::Blob.create_and_upload!(
|
||||||
io:, filename: "#{page_number}#{FORMAT}",
|
io:, filename: "#{page_number}.jpg",
|
||||||
metadata: { analyzed: true, identified: true, width: page.width, height: page.height }
|
metadata: { analyzed: true, identified: true, width: page.width, height: page.height }
|
||||||
),
|
),
|
||||||
name: ATTACHMENT_NAME,
|
name: ATTACHMENT_NAME,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ module Templates
|
|||||||
|
|
||||||
preview_image_attachments ||=
|
preview_image_attachments ||=
|
||||||
ActiveStorage::Attachment.joins(:blob)
|
ActiveStorage::Attachment.joins(:blob)
|
||||||
.where(blob: { filename: '0.jpg' })
|
.where(blob: { filename: ['0.jpg', '0.png'] })
|
||||||
.where(record_id: schema_documents.map(&:id),
|
.where(record_id: schema_documents.map(&:id),
|
||||||
record_type: 'ActiveStorage::Attachment',
|
record_type: 'ActiveStorage::Attachment',
|
||||||
name: :preview_images)
|
name: :preview_images)
|
||||||
@@ -29,7 +29,7 @@ module Templates
|
|||||||
attachment = schema_documents.find { |e| e.uuid == item['attachment_uuid'] }
|
attachment = schema_documents.find { |e| e.uuid == item['attachment_uuid'] }
|
||||||
|
|
||||||
first_page_blob = preview_image_attachments.find { |e| e.record_id == attachment.id }&.blob
|
first_page_blob = preview_image_attachments.find { |e| e.record_id == attachment.id }&.blob
|
||||||
first_page_blob ||= attachment.preview_images.joins(:blob).find_by(blob: { filename: '0.jpg' })&.blob
|
first_page_blob ||= attachment.preview_images.joins(:blob).find_by(blob: { filename: ['0.jpg', '0.png'] })&.blob
|
||||||
|
|
||||||
{
|
{
|
||||||
id: attachment.id,
|
id: attachment.id,
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ describe 'Templates API', type: :request do
|
|||||||
attachment = template.schema_documents.preload(:blob).find { |e| e.uuid == template_attachment_uuid }
|
attachment = template.schema_documents.preload(:blob).find { |e| e.uuid == template_attachment_uuid }
|
||||||
first_page_blob =
|
first_page_blob =
|
||||||
ActiveStorage::Attachment.joins(:blob)
|
ActiveStorage::Attachment.joins(:blob)
|
||||||
.where(blob: { filename: '0.jpg' })
|
.where(blob: { filename: '0.png' })
|
||||||
.where(record_id: template.schema_documents.map(&:id),
|
.where(record_id: template.schema_documents.map(&:id),
|
||||||
record_type: 'ActiveStorage::Attachment',
|
record_type: 'ActiveStorage::Attachment',
|
||||||
name: :preview_images)
|
name: :preview_images)
|
||||||
|
|||||||
Reference in New Issue
Block a user