Skip to content

Commit

Permalink
adds docx pdf conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
actlikewill committed Aug 22, 2023
1 parent 0b39820 commit e91e3b2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
24 changes: 23 additions & 1 deletion indigo_api/views/attachments.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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):
Expand Down
24 changes: 21 additions & 3 deletions indigo_app/static/javascript/indigo/views/document_source_att.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit e91e3b2

Please sign in to comment.