forked from Amsterdam/signals
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Protected media | ||
|
||
This app provides the possibility to protect the media folder. To use this functionality in production, specific uWSGI settings are required to use the X-Sendfile header. | ||
|
||
You can run uWSGI as follows: | ||
|
||
```bash | ||
uwsgi \ | ||
--master \ | ||
--http=0.0.0.0:8000 \ | ||
--module=signals.wsgi:application \ | ||
--static-map=/signals/static=./app/static \ | ||
--static-safe=./app/media \ | ||
--plugins=router_static \ | ||
--offload-threads=2 \ | ||
--collect-header="X-Sendfile X_SENDFILE" \ | ||
--response-route-if-not="empty:${X_SENDFILE} static:${X_SENDFILE}" \ | ||
--buffer-size=32768 \ | ||
--py-auto-reload=1 \ | ||
--die-on-term | ||
``` | ||
|
||
The relevant settings are `plugins`, `offload-threads`, `collect-header` and `response-route-if-not`. For more information see the [X-Sendfile emulation snippet of the uWSGI documentation](https://uwsgi-docs.readthedocs.io/en/latest/Snippets.html#x-sendfile-emulation). |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class MediaConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "media" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# Copyright (C) 2024 Delta10 B.V. | ||
from urllib.parse import urljoin | ||
|
||
from django.core import signing | ||
from django.core.files.storage import FileSystemStorage | ||
from django.utils.encoding import filepath_to_uri | ||
|
||
signer = signing.TimestampSigner() | ||
|
||
|
||
class ProtectedFileSystemStorage(FileSystemStorage): | ||
def url(self, name): | ||
if self.base_url is None: | ||
raise ValueError("This file is not accessible via a URL.") | ||
|
||
url = filepath_to_uri(name) | ||
if url is not None: | ||
url = url.lstrip("/") | ||
|
||
signature = signer.sign(url).split(':') | ||
|
||
full_path = urljoin(self.base_url, url) | ||
return full_path + f'?t={signature[1]}&s={signature[2]}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# Copyright (C) 2024 Delta10 B.V. | ||
from django.urls import re_path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
re_path(r"^(?P<path>.*)$", views.download_file, name='download_file'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# Copyright (C) 2024 Delta10 B.V. | ||
from datetime import timedelta | ||
import mimetypes | ||
import os | ||
|
||
from django.conf import settings | ||
from django.core import signing | ||
from django.contrib.staticfiles.views import serve | ||
from django.http import HttpResponse | ||
from django.views.static import serve | ||
|
||
signer = signing.TimestampSigner() | ||
|
||
def download_file(request, path): | ||
t = request.GET.get('t') | ||
s = request.GET.get('s') | ||
|
||
if not t or not s: | ||
return HttpResponse('No signature provided', status=401) | ||
|
||
try: | ||
signer.unsign(f'{path}:{t}:{s}', max_age=timedelta(hours=1)) | ||
except signing.SignatureExpired: | ||
return HttpResponse('Signature expired', status=401) | ||
except signing.BadSignature: | ||
return HttpResponse('Bad signature', status=401) | ||
|
||
if settings.DEBUG: | ||
response = serve(request, path, document_root=settings.MEDIA_ROOT, show_indexes=False) | ||
else: | ||
mimetype, encoding = mimetypes.guess_type(path) | ||
|
||
response = HttpResponse() | ||
|
||
if mimetype: | ||
response["Content-Type"] = mimetype | ||
if encoding: | ||
response["Content-Encoding"] = encoding | ||
|
||
response["X-Sendfile"] = os.path.join( | ||
settings.MEDIA_ROOT, path | ||
).encode("utf8") | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters