@@ -231,9 +231,11 @@ def write_to(r):
231231
232232 def prepare (self , reactor , clock , hs ):
233233
234- self .media_repo = hs .get_media_repository_resource ()
235- self .download_resource = self .media_repo .children [b"download" ]
236- self .thumbnail_resource = self .media_repo .children [b"thumbnail" ]
234+ media_resource = hs .get_media_repository_resource ()
235+ self .download_resource = media_resource .children [b"download" ]
236+ self .thumbnail_resource = media_resource .children [b"thumbnail" ]
237+ self .store = hs .get_datastore ()
238+ self .media_repo = hs .get_media_repository ()
237239
238240 self .media_id = "example.com/12345"
239241
@@ -357,6 +359,67 @@ def test_no_thumbnail_scale(self):
357359 """
358360 self ._test_thumbnail ("scale" , None , False )
359361
362+ def test_thumbnail_repeated_thumbnail (self ):
363+ """Test that fetching the same thumbnail works, and deleting the on disk
364+ thumbnail regenerates it.
365+ """
366+ self ._test_thumbnail (
367+ "scale" , self .test_image .expected_scaled , self .test_image .expected_found
368+ )
369+
370+ if not self .test_image .expected_found :
371+ return
372+
373+ # Fetching again should work, without re-requesting the image from the
374+ # remote.
375+ params = "?width=32&height=32&method=scale"
376+ channel = make_request (
377+ self .reactor ,
378+ FakeSite (self .thumbnail_resource ),
379+ "GET" ,
380+ self .media_id + params ,
381+ shorthand = False ,
382+ await_result = False ,
383+ )
384+ self .pump ()
385+
386+ self .assertEqual (channel .code , 200 )
387+ if self .test_image .expected_scaled :
388+ self .assertEqual (
389+ channel .result ["body" ],
390+ self .test_image .expected_scaled ,
391+ channel .result ["body" ],
392+ )
393+
394+ # Deleting the thumbnail on disk then re-requesting it should work as
395+ # Synapse should regenerate missing thumbnails.
396+ origin , media_id = self .media_id .split ("/" )
397+ info = self .get_success (self .store .get_cached_remote_media (origin , media_id ))
398+ file_id = info ["filesystem_id" ]
399+
400+ thumbnail_dir = self .media_repo .filepaths .remote_media_thumbnail_dir (
401+ origin , file_id
402+ )
403+ shutil .rmtree (thumbnail_dir , ignore_errors = True )
404+
405+ channel = make_request (
406+ self .reactor ,
407+ FakeSite (self .thumbnail_resource ),
408+ "GET" ,
409+ self .media_id + params ,
410+ shorthand = False ,
411+ await_result = False ,
412+ )
413+ self .pump ()
414+
415+ self .assertEqual (channel .code , 200 )
416+ if self .test_image .expected_scaled :
417+ self .assertEqual (
418+ channel .result ["body" ],
419+ self .test_image .expected_scaled ,
420+ channel .result ["body" ],
421+ )
422+
360423 def _test_thumbnail (self , method , expected_body , expected_found ):
361424 params = "?width=32&height=32&method=" + method
362425 channel = make_request (
0 commit comments