From 29221fb71d80409d031d823be5640eb7ac8fd37d Mon Sep 17 00:00:00 2001
From: Lukas Schauer <lukas@schauer.so>
Date: Fri, 1 May 2020 16:59:01 +0200
Subject: [PATCH] use pretty filenames for downloads instead of naming
 everything by course and video id

---
 jitsimod/templates/jitsi/room.html | 2 +-
 videos/models.py                   | 8 ++++++++
 videos/templates/course.html       | 2 +-
 videos/views.py                    | 3 ++-
 4 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/jitsimod/templates/jitsi/room.html b/jitsimod/templates/jitsi/room.html
index 905fc51..4f458fc 100644
--- a/jitsimod/templates/jitsi/room.html
+++ b/jitsimod/templates/jitsi/room.html
@@ -77,7 +77,7 @@
 			<div class="w3-bar-item w3-right w3-right-align">
 				<span>Uploaded: {{ video.created_at|date:"d.m.Y" }}</span><br/>
 				<form id="video-deleteform-{{ video.id }}" action="?" method="post">{% csrf_token %}<input type="hidden" name="deleterecording" value="{{ video.id }}" /></form>
-				<span><a href="#" onclick='if(confirm("Do you really want to delete this file?")) document.getElementById("video-deleteform-{{ video.id }}").submit();' class='w3-text-red'>Delete</a>{% if video.masterfile %} | <a href="{{ MEDIA_URL }}{{ video.masterfile }}?key={{ video.key }}&timestamp={{ video.created_at|date:'U' }}" download>Download</a>{% endif %}</span>
+				<span><a href="#" onclick='if(confirm("Do you really want to delete this file?")) document.getElementById("video-deleteform-{{ video.id }}").submit();' class='w3-text-red'>Delete</a>{% if video.masterfile %} | <a href="{{ MEDIA_URL }}{{ video.masterfile }}?key={{ video.key }}&timestamp={{ video.created_at|date:'U' }}&filename={{ video.name }}" download>Download</a>{% endif %}</span>
 			</div>
 		</li>
 		{% endfor %}
diff --git a/videos/models.py b/videos/models.py
index 9f54adc..6cf5551 100755
--- a/videos/models.py
+++ b/videos/models.py
@@ -4,6 +4,7 @@ from django.conf import settings
 import secrets
 import datetime
 import os
+import re
 import time
 
 def random_key(i):
@@ -160,6 +161,13 @@ class Video(MetaVideo):
     description = models.TextField(blank=True)
     length = models.DecimalField(null=True, blank=True, decimal_places=3, max_digits=10)
 
+    def clean_filename(self):
+        if isinstance(self, str):
+            toclean = self
+        else:
+            toclean = self.course.title + " - " + (self.name if self.name else str(self.id)) + os.path.splitext(self.masterfile.name)[1]
+        return re.sub(r'[^A-Za-z_0-9,.\- ]', '', toclean).replace(' ', '%20')
+
     def original_upload_path(self, filename):
         extension = filename[::-1].split('.', 1)[0][::-1]
         return os.path.join(self.course.course_path(), 'originals', '{}.{}'.format(int(time.time()), extension))
diff --git a/videos/templates/course.html b/videos/templates/course.html
index 2bedf00..ddc3fbf 100755
--- a/videos/templates/course.html
+++ b/videos/templates/course.html
@@ -44,7 +44,7 @@
 			<div class="w3-bar-item w3-right w3-right-align">
 				<span>Uploaded: {{ video.created_at|date:"d.m.Y" }}</span><br/>
 				{% if is_lecturer %}<form id="video-deleteform-{{ video.id }}" action="/delete" method="post">{% csrf_token %}<input type="hidden" name="type" value="video" /><input type="hidden" name="video" value="{{ video.id }}" /></form>{% endif %}
-				<span>{% if is_lecturer %}<a href="/getlog?video={{ video.id }}" class="w3-text-blue">Log</a> | <a href="#" onclick='if(confirm("Do you really want to delete this file?")) document.getElementById("video-deleteform-{{ video.id }}").submit();' class='w3-text-red'>Delete</a>{% if video.masterfile %} | {% endif %}{% endif %}{% if video.masterfile %}<a href="{{ MEDIA_URL }}{{ video.masterfile }}?key={{ video.key }}&timestamp={{ video.created_at|date:'U' }}" download>Download</a>{% endif %}</span>
+				<span>{% if is_lecturer %}<a href="/getlog?video={{ video.id }}" class="w3-text-blue">Log</a> | <a href="#" onclick='if(confirm("Do you really want to delete this file?")) document.getElementById("video-deleteform-{{ video.id }}").submit();' class='w3-text-red'>Delete</a>{% if video.masterfile %} | {% endif %}{% endif %}{% if video.masterfile %}<a href="{{ MEDIA_URL }}{{ video.masterfile }}?key={{ video.key }}&timestamp={{ video.created_at|date:'U' }}&filename={{ video.clean_filename }}" download>Download</a>{% endif %}</span>
 				{% if is_lecturer %}<br /><span>Views: {{ video.views }}</span>{% endif %}
 			</div>
 		</li>
diff --git a/videos/views.py b/videos/views.py
index 50602df..5ef841a 100755
--- a/videos/views.py
+++ b/videos/views.py
@@ -166,7 +166,8 @@ def cdn(request, path):
     if not hosts:
         raise Exception("No CDN hosts available!")
     host = random.choice(hosts).url.rstrip('/')
-    redir = redirect("{}/{}?key={}&timestamp={}".format(host, path, request.GET.get('key'), request.GET.get('timestamp')))
+    arg_filename = '&filename={}'.format(Video.clean_filename(request.GET.get('filename'))) if (request.GET.get('filename') is not None) else ''
+    redir = redirect("{}/{}?key={}&timestamp={}{}".format(host, path, request.GET.get('key'), request.GET.get('timestamp'), arg_filename))
     if path.endswith(".vtt"):
         redir["Content-Type"] = "text/vtt; charset=utf-8";
     redir["Access-Control-Allow-Origin"] = "https://lectures.fslab.de"
-- 
GitLab