one-off submission
This commit is contained in:
committed by
Pete Matsyburka
parent
c59d948d41
commit
f03166b4ea
@@ -4,7 +4,7 @@ module Templates
|
||||
module CloneAttachments
|
||||
module_function
|
||||
|
||||
def call(template:, original_template:, documents: [], excluded_attachment_uuids: [])
|
||||
def call(template:, original_template:, documents: [], excluded_attachment_uuids: [], save: true)
|
||||
schema_uuids_replacements = {}
|
||||
|
||||
template.schema.each_with_index do |schema_item, index|
|
||||
@@ -29,32 +29,31 @@ module Templates
|
||||
end
|
||||
end
|
||||
|
||||
template.save!
|
||||
attachments =
|
||||
original_template.schema_documents.filter_map do |document|
|
||||
new_attachment_uuid = schema_uuids_replacements[document.uuid]
|
||||
|
||||
original_template.schema_documents.filter_map do |document|
|
||||
new_attachment_uuid = schema_uuids_replacements[document.uuid]
|
||||
next unless new_attachment_uuid
|
||||
|
||||
next unless new_attachment_uuid
|
||||
|
||||
new_document =
|
||||
ApplicationRecord.no_touching do
|
||||
template.documents_attachments.create!(
|
||||
new_document =
|
||||
template.documents_attachments.new(
|
||||
uuid: new_attachment_uuid,
|
||||
blob_id: document.blob_id
|
||||
)
|
||||
end
|
||||
|
||||
clone_document_preview_images_attachments(document:, new_document:)
|
||||
clone_document_preview_images_attachments(document:, new_document:)
|
||||
|
||||
new_document
|
||||
end
|
||||
new_document
|
||||
end
|
||||
|
||||
template.save! if save
|
||||
|
||||
attachments
|
||||
end
|
||||
|
||||
def clone_document_preview_images_attachments(document:, new_document:)
|
||||
ApplicationRecord.no_touching do
|
||||
document.preview_images_attachments.each do |preview_image|
|
||||
new_document.preview_images_attachments.create!(blob_id: preview_image.blob_id)
|
||||
end
|
||||
document.preview_images_attachments.each do |preview_image|
|
||||
new_document.preview_images_attachments.new(blob_id: preview_image.blob_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -36,6 +36,24 @@ module Templates
|
||||
attachment
|
||||
end
|
||||
|
||||
def process(attachment, data, extract_fields: false)
|
||||
if attachment.content_type == PDF_CONTENT_TYPE && extract_fields && data.size < MAX_FLATTEN_FILE_SIZE
|
||||
pdf = HexaPDF::Document.new(io: StringIO.new(data))
|
||||
|
||||
fields = Templates::FindAcroFields.call(pdf, attachment, data)
|
||||
end
|
||||
|
||||
pdf ||= HexaPDF::Document.new(io: StringIO.new(data))
|
||||
|
||||
number_of_pages = pdf.pages.size
|
||||
|
||||
attachment.metadata['pdf'] ||= {}
|
||||
attachment.metadata['pdf']['number_of_pages'] = number_of_pages
|
||||
attachment.metadata['pdf']['fields'] = fields if fields
|
||||
|
||||
attachment
|
||||
end
|
||||
|
||||
def generate_preview_image(attachment, data)
|
||||
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
|
||||
|
||||
|
||||
Reference in New Issue
Block a user