Commit 7a6818c0 authored by chris's avatar chris
Browse files

Added view to automatically handle retrieving files from either the

filesystem or database.
parent ae90cca8
......@@ -66,8 +66,23 @@ class File(models.Model):
self._content_hash = utils.get_text_hash(self.content)
return self._content_hash
def dump(self, check_hash=False):
Writes the file content to the filesystem.
if check_hash:
self._content_hash = None
def dump_files(cls, debug=True, verbose=False):
Writes all files to the filesystem.
if debug:
tmp_debug = settings.DEBUG
settings.DEBUG = False
......@@ -4,6 +4,7 @@ import os
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404
from django.views.decorators.cache import cache_control
from django.views.static import serve as django_serve
import mimetypes
......@@ -11,8 +12,25 @@ from database_files.models import File
def serve(request, name):
Retrieves the file from the database.
f = get_object_or_404(File, name=name)
mimetype = mimetypes.guess_type(name)[0] or 'application/octet-stream'
response = HttpResponse(f.content, mimetype=mimetype)
response['Content-Length'] = f.size
return response
def serve_mixed(request, path, document_root):
First attempts to serve the file from the filesystem,
then tries the database.
# First attempt to serve from filesystem.
return django_serve(request, path, document_root)
except Http404:
# Then try serving from database.
return serve(request, path)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment