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 18, 2023
1 parent 12cfc92 commit 43136b1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
11 changes: 10 additions & 1 deletion indigo_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ class AttachmentSerializer(serializers.ModelSerializer):
url = serializers.SerializerMethodField()
download_url = serializers.SerializerMethodField()
view_url = serializers.SerializerMethodField()
pdf_url = serializers.SerializerMethodField()

class Meta:
model = Attachment
fields = (
'id',
'url', 'download_url', 'view_url',
'url', 'download_url', 'view_url', 'pdf_url',
'file',
'filename',
'mime_type',
Expand Down Expand Up @@ -162,6 +163,14 @@ def get_view_url(self, instance):
'pk': instance.pk,
})

def get_pdf_url(self, instance):
if not instance.pk:
return None
return reverse('document-attachments-as-pdf', request=self.context['request'], kwargs={
'document_id': instance.document_id,
'pk': instance.pk,
})

def create(self, validated_data):
file = validated_data.get('file', None)
if not file:
Expand Down
26 changes: 25 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,13 @@
from ..authz import ModelPermissions, RelatedDocumentPermissions
from .documents import DocumentResourceView
from .misc import DEFAULT_PERMS
from docpipe.soffice import soffice_convert
import os

DOCX_MIME_TYPES = [
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/msword',
]


def view_attachment(attachment):
Expand All @@ -30,6 +37,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 @@ -51,6 +68,13 @@ def view(self, request, *args, **kwargs):
attachment = self.get_object()
return view_attachment(attachment)

@action(detail=True, methods=['GET'])
def as_pdf(self, request, *args, **kwargs):
attachment = self.get_object()
if attachment.mime_type in DOCX_MIME_TYPES:
return view_attachment_as_pdf(attachment)
raise Http404()

def filter_queryset(self, queryset):
return queryset.filter(document=self.document).all()

Expand Down
23 changes: 20 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,16 @@
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,
}
this.pdf_mimetypes = {
'application/pdf': true,
'text/html': true,
};
// combine docx and pdf mimetypes
this.mime_types = {...this.pdf_mimetypes, ...this.docx_mimetypes};
this.chosen = null;

// work publication document
Expand Down Expand Up @@ -61,11 +70,18 @@
choices = this.attachments.filter(function(att) {
return self.mime_types[att.get('mime_type')];
}).map(function(att) {
let url = att.get('view_url');
let default_choice;
if (Object.keys(self.docx_mimetypes).includes(att.get('mime_type'))) {
url = att.get('pdf_url');
default_choice = true;
}
return {
'title': att.get('filename'),
'url': att.get('view_url'),
'id': att.get('id'),
'group': 'Attachments',
url,
default_choice,
};
});
if (this.pubdoc) choices.unshift(this.pubdoc);
Expand Down Expand Up @@ -108,7 +124,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 43136b1

Please sign in to comment.