diff options
author | Patrick Cloke <clokep@users.noreply.github.com> | 2020-07-24 09:39:02 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-24 09:39:02 -0400 |
commit | 5ea29d7f850b6d2acbbfaf2e81bc5f0625411320 (patch) | |
tree | 02322e5d5fefaab399b4277d77009f119bae8d90 /synapse/rest/media/v1/_base.py | |
parent | Return an empty body for OPTIONS requests. (#7886) (diff) | |
download | synapse-5ea29d7f850b6d2acbbfaf2e81bc5f0625411320.tar.xz |
Convert more of the media code to async/await (#7873)
Diffstat (limited to 'synapse/rest/media/v1/_base.py')
-rw-r--r-- | synapse/rest/media/v1/_base.py | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/synapse/rest/media/v1/_base.py b/synapse/rest/media/v1/_base.py index 595849f9d5..9a847130c0 100644 --- a/synapse/rest/media/v1/_base.py +++ b/synapse/rest/media/v1/_base.py @@ -18,7 +18,6 @@ import logging import os import urllib -from twisted.internet import defer from twisted.protocols.basic import FileSender from synapse.api.errors import Codes, SynapseError, cs_error @@ -77,8 +76,9 @@ def respond_404(request): ) -@defer.inlineCallbacks -def respond_with_file(request, media_type, file_path, file_size=None, upload_name=None): +async def respond_with_file( + request, media_type, file_path, file_size=None, upload_name=None +): logger.debug("Responding with %r", file_path) if os.path.isfile(file_path): @@ -89,7 +89,7 @@ def respond_with_file(request, media_type, file_path, file_size=None, upload_nam add_file_headers(request, media_type, file_size, upload_name) with open(file_path, "rb") as f: - yield make_deferred_yieldable(FileSender().beginFileTransfer(f, request)) + await make_deferred_yieldable(FileSender().beginFileTransfer(f, request)) finish_request(request) else: @@ -198,8 +198,9 @@ def _can_encode_filename_as_token(x): return True -@defer.inlineCallbacks -def respond_with_responder(request, responder, media_type, file_size, upload_name=None): +async def respond_with_responder( + request, responder, media_type, file_size, upload_name=None +): """Responds to the request with given responder. If responder is None then returns 404. @@ -218,7 +219,7 @@ def respond_with_responder(request, responder, media_type, file_size, upload_nam add_file_headers(request, media_type, file_size, upload_name) try: with responder: - yield responder.write_to_consumer(request) + await responder.write_to_consumer(request) except Exception as e: # The majority of the time this will be due to the client having gone # away. Unfortunately, Twisted simply throws a generic exception at us |