def test_media_storage(self, panstore): server_name = "test" media_cache = panstore.load_media_cache(server_name) assert not media_cache event = self.encrypted_media_event mxc = urlparse(event.url) assert mxc mxc_server = mxc.netloc mxc_path = mxc.path assert not panstore.load_media(server_name, mxc_server, mxc_path) media = MediaInfo(mxc_server, mxc_path, event.key, event.iv, event.hashes) panstore.save_media(server_name, media) media_cache = panstore.load_media_cache(server_name) assert (mxc_server, mxc_path) in media_cache media_info = media_cache[(mxc_server, mxc_path)] assert media_info == media assert media_info == panstore.load_media(server_name, mxc_server, mxc_path)
def store_thumbnail_cb(self, room, event): if not (event.thumbnail_url and event.thumbnail_key and event.thumbnail_iv and event.thumbnail_hashes): return try: mxc = urlparse(event.thumbnail_url) except ValueError: return if mxc is None: return mxc_server = mxc.netloc.strip("/") mxc_path = mxc.path.strip("/") media = MediaInfo( mxc_server, mxc_path, event.thumbnail_key, event.thumbnail_iv, event.thumbnail_hashes, ) self.media_info[(mxc_server, mxc_path)] = media self.pan_store.save_media(self.server_name, media)
async def upload(self, request): file_name = request.query.get("filename", "") content_type = request.headers.get("Content-Type", "application/octet-stream") client = next(iter(self.pan_clients.values())) body = await request.read() try: response, maybe_keys = await client.upload( data_provider=BufferedReader(BytesIO(body)), content_type=content_type, filename=file_name, encrypt=True, filesize=len(body), ) if not isinstance(response, UploadResponse): return web.Response( status=response.transport_response.status, content_type=response.transport_response.content_type, headers=CORS_HEADERS, body=await response.transport_response.read(), ) self.store.save_upload(self.name, response.content_uri, file_name, content_type) mxc = urlparse(response.content_uri) mxc_server = mxc.netloc.strip("/") mxc_path = mxc.path.strip("/") logger.info( f"Adding media info for {mxc_server}/{mxc_path} to the store") media_info = MediaInfo( mxc_server, mxc_path, maybe_keys["key"], maybe_keys["iv"], maybe_keys["hashes"], ) self.store.save_media(self.name, media_info) return web.Response( status=response.transport_response.status, content_type=response.transport_response.content_type, headers=CORS_HEADERS, body=await response.transport_response.read(), ) except ClientConnectionError as e: return web.Response(status=500, text=str(e)) except SendRetryError as e: return web.Response(status=503, text=str(e))
def store_media_cb(self, room, event): try: mxc = urlparse(event.url) except ValueError: return if mxc is None: return mxc_server = mxc.netloc.strip("/") mxc_path = mxc.path.strip("/") logger.info( f"Adding media info for {mxc_server}/{mxc_path} to the store") media = MediaInfo(mxc_server, mxc_path, event.key, event.iv, event.hashes) self.media_info[(mxc_server, mxc_path)] = media self.pan_store.save_media(self.server_name, media)