Added staging file previews support

This commit is contained in:
Roberto Rosario
2011-02-08 17:57:12 -04:00
parent 52843c3e98
commit 79d7932df0
73 changed files with 2279 additions and 16 deletions
@@ -0,0 +1,7 @@
from django.conf import settings
def public_download_url(file, **kwargs):
"""
Directs downloads to a handler at settings.PUBLIC_DOWNLOADS_URL_BASE
"""
return settings.PUBLIC_DOWNLOADS_URL_BASE + file.name
@@ -0,0 +1,30 @@
from django.http import HttpResponse
from django.utils.encoding import smart_str
def prepare_upload(request, url, **kwargs):
"""Directly uploads to the given URL"""
return url, {}
def serve_file(request, file, save_as, content_type, **kwargs):
"""
Serves the file in chunks for efficiency reasons, but the transfer still
goes through Django itself, so it's much worse than using the web server,
but at least it works with all configurations.
"""
response = HttpResponse(ChunkedFile(file), content_type=content_type)
if save_as:
response['Content-Disposition'] = smart_str(u'attachment; filename=%s' % save_as)
if file.size is not None:
response['Content-Length'] = file.size
return response
def public_download_url(file, **kwargs):
"""No public download URL"""
return None
class ChunkedFile(object):
def __init__(self, file):
self.file = file
def __iter__(self):
return self.file.chunks()
@@ -0,0 +1,11 @@
from django.conf import settings
from filetransfers.api import prepare_upload as delegate
def prepare_upload(*args, **kwargs):
"""Delegates uploads to other backends based on private=False or True"""
if kwargs['private']:
kwargs['backend'] = settings.PRIVATE_PREPARE_UPLOAD_BACKEND
else:
kwargs['backend'] = settings.PUBLIC_PREPARE_UPLOAD_BACKEND
return delegate(*args, **kwargs)
@@ -0,0 +1,10 @@
from django.http import HttpResponseRedirect
from django.utils.encoding import smart_str
def serve_file(request, file, **kwargs):
"""Serves files by redirecting to file.url (e.g., useful for Amazon S3)"""
return HttpResponseRedirect(smart_str(file.url))
def public_download_url(file, **kwargs):
"""Directs downloads to file.url (useful for normal file system storage)"""
return file.url
@@ -0,0 +1,12 @@
from django.http import HttpResponse
from django.utils.encoding import smart_str
def serve_file(request, file, save_as, content_type, **kwargs):
"""Lets the web server serve the file using the X-Sendfile extension"""
response = HttpResponse(content_type=content_type)
response['X-Sendfile'] = file.path
if save_as:
response['Content-Disposition'] = smart_str(u'attachment; filename=%s' % save_as)
if file.size is not None:
response['Content-Length'] = file.size
return response