Skip to main content

A package that includes xsendfile capabilities for Django

Project description

Travis-CI badge Codecov.io badge

Description

django-giftbox is an app for the Django web framework that provides an easy wrapper for X-Sendfile functionality in Apache.

This lets users protect files by not allowing them to be downloaded directly, but allows Django to programmatically send a redirect and let the webserver handle the transaction.

The current implementation is compatible with Django 1.11+ (tested against LTS releases including 2.2) and py2/3 compatible.

The only required dependency is Django itself and mod_xsendfile installed for Apache.

Installation

You can simply download from PyPi using pip:

pip install django-giftbox

To install extra python-magic functionality:

pip install django-giftbox[magic]

You’ll need to have libmagic (or libmagic-dev on many Linux distributions) installed to use this, otherwise python-magic will break.

Or feel free to clone from the master branch:

pip install git+https://github.com/bwhicks/django-giftbox.git@master#egg=giftbox

That’s it.

Configuration

There are two ‘modes’ for giftbox. One of them is dev, and this is the default when running using the Django development server. Giftbox should auto-detect this and run accordingly.

The other is prod, which assumes you are routing your Django appplication through a web server like Apache

In Django settings.py, define a dictionary called GIFTBOX_SETTINGS. You must define doc_root, which is the directory where the files you wish t Usageo serve via Giftbox are located. This can be a relative path under your vhost’s doc root or an absolute file path.

You can also specify these at run time, but you must least have GIFTBOX_SETTINGS with some sane defaults for one of those settings:

GIFTBOX_SETTINGS = {
  'type': 'prod',  # will still detect dev server locally
  'doc_root': '/path/to/protected/files',
}

A corresponding Apache entry in a Vhost or other configuration would be:

XSendFile on
XSendFilePath /path/to/protected/files

The major advantage of this is that you can block regular access to this path or leave it out of Apache’s docroot.

Optional python-magic

If libmagic and python-magic are installed, Giftbox will set the Content-Type header when passing information to your HTTP server. If you don’t want this functionality (serving many files quickly or large ones), you can disable it and your HTTP server’s mime handling will apply:

GIFTBOX_SETTINGS = {
  # other settings...
  'use_magic': False,
}

In a view or view function, create an instance as follows:

from gitfbox import GiftBox

def my_view_func(request):
  box = GiftBox(request)
  return box.send('filename')

box in this case is an instance of GiftBox, which can have its self.kwargs dict modified in any way, as well as having kwargs passed via its constructor. By default it looks to settings.py for its defaults.

box.send() returns an instance of django.httpd.HttpReponse (or FileResponse) with appropriate headers set and Content-Type cleared so that your web server can use its own MIME handling to set the type appropriately (unless you use the optional python-magic functionality). You can manually specify this before returning the HttpResponse object, too.

All of this depends on a correct server setup for Apache that properly creates a protected url that allows sendfile type requests.

The object allows flexible settings of virtually every kwarg at any point. If you need to set the doc_root` dynamically, either when you instantiate the box or when you call Giftbox.send(), you can do that.

Tests

All tests can be run using tox or python setup.py pytest.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-giftbox-0.1.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_giftbox-0.1.1-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-giftbox-0.1.1.tar.gz.

File metadata

  • Download URL: django-giftbox-0.1.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/36.5.0 requests-toolbelt/0.8.0 tqdm/4.19.1.post1 CPython/3.6.8

File hashes

Hashes for django-giftbox-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dd3243e43e1d082d450721f44363bb699f93eadf76423f5aea8cb0cf73afc088
MD5 74141d6de405eb9a400bd94319ccf7dc
BLAKE2b-256 de74f3258fe5827ef9574ba4e8964d37d13379d611273d0868990a1b472b8f40

See more details on using hashes here.

File details

Details for the file django_giftbox-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_giftbox-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/36.5.0 requests-toolbelt/0.8.0 tqdm/4.19.1.post1 CPython/3.6.8

File hashes

Hashes for django_giftbox-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cdd8ea2b8de95eaf26e75251bfe5b303cdd2117b4ccf1db53e56a7a447ee211b
MD5 a3ff9dc3e53071cd85db1cacb6674f39
BLAKE2b-256 fa7bba12fcfa9a8ea3285235881ca62494b3923673ae0f059e5036560cd69d24

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page