From: Christian Heller <c.heller@plomlompom.de>
Date: Tue, 10 Dec 2024 22:38:26 +0000 (+0100)
Subject: In YT result page, don't directly link files, instead link their pages.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/task?a=commitdiff_plain;h=b118c993fa56a537701b0a610abc46890416ace0;p=ytplom

In YT result page, don't directly link files, instead link their pages.
---

diff --git a/src/templates/yt_result.tmpl b/src/templates/yt_result.tmpl
index 6e14dc4..ea81f45 100644
--- a/src/templates/yt_result.tmpl
+++ b/src/templates/yt_result.tmpl
@@ -9,7 +9,7 @@
 <tr><th>duration:</th><td>{{video_data.duration}}</td></tr>
 <tr><th>definition:</th><td>{{video_data.definition}}</td></tr>
 <tr><th>YouTube ID:</th><td>{{video_data.id_}} (<a href="{{youtube_prefix}}{{video_data.id_}}">watch</a>)</td></tr>
-<tr><th>download:</th><td>{% if is_temp %}working on it{% else %}<a href="/{{page_names.download}}/{{video_data.id_}}">{{ file_path if file_path else "please do" }}</a>{% endif %}</td></tr>
+<tr><th>download:</th><td>{% if is_temp %}working on it{% elif file_path %}<a href="/{{page_names.file}}/{{file_digest}}">{{file_path}}</a>{% else %}<a href="/{{page_names.download}}/{{video_data.id_}}">please do</a>{% endif %}</td></tr>
 <tr>
 <th>linked queries:</th>
 <td>
diff --git a/src/ytplom/http.py b/src/ytplom/http.py
index e6b2775..b406868 100644
--- a/src/ytplom/http.py
+++ b/src/ytplom/http.py
@@ -248,14 +248,16 @@ class _TaskHandler(BaseHTTPRequestHandler):
         try:
             with DbConn() as conn:
                 file_data = VideoFile.get_by_yt_id(conn, video_id)
+                if file_data.missing:
+                    raise NotFoundException
+                self._redirect(Path('/')
+                               .joinpath(PAGE_NAMES['file'])
+                               .joinpath(file_data.digest.b64))
         except NotFoundException:
             self.server.downloads.queue_download(video_id)
             self._redirect(Path('/')
                            .joinpath(PAGE_NAMES['yt_result'])
                            .joinpath(video_id))
-            return
-        with file_data.full_path.open('rb') as video_file:
-            self._send_http(content=video_file.read())
 
     def _send_yt_query_page(self, query_id: QueryId) -> None:
         with DbConn() as conn:
@@ -284,15 +286,19 @@ class _TaskHandler(BaseHTTPRequestHandler):
                 video_data = YoutubeVideo.get_one(conn, video_id)
             except NotFoundException:
                 video_data = YoutubeVideo(video_id)
+            file_digest: Optional[str]
+            file_path: Optional[Path]
             try:
                 file = VideoFile.get_by_yt_id(conn, video_id)
-                file_path = file.full_path if file.present else None
+                file_digest = file.digest.b64
+                file_path = file.rel_path if file.present else None
             except NotFoundException:
-                file_path = None
+                file_path, file_digest = None, None
         self._send_rendered_template(
                 _NAME_TEMPLATE_YT_VIDEO,
                 {'video_data': video_data,
                  'is_temp': video_id in self.server.downloads.ids_unfinished,
+                 'file_digest': file_digest,
                  'file_path': file_path,
                  'youtube_prefix': YOUTUBE_URL_PREFIX,
                  'queries': linked_queries})