Skip to content

Commit

Permalink
Add support for CDN hosting of media content
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMaas committed Sep 1, 2023
1 parent 3d59b87 commit 8f61738
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
8 changes: 8 additions & 0 deletions cms/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,14 @@
# 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:
Expand Down
10 changes: 9 additions & 1 deletion docs/admins_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 changes: 4 additions & 0 deletions files/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 13 additions & 3 deletions files/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,12 @@ def get_encoding_info(self, encoding, full=False):
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
Expand Down Expand Up @@ -816,23 +822,27 @@ def hls_info(self):
"""

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
Expand Down
8 changes: 4 additions & 4 deletions templates/cms/media.html
Original file line number Diff line number Diff line change
Expand Up @@ -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">

Expand All @@ -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}}",
Expand All @@ -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">

Expand All @@ -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">

Expand Down

0 comments on commit 8f61738

Please sign in to comment.