Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit c2c5942

Browse files
committed
Move logic to download media to TransportLayerClient.
1 parent df36696 commit c2c5942

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

synapse/federation/transport/client.py

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import (
1919
TYPE_CHECKING,
2020
Any,
21+
BinaryIO,
2122
Callable,
2223
Collection,
2324
Dict,
@@ -804,6 +805,30 @@ async def get_account_status(
804805
destination=destination, path=path, data={"user_ids": user_ids}
805806
)
806807

808+
async def download_media_r0(
809+
self,
810+
destination: str,
811+
media_id: str,
812+
output_stream: BinaryIO,
813+
max_size: int,
814+
max_timeout_ms: int,
815+
) -> Tuple[int, Dict[bytes, List[bytes]]]:
816+
path = f"/_matrix/media/r0/download/{destination}/{media_id}"
817+
818+
return await self.client.get_file(
819+
destination,
820+
path,
821+
output_stream=output_stream,
822+
max_size=max_size,
823+
args={
824+
# tell the remote server to 404 if it doesn't
825+
# recognise the server_name, to make sure we don't
826+
# end up with a routing loop.
827+
"allow_remote": "false",
828+
"timeout_ms": str(max_timeout_ms),
829+
},
830+
)
831+
807832

808833
def _create_path(federation_prefix: str, path: str, *args: str) -> str:
809834
"""

synapse/media/media_repository.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class MediaRepository:
7777
def __init__(self, hs: "HomeServer"):
7878
self.hs = hs
7979
self.auth = hs.get_auth()
80-
self.client = hs.get_federation_http_client()
80+
self.client = hs.get_federation_transport_client()
8181
self.clock = hs.get_clock()
8282
self.server_name = hs.hostname
8383
self.store = hs.get_datastores().main
@@ -644,22 +644,13 @@ async def _download_remote_file(
644644
file_info = FileInfo(server_name=server_name, file_id=file_id)
645645

646646
with self.media_storage.store_into_file(file_info) as (f, fname, finish):
647-
request_path = "/".join(
648-
("/_matrix/media/r0/download", server_name, media_id)
649-
)
650647
try:
651-
length, headers = await self.client.get_file(
648+
length, headers = await self.client.download_media_r0(
652649
server_name,
653-
request_path,
650+
media_id,
654651
output_stream=f,
655652
max_size=self.max_upload_size,
656-
args={
657-
# tell the remote server to 404 if it doesn't
658-
# recognise the server_name, to make sure we don't
659-
# end up with a routing loop.
660-
"allow_remote": "false",
661-
"timeout_ms": str(max_timeout_ms),
662-
},
653+
max_timeout_ms=max_timeout_ms,
663654
)
664655
except RequestSendFailed as e:
665656
logger.warning(

0 commit comments

Comments
 (0)