diff options
author | Patrick Cloke <clokep@users.noreply.github.com> | 2021-03-01 12:23:46 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-01 12:23:46 -0500 |
commit | a0bc9d387e7823e64a8c382a10ccc73194494647 (patch) | |
tree | fce43c38aecca7943bb7ee3a14e19b0bd3fb610c /synapse/rest/media/v1/_base.py | |
parent | Allow bytecode again (#9502) (diff) | |
download | synapse-a0bc9d387e7823e64a8c382a10ccc73194494647.tar.xz |
Use the proper Request in type hints. (#9515)
This also pins the Twisted version in the mypy job for CI until proper type hints are fixed throughout Synapse.
Diffstat (limited to 'synapse/rest/media/v1/_base.py')
-rw-r--r-- | synapse/rest/media/v1/_base.py | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/synapse/rest/media/v1/_base.py b/synapse/rest/media/v1/_base.py index 90bbeca679..6366947071 100644 --- a/synapse/rest/media/v1/_base.py +++ b/synapse/rest/media/v1/_base.py @@ -21,7 +21,7 @@ from typing import Awaitable, Dict, Generator, List, Optional, Tuple from twisted.internet.interfaces import IConsumer from twisted.protocols.basic import FileSender -from twisted.web.http import Request +from twisted.web.server import Request from synapse.api.errors import Codes, SynapseError, cs_error from synapse.http.server import finish_request, respond_with_json @@ -49,18 +49,20 @@ TEXT_CONTENT_TYPES = [ def parse_media_id(request: Request) -> Tuple[str, str, Optional[str]]: try: + # The type on postpath seems incorrect in Twisted 21.2.0. + postpath = request.postpath # type: List[bytes] # type: ignore + assert postpath + # This allows users to append e.g. /test.png to the URL. Useful for # clients that parse the URL to see content type. - server_name, media_id = request.postpath[:2] - - if isinstance(server_name, bytes): - server_name = server_name.decode("utf-8") - media_id = media_id.decode("utf8") + server_name_bytes, media_id_bytes = postpath[:2] + server_name = server_name_bytes.decode("utf-8") + media_id = media_id_bytes.decode("utf8") file_name = None - if len(request.postpath) > 2: + if len(postpath) > 2: try: - file_name = urllib.parse.unquote(request.postpath[-1].decode("utf-8")) + file_name = urllib.parse.unquote(postpath[-1].decode("utf-8")) except UnicodeDecodeError: pass return server_name, media_id, file_name |