Skip to main content

Django Time-Token File Storage

This is a simple extension to Django’s FileSystemStorage that adds a URL parameter carrying a shared token, which is only valid for a defined period of time.

Additionally, a like-wise time-constrained file upload slot mechanism is available.

Functionality

File storage with token-secured URLs

This is a drop-in replacement for the Django FileSystemStorage, usable if media files are served by Django itself. It does currently not work if media files are served from an independent web server.

The storage and its accompanying view do the following:

  • When a URL to a storage file is generated, a HMAC-based token is generated

  • The token and the timestamp when it was generated are appended as request parameters to the URL

  • Upon retrieval of the file through the accompanying view, the requested file name and the passed timestamp are used to recalculate the HMAC-based token

  • Only if the tokens match, and a configured timeout has not passed, is the file served

The signature-based token ensures that the token is invalidated when:

  • The filename changes

  • The timestamp changes

  • The mtime of the file changes

  • The SECRET_KEY changes

Time-constrained uplaod slot

The upload slot mechanism can be used to generically handle file uploads by clients that can not upload files with a regular request. One example for this is a client using GraphQL to talk to Django, because GraphQL does not support file uploads and rather suggests to do uplaods out-of-band.

With Titofisto’s upload slot mechanism, calling code can request a secure URL which it can hand out to a client. When the client POSTs its file to the endpoint, a previously provided callback is called to handle the uploaded file.

Installation

To add django-titofisto to a project, first add it as dependency to your project, e.g. using poetry:

$ poetry add django-titofisto

django-titofisto will use the base FileSystemStorage for almost everything, including determining the MEDIA_ROOT. It merely adds a token as URL parameter to whatever the base FileSystemStorage.url() method returns.

Add the following to your settings:

DEFAULT_FILE_STORAGE = "titofisto.TitofistoStorage"
TITOFISTO_TIMEOUT = 3600  # optional, this is the default
TITOFISTO_PARAM = "titofisto_token"  # optional, this is the default

Add the following to your URL config:

from django.conf import settings
from django.urls import include, path

urlpatterns += [
    path(settings.MEDIA_URL.removeprefix("/"), include("titofisto.urls")),
]

Django will start serving media files under the configured MEDIA_URL.

Provide public media files

Sometimes, there might be media files, for example favicons, you want to be accessible without any authentication. Per default, django-titofisto will serve all files stored in the directory public without a token. You can disable or configure this behavior using these settings:

TITOFISTO_USE_PUBLIC_NAMESPACE = True # optional, this is the default
TITOFISTO_PUBLIC_NAMESPACE = "public/" # optional, this is the default

Use the time-constrained upload slot

To enable the file upload mechanism, a setting must be set, because the default upload/ prefix could potentially shadow expected media file URLs:

TITOFISTO_ENABLE_UPLOAD = True
TITOFISTO_UPLOAD_NAMESPACE = "titofisto/upload/"

A simplified example for using the upload slot mechanism is:

from titofisto.views import TitofistoUploadView

def handle_file(request, pk):
    instance = MyModel.objects.get(pk=pk)
    instance.photo = request.FILES["photo"]
    instance.save()

# This gets an uplaod URL to pass on to the client
# On upload, the handler above will be called, with 15 as positional argument
upload_url = TitofistoUploadView.get_upload_slot(
    "mymodule.handle_file",
    (15,)
)

Tests

To run the tests, execute:

DJANGO_SETTINGS_MODULE=titofisto.test_settings poetry run django-admin test

Credits

django-titofisto was developed for the AlekSIS school information system by its team:

Copyright © 2021, 2023 Dominik George <dominik.george@teckids.org>
Copyright © 2021 Jonathan Weth <dev@jonathanweth.de>

Metadata

Release files for django-titofisto 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-titofisto 1.2.0
File Size Uploaded
django_titofisto-1.2.0.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-titofisto 1.2.0
File Interpreter ABI Platform
django_titofisto-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.2 kB

Release files / django_titofisto-1.2.0.tar.gz

Download URL django_titofisto-1.2.0.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
40f0c1d934a86f7bdf43275700be3564cddeffb0b7071fb1dea5e8a4fe60ef57
BLAKE2b-256 checksum
How to use checksums
29f621e788cf8b5479bfc951f126e1a10d3c981dee5b6dc77d465b94b35320ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.13.12 Linux/6.19.6+deb14+1-amd64

Release files / django_titofisto-1.2.0-py3-none-any.whl

Download URL django_titofisto-1.2.0-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f56f0895a28a34144785f6b18716a603823dbdc458e77e33424e51bfc5c226a6
BLAKE2b-256 checksum
How to use checksums
3bf9eaf83d67d32407c91d7b37a4a811a441985e83d0761e9c002b2d3d59582f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.13.12 Linux/6.19.6+deb14+1-amd64

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2.2

1 release file

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page