Files

77 lines
2.1 KiB
Ruby
Raw Permalink Normal View History

2024-06-22 13:16:22 +03:00
# frozen_string_literal: true
class TemplatesDebugController < ApplicationController
load_and_authorize_resource :template
2025-08-12 18:40:55 +03:00
DEBUG_FILE = ''
2024-06-22 13:16:22 +03:00
def show
2025-10-28 10:52:37 +02:00
schema_uuids = @template.schema.index_by { |e| e['attachment_uuid'] }
attachment = @template.documents.find { |a| schema_uuids[a.uuid] }
2024-06-22 13:16:22 +03:00
2026-02-12 09:22:08 +02:00
if attachment
data = attachment.download
2024-06-22 13:16:22 +03:00
2026-02-12 09:22:08 +02:00
unless attachment.image?
pdf = HexaPDF::Document.new(io: StringIO.new(data))
2025-10-28 10:52:37 +02:00
2026-02-12 09:22:08 +02:00
fields = Templates::FindAcroFields.call(pdf, attachment, data)
end
2025-10-28 10:52:37 +02:00
2026-02-12 09:22:08 +02:00
# fields, = Templates::DetectFields.call(StringIO.new(data), attachment:) if fields.blank?
2024-06-22 13:16:22 +03:00
2026-02-12 09:22:08 +02:00
attachment.metadata['pdf'] ||= {}
attachment.metadata['pdf']['fields'] = fields
2024-06-22 13:16:22 +03:00
2026-02-12 09:22:08 +02:00
@template.update!(fields: Templates::ProcessDocument.normalize_attachment_fields(@template, [attachment]))
end
2024-06-22 13:16:22 +03:00
2025-08-12 18:40:55 +03:00
debug_file if DEBUG_FILE.present?
2024-06-22 13:16:22 +03:00
ActiveRecord::Associations::Preloader.new(
records: [@template],
2026-04-02 12:48:28 +03:00
associations: [{ schema_documents: { preview_images_attachments: :blob } }]
2024-06-22 13:16:22 +03:00
).call
@template_data =
@template.as_json.merge(
documents: @template.schema_documents.as_json(
2026-02-12 09:22:08 +02:00
methods: %i[metadata signed_key],
2024-06-22 13:16:22 +03:00
include: { preview_images: { methods: %i[url metadata filename] } }
)
).to_json
render 'templates/edit', layout: 'plain'
end
2025-08-12 18:40:55 +03:00
def debug_file
tempfile = Tempfile.new
tempfile.binmode
tempfile.write(File.read(DEBUG_FILE))
tempfile.rewind
filename = File.basename(DEBUG_FILE)
file = ActionDispatch::Http::UploadedFile.new(
tempfile:,
filename:,
type: Marcel::MimeType.for(tempfile)
)
params = { files: [file] }
2026-02-12 09:22:08 +02:00
documents, dynamic_documents = Templates::CreateAttachments.call(@template, params,
dynamic: DEBUG_FILE.ends_with?('.docx'))
2025-08-12 18:40:55 +03:00
2026-02-12 09:22:08 +02:00
schema = documents.map do |doc|
{
attachment_uuid: doc.uuid,
name: doc.filename.base,
dynamic: dynamic_documents.find { |e| e.uuid == doc.uuid }.present?
}
end
2025-08-12 18:40:55 +03:00
@template.update!(schema:)
end
2024-06-22 13:16:22 +03:00
end