optimize document upload
This commit is contained in:
@@ -6,6 +6,8 @@ class TemplateDocumentsController < ApplicationController
|
||||
def create
|
||||
return head :unprocessable_entity if params[:blobs].blank? && params[:files].blank?
|
||||
|
||||
old_fields_hash = @template.fields.hash
|
||||
|
||||
documents = Templates::CreateAttachments.call(@template, params)
|
||||
|
||||
schema = documents.map do |doc|
|
||||
@@ -14,6 +16,8 @@ class TemplateDocumentsController < ApplicationController
|
||||
|
||||
render json: {
|
||||
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(
|
||||
methods: %i[metadata signed_uuid],
|
||||
include: {
|
||||
|
||||
@@ -971,14 +971,26 @@ export default {
|
||||
|
||||
this.save()
|
||||
},
|
||||
updateFromUpload ({ schema, documents }) {
|
||||
this.template.schema.push(...schema)
|
||||
this.template.documents.push(...documents)
|
||||
updateFromUpload (data) {
|
||||
this.template.schema.push(...data.schema)
|
||||
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.$refs.previews.scrollTop = this.$refs.previews.scrollHeight
|
||||
|
||||
this.scrollIntoDocument(schema[0])
|
||||
this.scrollIntoDocument(data.schema[0])
|
||||
})
|
||||
|
||||
if (this.template.name === 'New Document') {
|
||||
@@ -1018,9 +1030,39 @@ export default {
|
||||
|
||||
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.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) => {
|
||||
(field.areas || []).forEach((area) => {
|
||||
if (area.attachment_uuid === replaceSchemaItem.attachment_uuid) {
|
||||
|
||||
@@ -95,7 +95,7 @@
|
||||
<label
|
||||
v-if="compact"
|
||||
tabindex="0"
|
||||
:title="selectedSubmitter.name"
|
||||
:title="selectedSubmitter?.name"
|
||||
class="cursor-pointer text-base-100 flex h-full items-center justify-center"
|
||||
>
|
||||
<button
|
||||
|
||||
@@ -10,37 +10,43 @@ module Templates
|
||||
module_function
|
||||
|
||||
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
|
||||
file, document_data = handle_file_types(file)
|
||||
next handle_file_types(template, file, params)
|
||||
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:)
|
||||
|
||||
Templates::ProcessDocument.call(document, document_data)
|
||||
handle_pdf_or_image(template, file, file.read, params)
|
||||
end
|
||||
Rails.logger.debug res
|
||||
res
|
||||
end
|
||||
|
||||
def handle_pdf_or_image(template, file, document_data = nil, params = {})
|
||||
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:)
|
||||
|
||||
Templates::ProcessDocument.call(document, document_data)
|
||||
end
|
||||
|
||||
def maybe_decrypt_pdf_or_raise(data, params)
|
||||
@@ -53,8 +59,8 @@ module Templates
|
||||
raise PdfEncrypted
|
||||
end
|
||||
|
||||
def handle_file_types(_file)
|
||||
raise InvalidFileType, blob.content_type
|
||||
def handle_file_types(_template, file, params)
|
||||
raise InvalidFileType, file.content_type
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user