django-downloadview/django_downloadview/storage.py
2020-01-13 11:25:40 +02:00

22 lines
674 B
Python

from django.core.files.storage import FileSystemStorage
from django.core.signing import TimestampSigner
class SignedURLMixin:
"""
Mixin for generating signed file URLs with compatible storage backends.
Adds X-Signature query parameters to the normal URLs generated by the storage class.
"""
def url(self, name):
path = super(SignedURLMixin, self).url(name)
signer = TimestampSigner()
signature = signer.sign(path)
return "{}?X-Signature={}".format(path, signature)
class SignedFileSystemStorage(SignedURLMixin, FileSystemStorage):
"""
Specialized filesystem storage that signs file URLs for clients.
"""