optimize document upload

pull/250/head
Pete Matsyburka 2 years ago
parent 8b823ca0d0
commit 2cb15f3282

@ -6,6 +6,8 @@ class TemplateDocumentsController < ApplicationController
def create def create
return head :unprocessable_entity if params[:blobs].blank? && params[:files].blank? return head :unprocessable_entity if params[:blobs].blank? && params[:files].blank?
old_fields_hash = @template.fields.hash
documents = Templates::CreateAttachments.call(@template, params) documents = Templates::CreateAttachments.call(@template, params)
schema = documents.map do |doc| schema = documents.map do |doc|
@ -14,6 +16,8 @@ class TemplateDocumentsController < ApplicationController
render json: { render json: {
schema:, schema:,
fields: old_fields_hash == @template.fields.hash ? nil : @template.fields,
submitters: old_fields_hash == @template.fields.hash ? nil : @template.submitters,
documents: documents.as_json( documents: documents.as_json(
methods: %i[metadata signed_uuid], methods: %i[metadata signed_uuid],
include: { include: {

@ -971,14 +971,26 @@ export default {
this.save() this.save()
}, },
updateFromUpload ({ schema, documents }) { updateFromUpload (data) {
this.template.schema.push(...schema) this.template.schema.push(...data.schema)
this.template.documents.push(...documents) this.template.documents.push(...data.documents)
if (data.fields) {
this.template.fields = data.fields
}
if (data.submitters) {
this.template.submitters = data.submitters
if (!this.template.submitters.find((s) => s.uuid === this.selectedSubmitter?.uuid)) {
this.selectedSubmitter = this.template.submitters[0]
}
}
this.$nextTick(() => { this.$nextTick(() => {
this.$refs.previews.scrollTop = this.$refs.previews.scrollHeight this.$refs.previews.scrollTop = this.$refs.previews.scrollHeight
this.scrollIntoDocument(schema[0]) this.scrollIntoDocument(data.schema[0])
}) })
if (this.template.name === 'New Document') { if (this.template.name === 'New Document') {
@ -1018,9 +1030,39 @@ export default {
this.save() this.save()
}, },
onDocumentReplace ({ replaceSchemaItem, schema, documents }) { onDocumentReplace (data) {
const { replaceSchemaItem, schema, documents } = data
this.template.schema.splice(this.template.schema.indexOf(replaceSchemaItem), 1, schema[0]) this.template.schema.splice(this.template.schema.indexOf(replaceSchemaItem), 1, schema[0])
this.template.documents.push(...documents) this.template.documents.push(...documents)
if (data.fields) {
this.template.fields = data.fields
const removedFieldUuids = []
this.template.fields.forEach((field) => {
[...(field.areas || [])].forEach((area) => {
if (area.attachment_uuid === replaceSchemaItem.attachment_uuid) {
field.areas.splice(field.areas.indexOf(area), 1)
removedFieldUuids.push(field.uuid)
}
})
})
this.template.fields =
this.template.fields.filter((f) => !removedFieldUuids.includes(f.uuid) || f.areas?.length)
}
if (data.submitters) {
this.template.submitters = data.submitters
if (!this.template.submitters.find((s) => s.uuid === this.selectedSubmitter?.uuid)) {
this.selectedSubmitter = this.template.submitters[0]
}
}
this.template.fields.forEach((field) => { this.template.fields.forEach((field) => {
(field.areas || []).forEach((area) => { (field.areas || []).forEach((area) => {
if (area.attachment_uuid === replaceSchemaItem.attachment_uuid) { if (area.attachment_uuid === replaceSchemaItem.attachment_uuid) {

@ -95,7 +95,7 @@
<label <label
v-if="compact" v-if="compact"
tabindex="0" tabindex="0"
:title="selectedSubmitter.name" :title="selectedSubmitter?.name"
class="cursor-pointer text-base-100 flex h-full items-center justify-center" class="cursor-pointer text-base-100 flex h-full items-center justify-center"
> >
<button <button

@ -10,37 +10,43 @@ module Templates
module_function module_function
def call(template, params) def call(template, params)
Array.wrap(params[:files].presence || params[:file]).map do |file| res = Array.wrap(params[:files].presence || params[:file]).map do |file|
if file.content_type.exclude?('image') && file.content_type != PDF_CONTENT_TYPE if file.content_type.exclude?('image') && file.content_type != PDF_CONTENT_TYPE
file, document_data = handle_file_types(file) next handle_file_types(template, file, params)
end end
document_data ||= file.read handle_pdf_or_image(template, file, file.read, params)
end
Rails.logger.debug res
res
end
if file.content_type == PDF_CONTENT_TYPE def handle_pdf_or_image(template, file, document_data = nil, params = {})
document_data = maybe_decrypt_pdf_or_raise(document_data, params) document_data ||= file.read
annotations = if file.content_type == PDF_CONTENT_TYPE
document_data.size < ANNOTATIONS_SIZE_LIMIT ? Templates::BuildAnnotations.call(document_data) : [] document_data = maybe_decrypt_pdf_or_raise(document_data, params)
end
sha256 = Base64.urlsafe_encode64(Digest::SHA256.digest(document_data)) annotations =
document_data.size < ANNOTATIONS_SIZE_LIMIT ? Templates::BuildAnnotations.call(document_data) : []
end
blob = ActiveStorage::Blob.create_and_upload!( sha256 = Base64.urlsafe_encode64(Digest::SHA256.digest(document_data))
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:) 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
)
Templates::ProcessDocument.call(document, document_data) document = template.documents.create!(blob:)
end
Templates::ProcessDocument.call(document, document_data)
end end
def maybe_decrypt_pdf_or_raise(data, params) def maybe_decrypt_pdf_or_raise(data, params)
@ -53,8 +59,8 @@ module Templates
raise PdfEncrypted raise PdfEncrypted
end end
def handle_file_types(_file) def handle_file_types(_template, file, params)
raise InvalidFileType, blob.content_type raise InvalidFileType, file.content_type
end end
end end
end end

Loading…
Cancel
Save