From e91e3b2453bbd7044532d4c9b838a828c084ef4e Mon Sep 17 00:00:00 2001 From: Wilson Gaturu Date: Fri, 18 Aug 2023 14:50:31 +0300 Subject: [PATCH] adds docx pdf conversion --- indigo_api/views/attachments.py | 24 ++++++++++++++++++- .../indigo/views/document_source_att.js | 24 ++++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/indigo_api/views/attachments.py b/indigo_api/views/attachments.py index 3a23cd3515..d6df27957e 100644 --- a/indigo_api/views/attachments.py +++ b/indigo_api/views/attachments.py @@ -1,4 +1,4 @@ -from django.http import HttpResponse +from django.http import HttpResponse, Http404 from django.shortcuts import get_list_or_404 from rest_framework import viewsets @@ -11,6 +11,16 @@ from ..authz import ModelPermissions, RelatedDocumentPermissions from .documents import DocumentResourceView from .misc import DEFAULT_PERMS +from docpipe.soffice import soffice_convert +import os + +DOC_MIMETYPES = [ + "application/vnd.oasis.opendocument.text", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/msword", + "application/rtf", + "text/rtf", +] def view_attachment(attachment): @@ -30,6 +40,16 @@ def view_attachment_by_filename(doc_id, filename): return view_attachment(attachment) +def view_attachment_as_pdf(attachment): + suffix = os.path.splitext(attachment.filename)[1].lstrip('.') + pdf = soffice_convert(attachment.file, suffix, 'pdf')[0] + file_bytes = pdf.read() + response = HttpResponse(file_bytes, content_type="application/pdf") + response['Content-Disposition'] = 'inline; filename=%s' % attachment.filename + response['Content-Length'] = str(len(file_bytes)) + return response + + def download_attachment(attachment): response = view_attachment(attachment) response['Content-Disposition'] = 'attachment; filename=%s' % attachment.filename @@ -49,6 +69,8 @@ def download(self, request, *args, **kwargs): @action(detail=True, methods=['GET']) def view(self, request, *args, **kwargs): attachment = self.get_object() + if attachment.mime_type in DOC_MIMETYPES: + return view_attachment_as_pdf(attachment) return view_attachment(attachment) def filter_queryset(self, queryset): diff --git a/indigo_app/static/javascript/indigo/views/document_source_att.js b/indigo_app/static/javascript/indigo/views/document_source_att.js index 08e8032cb6..4c54bd8e75 100644 --- a/indigo_app/static/javascript/indigo/views/document_source_att.js +++ b/indigo_app/static/javascript/indigo/views/document_source_att.js @@ -23,7 +23,19 @@ this.$dropdown = this.$('.source-attachment-list'); this.$toggle = this.$('.source-attachment-toggle'); this.iframe = document.getElementById('source-attachment-iframe'); - this.mime_types = {'application/pdf': true, 'text/html': true}; + this.docx_mimetypes = { + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': true, + 'application/msword': true, + 'application/vnd.oasis.opendocument.text': true, + 'application/rtf' : true, + 'text/rtf': true, + }; + this.pdf_mimetypes = { + 'application/pdf': true, + 'text/html': true, + }; + // combine docx and pdf mimetypes + this.mime_types = Object.assign({}, this.pdf_mimetypes, this.docx_mimetypes); this.chosen = null; // work publication document @@ -61,11 +73,16 @@ choices = this.attachments.filter(function(att) { return self.mime_types[att.get('mime_type')]; }).map(function(att) { + let default_choice; + if (Object.keys(self.docx_mimetypes).includes(att.get('mime_type'))) { + default_choice = true; + } return { 'title': att.get('filename'), - 'url': att.get('view_url'), 'id': att.get('id'), 'group': 'Attachments', + 'url': att.get('view_url'), + default_choice, }; }); if (this.pubdoc) choices.unshift(this.pubdoc); @@ -108,7 +125,8 @@ var show = !$(e.target).hasClass('active'); if (show) { - this.choose(this.chosen || this.choices[0]); + const default_choice = this.choices.find(choice => choice.default_choice) || this.choices[0]; + this.choose(this.chosen || default_choice); } else { this.$view.addClass('d-none'); this.$('.source-attachment-toggle').removeClass('active');