From 3d2acc930f0a633bf400c96f3e636b5b662a54cb Mon Sep 17 00:00:00 2001
From: Erik Johnston <erik@matrix.org>
Date: Fri, 19 Feb 2021 10:46:18 +0000
Subject: [PATCH] Return a 404 if we don't have the original file

---
 synapse/rest/media/v1/media_storage.py      | 2 +-
 synapse/rest/media/v1/thumbnail_resource.py | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/synapse/rest/media/v1/media_storage.py b/synapse/rest/media/v1/media_storage.py
index 1057e638be..b1b1c9e6ec 100644
--- a/synapse/rest/media/v1/media_storage.py
+++ b/synapse/rest/media/v1/media_storage.py
@@ -244,7 +244,7 @@ class MediaStorage:
                     await consumer.wait()
                 return local_path
 
-        raise Exception("file could not be found")
+        raise NotFoundError()
 
     def _file_info_to_path(self, file_info: FileInfo) -> str:
         """Converts file_info into a relative path.
diff --git a/synapse/rest/media/v1/thumbnail_resource.py b/synapse/rest/media/v1/thumbnail_resource.py
index c345ec6498..3ab90e9f9b 100644
--- a/synapse/rest/media/v1/thumbnail_resource.py
+++ b/synapse/rest/media/v1/thumbnail_resource.py
@@ -340,6 +340,13 @@ class ThumbnailResource(DirectServeJsonResource):
             # width/height/method so we can just call the "generate exact"
             # methods.
 
+            # First let's check that we do actually have the original image
+            # still. This will throw a 404 if we don't.
+            # TODO: We should refetch the thumbnails for remote media.
+            await self.media_storage.ensure_media_is_in_local_cache(
+                FileInfo(server_name, file_id, url_cache=url_cache)
+            )
+
             if server_name:
                 await self.media_repo.generate_remote_exact_thumbnail(
                     server_name,
-- 
GitLab