| 1 |
from django.core.files.storage import FileSystemStorage |
| 2 |
from django.core.signing import TimestampSigner |
| 3 |
|
| 4 |
|
| 5 |
class SignedURLMixin: |
| 6 |
""" |
| 7 |
Mixin for generating signed file URLs with compatible storage backends. |
| 8 |
|
| 9 |
Adds X-Signature query parameters to the normal URLs generated by the storage class. |
| 10 |
""" |
| 11 |
|
| 12 |
def url(self, name): |
| 13 |
path = super().url(name) |
| 14 |
signer = TimestampSigner() |
| 15 |
signature = signer.sign(path) |
| 16 |
return "{}?X-Signature={}".format(path, signature) |
| 17 |
|
| 18 |
|
| 19 |
class SignedFileSystemStorage(SignedURLMixin, FileSystemStorage): |
| 20 |
""" |
| 21 |
Specialized filesystem storage that signs file URLs for clients. |
| 22 |
""" |