Skip to content

Commit 912ebff

Browse files
committed
Fix bug where the thumbnail endpoint was not used for downloading thumbnails (#9)
Closes #8
1 parent c117b52 commit 912ebff

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/matrix_content_scanner/scanner/file_downloader.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ async def download_file(
6060
ContentScannerRestError: The file was not found or could not be downloaded due
6161
to an error on the remote homeserver's side.
6262
"""
63-
url = await self._build_https_url(media_path)
63+
url = await self._build_https_url(
64+
media_path, for_thumbnail=thumbnail_params is not None
65+
)
6466

6567
# Attempt to retrieve the file at the generated URL.
6668
try:
@@ -71,7 +73,11 @@ async def download_file(
7173
# again with an r0 endpoint.
7274
logger.info("File not found, trying legacy r0 path")
7375

74-
url = await self._build_https_url(media_path, endpoint_version="r0")
76+
url = await self._build_https_url(
77+
media_path,
78+
endpoint_version="r0",
79+
for_thumbnail=thumbnail_params is not None,
80+
)
7581

7682
try:
7783
file = await self._get_file_content(url, thumbnail_params)
@@ -89,6 +95,8 @@ async def _build_https_url(
8995
self,
9096
media_path: str,
9197
endpoint_version: str = "v3",
98+
*,
99+
for_thumbnail: bool,
92100
) -> str:
93101
"""Turn a `server_name/media_id` path into an https:// one we can use to fetch
94102
the media.
@@ -100,6 +108,9 @@ async def _build_https_url(
100108
media_path: The media path to translate.
101109
endpoint_version: The version of the download endpoint to use. As of Matrix
102110
v1.1, this is either "v3" or "r0".
111+
for_thumbnail: True if a server-side thumbnail is desired instead of the full
112+
media. In that case, the URL for the `/thumbnail` endpoint is returned
113+
instead of the `/download` endpoint.
103114
104115
Returns:
105116
An https URL to use. If `base_homeserver_url` is set in the config, this
@@ -129,7 +140,9 @@ async def _build_https_url(
129140
# didn't find a .well-known file.
130141
base_url = "https://" + server_name
131142

132-
prefix = self.MEDIA_DOWNLOAD_PREFIX
143+
prefix = (
144+
self.MEDIA_THUMBNAIL_PREFIX if for_thumbnail else self.MEDIA_DOWNLOAD_PREFIX
145+
)
133146

134147
# Build the full URL.
135148
path_prefix = prefix % endpoint_version

tests/scanner/test_file_downloader.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ async def test_download(self) -> None:
6969
self.assertEqual(media.content_type, "image/png")
7070

7171
# Check that we tried downloading from the set base URL.
72-
args = self.get_mock.call_args
73-
self.assertTrue(args[0][0].startswith("http://my-site.com/"))
72+
args = self.get_mock.call_args.args
73+
self.assertTrue(args[0].startswith("http://my-site.com/"))
7474

7575
async def test_no_base_url(self) -> None:
7676
"""Tests that configuring a base homeserver URL means files are downloaded from
@@ -146,7 +146,9 @@ async def test_thumbnail(self) -> None:
146146
MEDIA_PATH, to_thumbnail_params({"height": "50"})
147147
)
148148

149-
query: CIMultiDictProxy[str] = self.get_mock.call_args[1]["query"]
149+
url: str = self.get_mock.call_args.args[0]
150+
query: CIMultiDictProxy[str] = self.get_mock.call_args.kwargs["query"]
151+
self.assertIn("/thumbnail/", url)
150152
self.assertIn("height", query)
151153
self.assertEqual(query.get("height"), "50", query.getall("height"))
152154

0 commit comments

Comments
 (0)