From 02c6ec450007a79a9275f73de3f5c27a1cc5267b Mon Sep 17 00:00:00 2001 From: miruka Date: Mon, 4 Nov 2019 10:29:18 -0400 Subject: [PATCH] Implement Media download --- src/python/media_cache.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/python/media_cache.py b/src/python/media_cache.py index ad0e4cc7..539fbe2e 100644 --- a/src/python/media_cache.py +++ b/src/python/media_cache.py @@ -79,7 +79,17 @@ class Media: async def _get_remote_data(self) -> bytes: - raise NotImplementedError() + parsed = urlparse(self.mxc) + + resp = await self.cache.client.download( + server_name = parsed.netloc, + media_id = parsed.path.lstrip("/"), + ) + + if isinstance(resp, nio.DownloadError): + raise DownloadFailed(resp.message, resp.status_code) + + return resp.body @dataclass @@ -180,5 +190,9 @@ class MediaCache: self.downloads_dir.mkdir(parents=True, exist_ok=True) + async def get_media(self, mxc: str) -> str: + return str(await Media(self, mxc, data=None).get()) + + async def get_thumbnail(self, mxc: str, width: int, height: int) -> str: return str(await Thumbnail(self, mxc, None, (width, height)).get())