django-dbfilestorage/dbfilestorage/views.py
Tyrel Souza 09d721df0d separate filename and filehash (#21)
* separate filename and filehash

* rename migration

* test url

* bump version

* update changelog

* test fail url

* update coveragerc

* add coveragerc

* EOFNL

* update covrc

* filename
2017-01-20 14:38:41 -05:00

25 lines
771 B
Python

from django.http import HttpResponse, Http404
from django.db.models import Q
from django.shortcuts import get_object_or_404
from .models import DBFile
def show_file(request, name):
"""
Get the file object referenced by :name:
Render the decoded base64 representation of the file,
applying the content_type (or closest representation)
:return HttpResponse: Rendered file
"""
dbf = DBFile.objects.filter(Q(name=name)|Q(filehash=name))
if dbf.exists():
response = HttpResponse(
dbf[0].b64.decode('base64'),
content_type=dbf[0].content_type)
response['Content-Disposition'] = 'attachment; filename="{}"'.format(
dbf[0].name)
return response
raise Http404