@@ -59,6 +59,7 @@ def create_resource_dict(self) -> dict[str, Resource]:
5959 return resources
6060
6161 def _store_media_with_path (self , file_info : FileInfo , expected_path : str ) -> None :
62+ """Store media with given path."""
6263 assert isinstance (self .repo , MediaRepository )
6364 ctx = self .repo .media_storage .store_into_file (file_info )
6465 (f , fname ) = self .get_success (ctx .__aenter__ ())
@@ -70,8 +71,8 @@ def _store_media_with_path(self, file_info: FileInfo, expected_path: str) -> Non
7071
7172 def _create_local_media_with_media_id_path (self , user : str ) -> MXCUri :
7273 """
73- Force creation of a media object at the specific place based on the media id
74- path. This does assert that the file exists where it is expected to be
74+ Force creation of a local media object at the specific place based on the media
75+ id path. This does assert that the file exists where it is expected to be
7576 """
7677 assert isinstance (self .repo , MediaRepository )
7778 media_id = random_string (24 )
@@ -121,6 +122,7 @@ def _create_local_media_with_media_id_path(self, user: str) -> MXCUri:
121122 return MXCUri (server_name = self .hs .config .server .server_name , media_id = media_id )
122123
123124 def _create_local_media_with_sha256_path (self , user : str ) -> MXCUri :
125+ """Creates local media object with sha256 path."""
124126 assert isinstance (self .repo , MediaRepository )
125127 media_id = random_string (24 )
126128 # Curate a specialized FileInfo that includes sha256 data, then file will be
@@ -162,18 +164,17 @@ def _create_local_media_with_sha256_path(self, user: str) -> MXCUri:
162164 media_id = media_id ,
163165 file_id = media_id ,
164166 media_type = "image/png" ,
165- # We purposely do not use the sha256 here, as it directly causes the sha256
166- # path for thumbnails to be populated, and that is not what we are looking
167- # for.
168167 sha256 = SMALL_PNG_SHA256 ,
169168 )
170169 )
171-
172170 return MXCUri (server_name = self .hs .config .server .server_name , media_id = media_id )
173171
174172 def _create_remote_media_with_media_id_path (self , server_name : str ) -> str :
173+ """Creates remote media object with media id path."""
175174 media_id = random_string (24 )
176175 upload_name = "other_server_media"
176+ # Curate a specialized FileInfo that is lacking sha256 data, then file will be
177+ # forced to the old media_id path
177178 file_info = FileInfo (
178179 server_name = server_name ,
179180 file_id = media_id ,
@@ -205,6 +206,7 @@ def _create_remote_media_with_media_id_path(self, server_name: str) -> str:
205206 return media_id
206207
207208 def _create_remote_media_with_sha256_path (self , server_name : str ) -> str :
209+ """Creates remote media object with sha256 path."""
208210 media_id = random_string (24 )
209211 upload_name = "other_server_media"
210212 file_info = FileInfo (
@@ -265,11 +267,12 @@ def test_create_or_update_content_creates_new_content_with_sha256_path(
265267 assert not os .path .exists (old_path )
266268
267269 def test_create_or_update_content_updates_content_with_media_id_path (self ) -> None :
268- """Test that `create_or_update_content` function can update existing media with media_id path"""
269- # Strictly speaking, this is not an operation that is supposed to be supported,
270- # but is currently possible. In the case it becomes necessary, the behavior
271- # should not be unexpected.
272-
270+ """
271+ Test that `create_or_update_content` function can update existing media with
272+ media_id path. Strictly speaking, this is not an operation that is supposed to
273+ be supported, but is currently possible. In the case it becomes necessary, the
274+ behavior should not be unexpected.
275+ """
273276 # Create media with media_id path
274277 mxc_uri = self ._create_local_media_with_media_id_path (self .creator )
275278 media_id = mxc_uri .media_id
@@ -291,11 +294,12 @@ def test_create_or_update_content_updates_content_with_media_id_path(self) -> No
291294 assert os .path .exists (self .repo .filepaths .local_media_filepath (media_id ))
292295
293296 def test_create_or_update_content_updates_content_with_sha256_path (self ) -> None :
294- """Test that `create_or_update_content` function can update existing media with sha256 path"""
295- # Strictly speaking, this is not an operation that is supposed to be supported,
296- # but is currently possible. In the case it becomes necessary, the behavior
297- # should not be unexpected.
298-
297+ """
298+ Test that `create_or_update_content` function can update existing media with
299+ sha256 path. Strictly speaking, this is not an operation that is supposed to be
300+ supported, but is currently possible. In the case it becomes necessary, the
301+ behavior should not be unexpected.
302+ """
299303 # Create media with sha256 path
300304 mxc_uri = self ._create_local_media_with_sha256_path (self .creator )
301305 media_id = mxc_uri .media_id
@@ -392,6 +396,10 @@ def test_get_local_media_with_sha256_path(self) -> None:
392396 self .assertEqual (channel .result ["body" ], SMALL_PNG )
393397
394398 def test_get_remote_media_impl_with_sha256_path_cache_hit (self ) -> None :
399+ """
400+ Test `_get_remote_media_impl` can fetch the media with sha256 path successfully
401+ in case of cache hit.
402+ """
395403 server_name = "other_server.com"
396404 # Generate remote media with sha256 path
397405 media_id = self ._create_remote_media_with_sha256_path (server_name )
@@ -423,7 +431,10 @@ def test_get_remote_media_impl_with_sha256_path_cache_hit(self) -> None:
423431 def test_get_remote_media_impl_with_sha256_path_cache_miss_no_federation (
424432 self ,
425433 ) -> None :
426- """Test that `_get_remote_media_impl` can fetch the media with sha256 path successfully"""
434+ """
435+ Test that `_get_remote_media_impl` can fetch the media with sha256 path
436+ successfully in case of cache miss, without using federation endpoint.
437+ """
427438
428439 # Mock the download media function of the client.
429440 async def _mock_download_media (
@@ -492,7 +503,7 @@ async def _mock_download_media(
492503 assert thumbnail is not None
493504
494505 def test_remove_local_media_from_disk_with_media_id_path (self ) -> None :
495- """Test that `_remove_local_media_from_disk` can remove media with media_id path"""
506+ """Test that `_remove_local_media_from_disk` can remove media with media_id path. """
496507 # Generate 2 media with media_id path
497508 media1_mxc = self ._create_local_media_with_media_id_path (self .creator )
498509 media1_id = media1_mxc .media_id
@@ -509,7 +520,7 @@ def test_remove_local_media_from_disk_with_media_id_path(self) -> None:
509520 assert not os .path .exists (self .repo .filepaths .local_media_filepath (media2_id ))
510521
511522 def test_remove_local_media_from_disk_with_sha256_path (self ) -> None :
512- """Test that `_remove_local_media_from_disk` can remove media with sha256 path"""
523+ """Test that `_remove_local_media_from_disk` can remove media with sha256 path. """
513524 # Generate 2 media with sha256 path with the same image.
514525 # There should be 2 rows in the table and only 1 media stored in filesystem.
515526 media1_mxc = self ._create_local_media_with_sha256_path (self .creator )
0 commit comments