|  |  | @ -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 | 
			
		
	
	
		
		
			
				
					|  |  | 
 |