optimize document upload
This commit is contained in:
@@ -31,8 +31,6 @@ class SubmitFormController < ApplicationController
|
|||||||
|
|
||||||
Submitters::MaybeUpdateDefaultValues.call(@submitter, current_user)
|
Submitters::MaybeUpdateDefaultValues.call(@submitter, current_user)
|
||||||
|
|
||||||
cookies[:submitter_sid] = @submitter.signed_id
|
|
||||||
|
|
||||||
render(@submitter.submission.template.archived_at? || @submitter.submission.archived_at? ? :archived : :show)
|
render(@submitter.submission.template.archived_at? || @submitter.submission.archived_at? ? :archived : :show)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -61,9 +61,6 @@ Rails.configuration.to_prepare do
|
|||||||
end
|
end
|
||||||
|
|
||||||
ActiveStorage::DirectUploadsController.before_action do
|
ActiveStorage::DirectUploadsController.before_action do
|
||||||
next if current_user
|
|
||||||
next if Submitter.find_signed(cookies[:submitter_sid])
|
|
||||||
|
|
||||||
head :forbidden
|
head :forbidden
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -10,60 +10,39 @@ module Templates
|
|||||||
module_function
|
module_function
|
||||||
|
|
||||||
def call(template, params)
|
def call(template, params)
|
||||||
find_or_create_blobs(params).map do |blob|
|
Array.wrap(params[:files].presence || params[:file]).map do |file|
|
||||||
document_data = blob.download
|
if file.content_type.exclude?('image') && file.content_type != PDF_CONTENT_TYPE
|
||||||
|
file, document_data = handle_file_types(file)
|
||||||
if !blob.image? && blob.content_type != PDF_CONTENT_TYPE
|
|
||||||
blob, document_data = handle_file_types(blob, document_data)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
document_data ||= file.read
|
||||||
|
|
||||||
|
if file.content_type == PDF_CONTENT_TYPE
|
||||||
|
document_data = maybe_decrypt_pdf_or_raise(document_data, params)
|
||||||
|
|
||||||
|
annotations =
|
||||||
|
document_data.size < ANNOTATIONS_SIZE_LIMIT ? Templates::BuildAnnotations.call(document_data) : []
|
||||||
|
end
|
||||||
|
|
||||||
|
sha256 = Base64.urlsafe_encode64(Digest::SHA256.digest(document_data))
|
||||||
|
|
||||||
|
blob = ActiveStorage::Blob.create_and_upload!(
|
||||||
|
io: StringIO.new(document_data),
|
||||||
|
filename: file.original_filename,
|
||||||
|
metadata: {
|
||||||
|
identified: file.content_type == PDF_CONTENT_TYPE,
|
||||||
|
analyzed: file.content_type == PDF_CONTENT_TYPE,
|
||||||
|
pdf: { annotations: }.compact_blank, sha256:
|
||||||
|
}.compact_blank,
|
||||||
|
content_type: file.content_type
|
||||||
|
)
|
||||||
|
|
||||||
document = template.documents.create!(blob:)
|
document = template.documents.create!(blob:)
|
||||||
|
|
||||||
if blob.content_type == PDF_CONTENT_TYPE && blob.metadata['pdf'].nil?
|
|
||||||
data = maybe_decrypt_pdf_or_raise(document_data, params)
|
|
||||||
|
|
||||||
if data != document_data
|
|
||||||
blob = ActiveStorage::Blob.create_and_upload!(
|
|
||||||
io: StringIO.new(new_data), filename: blob.filename
|
|
||||||
)
|
|
||||||
document_data = data
|
|
||||||
end
|
|
||||||
|
|
||||||
annotations =
|
|
||||||
document_data.size > ANNOTATIONS_SIZE_LIMIT ? [] : Templates::BuildAnnotations.call(document_data)
|
|
||||||
|
|
||||||
blob.metadata['pdf'] = { 'annotations' => annotations }
|
|
||||||
blob.metadata['sha256'] = Base64.urlsafe_encode64(Digest::SHA256.digest(document_data))
|
|
||||||
end
|
|
||||||
|
|
||||||
blob.save!
|
|
||||||
|
|
||||||
Templates::ProcessDocument.call(document, document_data)
|
Templates::ProcessDocument.call(document, document_data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_or_create_blobs(params)
|
|
||||||
params[:files].map do |file|
|
|
||||||
data = file.read
|
|
||||||
|
|
||||||
if file.content_type == PDF_CONTENT_TYPE
|
|
||||||
data = maybe_decrypt_pdf_or_raise(data, params)
|
|
||||||
|
|
||||||
annotations = data.size > ANNOTATIONS_SIZE_LIMIT ? [] : Templates::BuildAnnotations.call(data)
|
|
||||||
metadata = { 'identified' => true, 'analyzed' => true,
|
|
||||||
'sha256' => Base64.urlsafe_encode64(Digest::SHA256.digest(data)),
|
|
||||||
'pdf' => { 'annotations' => annotations } }
|
|
||||||
end
|
|
||||||
|
|
||||||
ActiveStorage::Blob.create_and_upload!(
|
|
||||||
io: StringIO.new(data),
|
|
||||||
filename: file.original_filename,
|
|
||||||
metadata:,
|
|
||||||
content_type: file.content_type
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def maybe_decrypt_pdf_or_raise(data, params)
|
def maybe_decrypt_pdf_or_raise(data, params)
|
||||||
if data.size < ANNOTATIONS_SIZE_LIMIT && PdfUtils.encrypted?(data)
|
if data.size < ANNOTATIONS_SIZE_LIMIT && PdfUtils.encrypted?(data)
|
||||||
PdfUtils.decrypt(data, params[:password])
|
PdfUtils.decrypt(data, params[:password])
|
||||||
@@ -74,7 +53,7 @@ module Templates
|
|||||||
raise PdfEncrypted
|
raise PdfEncrypted
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_file_types(_document_data, blob)
|
def handle_file_types(_file)
|
||||||
raise InvalidFileType, blob.content_type
|
raise InvalidFileType, blob.content_type
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user