2025-10-28 10:52:37 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class TemplatesDetectFieldsController < ApplicationController
|
|
|
|
|
include ActionController::Live
|
|
|
|
|
|
|
|
|
|
load_and_authorize_resource :template
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
response.headers['Content-Type'] = 'text/event-stream'
|
|
|
|
|
|
|
|
|
|
sse = SSE.new(response.stream)
|
|
|
|
|
|
|
|
|
|
documents = @template.schema_documents.preload(:blob)
|
2026-01-18 17:22:55 +02:00
|
|
|
documents = documents.where(uuid: params[:attachment_uuid]) if params[:attachment_uuid].present?
|
|
|
|
|
|
|
|
|
|
page_number = params[:page].present? ? params[:page].to_i : nil
|
2025-10-28 10:52:37 +02:00
|
|
|
|
|
|
|
|
documents.each do |document|
|
|
|
|
|
io = StringIO.new(document.download)
|
|
|
|
|
|
2026-01-18 17:22:55 +02:00
|
|
|
Templates::DetectFields.call(io, attachment: document, page_number:) do |(attachment_uuid, page, fields)|
|
2025-10-28 10:52:37 +02:00
|
|
|
sse.write({ attachment_uuid:, page:, fields: })
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
sse.write({ completed: true })
|
|
|
|
|
ensure
|
|
|
|
|
response.stream.close
|
|
|
|
|
end
|
|
|
|
|
end
|