|
|
|
@ -1,6 +1,6 @@ |
|
|
|
import graphene |
|
|
|
import graphene_django |
|
|
|
from django.http import HttpResponse, HttpResponseGone |
|
|
|
from django.http import Http404, HttpResponse, HttpResponseGone |
|
|
|
from graphql import GraphQLError |
|
|
|
from rest_framework import permissions, status |
|
|
|
from rest_framework.generics import CreateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView |
|
|
|
@ -79,18 +79,37 @@ class DocumentView(RetrieveUpdateDestroyAPIView): |
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs): |
|
|
|
""" Return the file instead of meta data """ |
|
|
|
doc = self.get_object() |
|
|
|
content_type = 'application/pdf' if 'pdf' in doc.filename else 'image/jpeg' |
|
|
|
document = self.get_object() |
|
|
|
content_type = _content_type_from_filename(document.filename) |
|
|
|
|
|
|
|
# If file doesn't exist anymore, delete it |
|
|
|
try: |
|
|
|
file_contents = doc.file.read() |
|
|
|
file_contents = document.file.read() |
|
|
|
except TypeError: |
|
|
|
doc.delete() |
|
|
|
document.delete() |
|
|
|
return HttpResponseGone('File no longer exists') |
|
|
|
return HttpResponse(file_contents, content_type=content_type) |
|
|
|
|
|
|
|
|
|
|
|
def get_document_file_by_key(request, file_key): |
|
|
|
file = Document.get_file(file_key) |
|
|
|
content_type = _content_type_from_filename(file.name) |
|
|
|
try: |
|
|
|
return HttpResponse(file, content_type=content_type) |
|
|
|
except TypeError: |
|
|
|
raise Http404("File not found") |
|
|
|
|
|
|
|
|
|
|
|
def _content_type_from_filename(filename): |
|
|
|
if filename.endswith('pdf'): |
|
|
|
content_type = 'application/pdf' |
|
|
|
elif filename.endswith('png'): |
|
|
|
content_type = 'image/png' |
|
|
|
else: |
|
|
|
content_type = 'image/jpeg' |
|
|
|
return content_type |
|
|
|
|
|
|
|
|
|
|
|
class DocumentType(graphene_django.DjangoObjectType): |
|
|
|
file_url = graphene.String(source='get_file_url') |
|
|
|
|
|
|
|
|