Anonymous avatar Anonymous committed b3f6589

finalized API

Comments (0)

Files changed (5)

filetransfers/api.py

 from django.utils.importlib import import_module
 import mimetypes
 
-DEFAULT_FILE_UPLOAD_BACKEND = getattr(settings, 'DEFAULT_FILE_UPLOAD_BACKEND',
-                                      __name__ + '.backends.default.prepare_upload')
-DEFAULT_FILE_SERVING_BACKEND = getattr(settings, 'DEFAULT_FILE_SERVING_BACKEND',
-                                       __name + '.backends.default.serve_chunked_file')
+UPLOAD_BACKEND = getattr(settings, 'FILETRANSFERS_UPLOAD_BACKEND',
+                         __name__ + '.backends.default.prepare_upload')
+DOWNLOAD_BACKEND = getattr(settings, 'FILETRANSFERS_DOWNLOAD_BACKEND',
+                           __name__ + '.backends.default.serve_file')
 
-_upload_backends = {}
-_file_serving_backends = {}
+_backends_cache = {}
 
 # Public API
-def prepare_upload(url, backend=None):
+def prepare_upload(request, url, backend=None):
     # TODO: Support specifying maxmium file upload size and other constraints?
     # Probably can't be done for all backends, so maybe the developer should
     # always check these constraints after the upload?
-    handler = _load_backend(backend, DEFAULT_FILE_UPLOAD_BACKEND, _upload_backends)
-    result = handler(url)
+    handler = _load_backend(backend, UPLOAD_BACKEND)
+    result = handler(request, url)
     if isinstance(result, (tuple, list)):
         return result
     return result, {}
 
 def serve_file(request, file, backend=None, save_as=False, content_type=None):
-    """
-    Serves a file to the browser.
-
-    This function provides a common API, so you can write reusable Django apps
-    which don't make any assumptions about the underlying file storage service.
-    For example, files could be stored somewhere in the cloud on a different
-    server or they could be served efficiently via xsendfile.
-
-    Arguments:
-    * request: The current request
-    * file: The Django File object that should be served (e.g. from FileField)
-    * backend: Overrides default backend (settings.DEFAULT_FILE_SERVING_BACKEND)
-    * save_as: Forces browser to open a "Save as..." window. If this is True
-               the file's name will be file.name. Alternatively, pass a string
-               to override the file name. The default is to let the browser
-               decide how to handle the download.
-    * content_type: Overrides the file's content type in the response.
-                    By default the content type will be detected via
-                    mimetypes.guess_type().
-
-    Returns an HttpResponse object that handles the downoad.
-    """
     # Backends are responsible for handling range requests.
-    handler = _load_backend(backend, DEFAULT_FILE_SERVING_BACKEND, _file_serving_backends)
+    handler = _load_backend(backend, DOWNLOAD_BACKEND)
     filename = file.name.rsplit('/')[-1]
     if save_as is True:
         save_as = filename
     return handler(request, file, save_as=save_as, content_type=content_type)
 
 # Internal utilities
-def _load_backend(backend, default_backend, backends_cache):
+def _load_backend(backend, default_backend):
     if backend is None:
         backend = default_backend
-    if backend not in _file_serving_backends:
+    if backend not in _backends_cache:
         module_name, func_name = backend.rsplit('.', 1)
-        backends_cache[backend] = getattr(import_module(module_name), func_name)
-    return backends_cache[backend]
+        _backends_cache[backend] = getattr(import_module(module_name), func_name)
+    return _backends_cache[backend]

filetransfers/backends/default.py

 from django.http import HttpResponse
 from django.utils.encoding import smart_str
 
-def simple_upload_url(url):
+def simple_upload_url(request, url):
     return url
 
-def serve_chunked_file(request, file, save_as, content_type):
+def serve_file(request, file, save_as, content_type):
     response = HttpResponse(ChunkedFile(file), content_type=content_type)
     if save_as:
         response['Content-Disposition'] = smart_str(u'attachment; filename=%s' % save_as)

filetransfers/backends/xsendfile.py

 from django.http import HttpResponse
 from django.utils.encoding import smart_str
 
-def serve_xsendfile(request, file, save_as, content_type):
+def serve_file(request, file, save_as, content_type):
     response = HttpResponse(ChunkedFile(file), content_type=content_type)
     response['X-Sendfile'] = file.path
     if save_as:

filetransfers/templatetags/filetransfers.py

+from django.template import Library
+from django.utils.safestring import mark_safe
+
+register = Library()
+
+_hidden_data_field = '<input type="hidden" name="%s" value="%s" />'
+
+@register.simple_tag
+def render_upload_data(data):
+    inputs = ''.join(_hidden_data_field % item for item in data.items())
+    if inputs:
+        return mark_safe('<div style="display:none">%s</div>' % inputs)
+    return ''

filetransfers/templatetags/storage.py

-from django.template import Library
-from django.utils.safestring import mark_safe
-
-register = Library()
-
-_hidden_data_field = '<input type="hidden" name="%s" value="%s" />'
-
-@register.simple_tag
-def render_upload_data(data):
-    inputs = ''.join(_hidden_data_field % item for item in data.items())
-    if inputs:
-        return mark_safe('<div style="display:none">%s</div>' % inputs)
-    return ''
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.