From 0c8ffa04c3a7afe075e5955be2480171f8699215 Mon Sep 17 00:00:00 2001 From: iozeey Date: Thu, 16 Nov 2023 16:15:56 +0500 Subject: [PATCH] delete image and add blank page basics --- .../api/templates_documents_controller.rb | 26 ++++ app/javascript/template_builder/builder.vue | 71 +++++++++- app/javascript/template_builder/preview.vue | 2 +- app/javascript/template_builder/upload.vue | 130 +++++++++--------- config/routes.rb | 5 +- lib/templates/process_document.rb | 48 +++++++ 6 files changed, 211 insertions(+), 71 deletions(-) diff --git a/app/controllers/api/templates_documents_controller.rb b/app/controllers/api/templates_documents_controller.rb index 04fdb12c..bfb8b459 100644 --- a/app/controllers/api/templates_documents_controller.rb +++ b/app/controllers/api/templates_documents_controller.rb @@ -23,5 +23,31 @@ module Api ) } end + + def del_image + template = Template.find(params[:template_id]) + attachment_id = params[:attachment_id] + document_id = params[:documentId] + page_number = template.documents.find(document_id).preview_images.find_index { |pic| pic.id == attachment_id } + if page_number + Templates::ProcessDocument.delete_picture(template, attachment_id, page_number) + render json: { success: true, message: 'New blank image added successfully' } + else + page_number = "No image found for deletion" + render json: { success: false, message: "Error: #{page_number}" }, status: :unprocessable_entity + end + end + + def add_new_image + byebug + template = Template.find(params[:template_id]) + begin + Templates::ProcessDocument.upload_new_blank_image(template) + render json: { success: true, message: 'New blank image added successfully' } + rescue StandardError => e + render json: { success: false, message: "Error adding new blank image: #{e.message}" }, status: :unprocessable_entity + end + end + end end diff --git a/app/javascript/template_builder/builder.vue b/app/javascript/template_builder/builder.vue index 731f21e5..31ad58cc 100644 --- a/app/javascript/template_builder/builder.vue +++ b/app/javascript/template_builder/builder.vue @@ -742,7 +742,7 @@ export default { headers: { 'Content-Type': 'application/json' } }) }, - removeImage (imageId) { + removeImage ({ item, imageId }) { console.log(this.template.documents[0].preview_secured_images) // output: 0: {id: 26, name: 'preview_images', uuid: 'db2de68e-c52f-41e0-a743-550a5ba26ec0', record_type: 'ActiveStorage::Attachment', record_id: 25, …} 1 : {id: 27, name: 'preview_images', uuid: '4f2cd882-95fb-4344-8571-78c9bb5a20aa', record_type: 'ActiveStorage::Attachment', record_id: 25, …} 2: {id: 28, name: 'preview_images', uuid: '28bb656a-0799-4a73-b665-692aab2c690b', record_type: 'ActiveStorage::Attachment', record_id: 25, …} length: 3, imageId: 27 // const indexToRemove = this.template.documents.findIndex(item => item.id === imageId) @@ -750,17 +750,78 @@ export default { // Check if the document has a preview_images array if (Array.isArray(document.preview_images)) { // Find the index of the preview image with the matching id - console.log('simple preview', document.preview_images, ': secured', document.preview_secured_images) const indexToRemove = document.preview_images.findIndex( (previewImage) => previewImage.id === imageId ) + // console.log('simple preview', document.preview_images, ': secured', document.preview_secured_images) + console.log(indexToRemove) if (indexToRemove !== -1) { - document.preview_images.splice(indexToRemove, 1) - // Optionally, emit a 'change' event if needed - // this.$emit('change'); + // here I want to send call to a controller named template_document + const documentId = document.id + const apiUrl = `/api/templates/${this.template.id}/documents/${documentId}/del_image` + fetch(apiUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + template: this.template.id, + attachment_id: imageId, + documentId + // page_number: document.preview_images.find(x => x.id === imageId).name + }) + }) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`) + } + return response.json() + }) + .then((data) => { + // remove from frontend + document.preview_images.splice(indexToRemove, 1) + console.log('Success:', data) + }) + .catch((error) => { + console.error('Error:', error) + }) } } }) + }, + addBlankPage (item) { + console.log(this.sortedDocuments) + // console.log(this.template) + const documentId = item.id + console.log(documentId) + const apiUrl = `/api/templates/${this.template.id}/documents/${documentId}/add_new_image` + fetch(apiUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + template: this.template.id, + document: item + }) + }) + // this.baseFetch(`/api/templates/${this.templateId}/documents/add_new_image`, { + // method: 'POST', + // body: JSON.stringify({ template: this.template }), + // headers: { 'Content-Type': 'application/json' } + // }) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`) + } + return response.json() + }) + .then((data) => { + console.log('Success:', data) + }) + .catch((error) => { + console.error('Error:', error) + }) } } } diff --git a/app/javascript/template_builder/preview.vue b/app/javascript/template_builder/preview.vue index 24a7ee5c..e87d3af1 100644 --- a/app/javascript/template_builder/preview.vue +++ b/app/javascript/template_builder/preview.vue @@ -73,7 +73,7 @@ diff --git a/app/javascript/template_builder/upload.vue b/app/javascript/template_builder/upload.vue index bb44256a..f34f1049 100644 --- a/app/javascript/template_builder/upload.vue +++ b/app/javascript/template_builder/upload.vue @@ -10,15 +10,24 @@ width="20" class="animate-spin" /> - - Uploading... - Processing... - Add Document - - - diff --git a/config/routes.rb b/config/routes.rb index d4fb5a91..9eea5e25 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,7 +43,10 @@ Rails.application.routes.draw do end resources :templates, only: %i[update show index destroy] do resources :submissions, only: %i[index create] - resources :documents, only: %i[create], controller: 'templates_documents' + resources :documents, only: %i[create], controller: 'templates_documents'do + post 'add_new_image', on: :member + post 'del_image', on: :member + end end end diff --git a/lib/templates/process_document.rb b/lib/templates/process_document.rb index f42249f4..cc987005 100644 --- a/lib/templates/process_document.rb +++ b/lib/templates/process_document.rb @@ -105,11 +105,14 @@ module Templates def generate_pdf_secured_preview_images(template, attachment, data) ActiveStorage::Attachment.where(name: SECURED_ATTACHMENT_NAME, record: attachment).destroy_all + # delete field image from pdf document throught hexapdf number_of_pages = PDF::Reader.new(StringIO.new(data)).pages.size - 1 (0..number_of_pages).each do |page_number| pdf = Vips::Image.new_from_buffer(data, '', dpi: DPI, page: page_number) pdf = pdf.resize(MAX_WIDTH / pdf.width.to_f) redacted_boxes = template.fields.select { |field| field['type'] == 'redact' && field['areas'][0]['page'] == page_number } + # deleted_page_field = template.fields.select { |field| field['type'] == 'deleted_page' && field['areas'][0]['page'] == page_number } + # break if !deleted_page_field.empty? if !redacted_boxes.empty? redacted_boxes.each do |box| x = (box['areas'][0]['x'] * pdf.width).to_i @@ -134,5 +137,50 @@ module Templates end end + def delete_picture(template, attachment_id, page_number) + attachment = ActiveStorage::Attachment.find_by(id: attachment_id) + byebug + return unless attachment + attachment.purge + deleted_page_field = { + 'type' => 'deleted_page', + 'areas' => [{ + 'page' => page_number, + 'x' => 0, + 'y' => 0, + 'w' => 1, + 'h' => 1 + }] + } + template.fields << deleted_page_field + template.save! + end + + def upload_new_blank_image(template) + existing_document = template.documents.first + blank_image = generate_blank_image + blank_blob = create_blob_from_image(blank_image) + upload_new_attachment(template, blank_blob, ATTACHMENT_NAME) + # puts '-----New blank image uploaded successfully!-----' + Rails.logger.info('New blank image uploaded successfully!') + rescue StandardError => e + Rails.logger.error("Error uploading new blank image: #{e.message}") + end + def generate_blank_image + height = 2000 + Vips::Image.black(MAX_WIDTH, height) + end + def create_blob_from_image(image) + ActiveStorage::Blob.create_and_upload!( + io: StringIO.new(image.write_to_buffer(FORMAT, Q: Q, interlace: true)), + filename: "#{SecureRandom.uuid}#{FORMAT}", + metadata: { analyzed: true, identified: true, width: image.width, height: image.height } + ) + end + def upload_new_attachment(template, blob, attachment_name) + template.documents.attach(blob) + template.documents.last.update!(name: attachment_name) + end + end end