Files
docuseal/app/controllers/templates_uploads_controller.rb
T

77 lines
2.3 KiB
Ruby
Raw Normal View History

2023-09-14 01:43:08 +03:00
# frozen_string_literal: true
class TemplatesUploadsController < ApplicationController
2023-09-17 00:45:57 +03:00
load_and_authorize_resource :template, parent: false
2023-10-28 23:07:18 +03:00
layout 'plain'
def show; end
2023-09-14 01:43:08 +03:00
def create
2023-10-28 23:07:18 +03:00
url_params = create_file_params_from_url if params[:url].present?
2024-01-14 23:22:41 +02:00
save_template!(@template, url_params)
2023-09-14 01:43:08 +03:00
2024-06-22 13:16:22 +03:00
documents = Templates::CreateAttachments.call(@template, url_params || params, extract_fields: true)
2023-09-14 01:43:08 +03:00
schema = documents.map { |doc| { attachment_uuid: doc.uuid, name: doc.filename.base } }
2024-07-17 13:44:02 +03:00
if @template.fields.blank?
@template.fields = Templates::ProcessDocument.normalize_attachment_fields(@template, documents)
2024-06-22 13:16:22 +03:00
2024-07-17 13:44:02 +03:00
schema.each { |item| item['pending_fields'] = true } if @template.fields.present?
end
2024-06-22 13:16:22 +03:00
2024-07-17 13:44:02 +03:00
@template.update!(schema:)
2023-09-14 01:43:08 +03:00
enqueue_template_created_webhooks(@template)
2024-05-12 11:37:33 +03:00
2023-09-17 00:45:57 +03:00
redirect_to edit_template_path(@template)
2024-01-14 23:22:41 +02:00
rescue Templates::CreateAttachments::PdfEncrypted
render turbo_stream: turbo_stream.append(params[:form_id], html: helpers.tag.prompt_password)
2023-10-28 23:07:18 +03:00
rescue StandardError => e
Rollbar.error(e) if defined?(Rollbar)
2024-05-27 00:39:36 +03:00
raise if Rails.env.local?
2024-09-18 19:47:47 +03:00
redirect_to root_path, alert: I18n.t('unable_to_update_file')
2023-10-28 23:07:18 +03:00
end
private
2024-01-14 23:22:41 +02:00
def save_template!(template, url_params)
template.account = current_account
template.author = current_user
template.folder = TemplateFolders.find_or_create_by_name(current_user, params[:folder_name])
template.name = File.basename((url_params || params)[:files].first.original_filename, '.*')
template.save!
template
end
2023-10-28 23:07:18 +03:00
def create_file_params_from_url
tempfile = Tempfile.new
tempfile.binmode
2024-09-07 01:30:34 +03:00
tempfile.write(DownloadUtils.call(params[:url]).body)
2023-10-28 23:07:18 +03:00
tempfile.rewind
2024-11-25 18:59:37 +02:00
filename = URI.decode_www_form_component(params[:filename]) if params[:filename].present?
filename ||= File.basename(URI.decode_www_form_component(params[:url]))
2023-10-28 23:07:18 +03:00
file = ActionDispatch::Http::UploadedFile.new(
tempfile:,
2024-11-25 18:59:37 +02:00
filename:,
2023-10-28 23:07:18 +03:00
type: Marcel::MimeType.for(tempfile)
)
{ files: [file] }
end
def enqueue_template_created_webhooks(template)
2024-11-01 15:35:02 +02:00
WebhookUrls.for_account_id(template.account_id, 'template.created').each do |webhook_url|
SendTemplateCreatedWebhookRequestJob.perform_async('template_id' => template.id,
'webhook_url_id' => webhook_url.id)
end
end
2023-09-14 01:43:08 +03:00
end