Files
docuseal/app/controllers/templates_uploads_controller.rb
T

75 lines
2.1 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
2024-06-18 16:51:42 +03:00
SendTemplateCreatedWebhookRequestJob.perform_async('template_id' => @template.id)
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?
2023-10-28 23:07:18 +03:00
redirect_to root_path, alert: 'Unable to upload file'
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
2023-11-08 15:25:10 +02:00
tempfile.write(conn.get(Addressable::URI.parse(params[:url]).display_uri.to_s).body)
2023-10-28 23:07:18 +03:00
tempfile.rewind
file = ActionDispatch::Http::UploadedFile.new(
tempfile:,
filename: File.basename(
2024-07-20 21:17:47 +03:00
URI.decode_www_form_component(params[:filename].presence || params[:url]), '.*'
2023-10-28 23:07:18 +03:00
),
type: Marcel::MimeType.for(tempfile)
)
{ files: [file] }
end
def conn
Faraday.new do |faraday|
faraday.response :follow_redirects
end
2023-09-14 01:43:08 +03:00
end
end