misc: add support for offsite podcasts
authorFrédéric Péters <fpeters@0d.be>
Sat, 12 Nov 2022 17:33:23 +0000 (18:33 +0100)
committerFrédéric Péters <fpeters@0d.be>
Sat, 12 Nov 2022 17:33:23 +0000 (18:33 +0100)
panikweb/settings.py
panikweb/urls.py
panikweb/views.py

index aa7505b4a6d29cd0e38742d4851fda640ba3001b..c007872e1f881f411ceb302018dd4a39fce363e2 100644 (file)
@@ -266,6 +266,20 @@ HOME_NEWSITEMS_COUNT = 3
 HOME_PODCASTS_COUNT = 3
 USE_AGENDA_ONLY_FIELD = False
 
+# allow to serve from an alternative path if file is not found locally,
+# to be used with nginx, sth like
+#  location /remote_media {
+#    internal;
+#    proxy_pass https://panikdb.radiopanik.org/media/;
+#    proxy_limit_rate 500k;
+#  }
+#  location /local_media {
+#    internal;
+#    alias /srv/www.radiopanik.org/media;
+#  }
+# with OFFSITE_MEDIA_SOUNDS = ('/local_media/', '/remote_media/')
+OFFSITE_MEDIA_SOUNDS = None
+
 PROGRAM_PREFIX = 'programme/'
 EMISSIONS_PREFIX = 'emissions/'
 NEWSITEMS_PREFIX = 'actus/'
index dbed28b464e0d4769fff345048032b83dcc95f0d..73f607a6990381976ef2aa495da6cd3c423bd24a 100644 (file)
@@ -85,6 +85,10 @@ urlpatterns = [
 
 urlpatterns = combo_plugins.register_plugins_urls(urlpatterns)
 urlpatterns += staticfiles_urlpatterns()
+
+if settings.OFFSITE_MEDIA_SOUNDS:
+    urlpatterns += [url(r'^media/(?P<location>sounds/.*)', views.media_hosting)]
+
 urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
 
 if settings.DEBUG_TOOLBAR:
index 92b801a578cef5fcecfd21668bcedd8d829f0cdc..d4b5df9eb2389dc7179ad60d7a312f8422ef24ef 100644 (file)
@@ -9,8 +9,9 @@ from datetime import date, datetime, timedelta
 from django.conf import settings
 from django.contrib.sites.models import Site
 from django.contrib.syndication.views import Feed, add_domain
+from django.core.files.storage import default_storage
 from django.core.paginator import Paginator
-from django.http import Http404, JsonResponse
+from django.http import Http404, HttpResponse, JsonResponse
 from django.urls import reverse
 from django.utils.encoding import force_text, python_2_unicode_compatible
 from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed
@@ -1117,3 +1118,13 @@ class Chat(DetailView, EmissionMixin):
 
 
 chat = cache_control(max_age=15)(Chat.as_view())
+
+
+def media_hosting(request, location, *args, **kwargs):
+    local_path = default_storage.path(location)
+    response = HttpResponse()
+    if os.path.exists(local_path):
+        response['X-Accel-Redirect'] = settings.OFFSITE_MEDIA_SOUNDS[0] + location
+    else:
+        response['X-Accel-Redirect'] = settings.OFFSITE_MEDIA_SOUNDS[1] + location
+    return response