Преглед изворни кода

Add support for CDN hosting of media content

Kyle Maas пре 1 година
родитељ
комит
8f61738e35
5 измењених фајлова са 38 додато и 8 уклоњено
  1. 8 0
      cms/settings.py
  2. 9 1
      docs/admins_docs.md
  3. 4 0
      files/context_processors.py
  4. 13 3
      files/models.py
  5. 4 4
      templates/cms/media.html

+ 8 - 0
cms/settings.py

@@ -469,6 +469,14 @@ if "http" not in FRONTEND_HOST:
     # FRONTEND_HOST needs a http:// preffix
     FRONTEND_HOST = f"http://{FRONTEND_HOST}"
 
+try:
+    if (FRONTEND_HOST_MEDIA is not None) and ("http" not in FRONTEND_HOST_MEDIA):
+        # FRONTEND_HOST_MEDIA needs a http:// preffix
+        FRONTEND_HOST_MEDIA = f"http://{FRONTEND_HOST_MEDIA}"
+except NameError:
+    # Ignore it - FRONTEND_HOST_MEDIA is probably not defined
+    pass
+
 if LOCAL_INSTALL:
     SSL_FRONTEND_HOST = FRONTEND_HOST.replace("http", "https")
 else:

+ 9 - 1
docs/admins_docs.md

@@ -443,6 +443,14 @@ ADMINS_NOTIFICATIONS = {
 - Make the portal workflow public, but at the same time set `GLOBAL_LOGIN_REQUIRED = True` so that only logged in users can see content.
 - You can either set `REGISTER_ALLOWED = False` if you want to add members yourself or checkout options on "django-allauth settings" that affects registration in `cms/settings.py`. Eg set the portal invite only, or set email confirmation as mandatory, so that you control who registers.
 
+### 5.24 Configure different URL for media
+
+If you want to use a different base URL for where your media is hosted (for example, if you keep your media directory synchronized to CDN servers), you can specify:
+
+- FRONTEND_HOST_MEDIA: The base URL (e.g. "https://cdn.example.com") where your media should be accessed from
+
+Please note: you will need to ensure that however you're hosting your media files, it adds an Access-Control-Allow-Origin header to allow the main MediaCMS scripts to download from it.
+
 ## 6. Manage pages
 to be written
 
@@ -734,4 +742,4 @@ this will re-create the sprites for videos that the task failed.
 ## 17. Cookie consent code
 On file `templates/components/header.html` you can find a simple cookie consent code. It is commented, so you have to remove the `{% comment %}` and `{% endcomment %}` lines in order to enable it. Or you can replace that part with your own code that handles cookie consent banners.
 
-![Simple Cookie Consent](images/cookie_consent.png)
+![Simple Cookie Consent](images/cookie_consent.png)

+ 4 - 0
files/context_processors.py

@@ -31,4 +31,8 @@ def stuff(request):
     ret["ALLOW_RATINGS_CONFIRMED_EMAIL_ONLY"] = settings.ALLOW_RATINGS_CONFIRMED_EMAIL_ONLY
     ret["VIDEO_PLAYER_FEATURED_VIDEO_ON_INDEX_PAGE"] = settings.VIDEO_PLAYER_FEATURED_VIDEO_ON_INDEX_PAGE
     ret["RSS_URL"] = "/rss"
+    try:
+        ret["FRONTEND_HOST_MEDIA"] = settings.FRONTEND_HOST_MEDIA.rstrip('/')
+    except AttributeError:
+        ret["FRONTEND_HOST_MEDIA"] = None
     return ret

+ 13 - 3
files/models.py

@@ -696,6 +696,12 @@ class Media(models.Model):
         ep = {}
         ep["title"] = encoding.profile.name
         ep["url"] = encoding.media_encoding_url
+        try:
+            if settings.FRONTEND_HOST_MEDIA is not None:
+                ep["url"] = settings.FRONTEND_HOST_MEDIA + encoding.media_encoding_url
+        except:
+            pass
+
         ep["progress"] = encoding.progress
         ep["size"] = encoding.size
         ep["encoding_id"] = encoding.id
@@ -816,23 +822,27 @@ class Media(models.Model):
         """
 
         res = {}
+        prefix = ""
+        if settings.FRONTEND_HOST_MEDIA is not None:
+            prefix = settings.FRONTEND_HOST_MEDIA
+
         if self.hls_file:
             if os.path.exists(self.hls_file):
                 hls_file = self.hls_file
                 p = os.path.dirname(hls_file)
                 m3u8_obj = m3u8.load(hls_file)
                 if os.path.exists(hls_file):
-                    res["master_file"] = helpers.url_from_path(hls_file)
+                    res["master_file"] = prefix + helpers.url_from_path(hls_file)
                     for iframe_playlist in m3u8_obj.iframe_playlists:
                         uri = os.path.join(p, iframe_playlist.uri)
                         if os.path.exists(uri):
                             resolution = iframe_playlist.iframe_stream_info.resolution[1]
-                            res["{}_iframe".format(resolution)] = helpers.url_from_path(uri)
+                            res["{}_iframe".format(resolution)] = prefix + helpers.url_from_path(uri)
                     for playlist in m3u8_obj.playlists:
                         uri = os.path.join(p, playlist.uri)
                         if os.path.exists(uri):
                             resolution = playlist.stream_info.resolution[1]
-                            res["{}_playlist".format(resolution)] = helpers.url_from_path(uri)
+                            res["{}_playlist".format(resolution)] = prefix + helpers.url_from_path(uri)
         return res
 
     @property

+ 4 - 4
templates/cms/media.html

@@ -22,7 +22,7 @@
 
 {% if media_object.media_type == "video" %}
 
-<meta property="og:image" content="{{FRONTEND_HOST}}{{media_object.poster_url}}">
+<meta property="og:image" content="{{FRONTEND_HOST_MEDIA}}{{media_object.poster_url}}">
 
 <meta name="twitter:card" content="summary_large_image">
 
@@ -34,7 +34,7 @@
     "url": "{{FRONTEND_HOST}}{{media_object.get_absolute_url}}",
     "description": "{% if media_object.summary %}{{media_object.summary}}{% else %}{{media_object.description}}{% endif %}",
     "thumbnailUrl": [
-    	"{{FRONTEND_HOST}}{{media_object.poster_url}}"
+    	"{{FRONTEND_HOST_MEDIA}}{{media_object.poster_url}}"
     ],
     "uploadDate": "{{media_object.add_date}}",
     "dateModified": "{{media_object.edit_date}}",
@@ -49,7 +49,7 @@
 
 {% elif media_object.media_type == "audio" %}
 
-<meta property="og:image" content="{{FRONTEND_HOST}}{{media_object.poster_url}}">
+<meta property="og:image" content="{{FRONTEND_HOST_MEDIA}}{{media_object.poster_url}}">
 
 <meta name="twitter:card" content="summary_large_image">
 
@@ -72,7 +72,7 @@
 
 {% elif media_object.media_type == "image" %}
 
-<meta property="og:image" content="{{FRONTEND_HOST}}{{media_object.original_media_url}}">
+<meta property="og:image" content="{{FRONTEND_HOST_MEDIA}}{{media_object.original_media_url}}">
 
 <meta name="twitter:card" content="summary_large_image">