A Django 1.10+/2.x storages backend backed by your existing database.
Project description
django-database-storage-backend
A Django 1.10+/2.x storages backend backed by your existing database.
This module provides an app named dbstorage
. The app contains a single model StoredFile
which is where files stored with the storage backend are put. The app also provides DatabaseStorage
which you can set on your file fields.
Usage
Install libmagic, e.g.:
brew install libmagic
Install this package:
pip django-database-storage-backend
Put 'dbstorage'
in your INSTALLED_APPS
in your settings.py
.
Add to your settings.py
:
DEFAULT_FILE_STORAGE = 'dbstorage.storage.DatabaseStorage'
Add to your urls.py
URLconf:
url(r'^user-media/', include('dbstorage.urls')),
user-media/
determines the URL path where media files are accessed from. You can set this to anything.
Add a File or ImageFile field to your model:
image = models.ImageField(upload_to='some-root')
Files saved into this field will be stored in the database and available at the URL path /user-media/some-root/{HASH}.{EXT}
. HASH
is the SHA-1 hash of the file content. The file extension is replaced with a normalized file extension by auto-detecting the file type so that no file name information besides the file's type is leaked. This also prevents unauthorized users from randomly guessing the URLs to uploaded files (although SHA-1 is generally deprecated for security purposes in favor of SHA-256 --- so don't use this as your only form of authorization logic if it matters).
The upload_to
value cannot be more than 200 characters as the maximum full path length is 255 characters.
Features
- Easily configure a media storage backend that just uses your existing database for storing and serving your media files.
- Filenames don't leak the name of the file that it was uploaded from and are based on a hash of the file's content to prevent unauthorized users from guessing file URLs.
- Secure headers are set to prevent untrusted content from being a cross-site scripting vulnerability.
- The backend supports the
delete
,exists
,listdir
,size
,url
,created_time
, andmodified_time
functions. - Stored files appear in the Django admin.
Dynamic image resizing
When storing images, the view method can automatically resize an image to one of a few pre-defined sizes. To use this feature, you must install pillow
. Then add ?width=
plus xs
(768px), sm
(1024 px), md
(1100px), or lg
(1400px) to the URL when accessing the image. The image returned in the response will have this size as a maximum dimension.
For Project Maintainers
To publish a universal wheel to pypi::
pip3 install twine
rm -rf dist
python3 setup.py bdist_wheel --universal
twine upload dist/*
git tag v1.0.XXX
git push --tags
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for django_database_storage_backend-1.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38b1ccd5ba9768b586d0af868fbbdc8a039d63aae040571ab66e17405a26bd25 |
|
MD5 | eb8309e09a246393ff9ad7a17329018a |
|
BLAKE2b-256 | 8020a59d56433a60d952001af484298b44585396debfd0a2699fe58f9a2807e6 |