From 1a6a9fbbe6a9137e38509f8859d313dfa49d16ee Mon Sep 17 00:00:00 2001 From: Denis Ryabyy Date: Thu, 23 Aug 2018 10:23:19 +0300 Subject: [PATCH] Add support for filename when uploading media Signed-off-by: Denis Ryabyy --- matrix_client/api.py | 9 +++++++-- matrix_client/client.py | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 718332fc..5011a4ab 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -744,12 +744,17 @@ def _send(self, method, path, content=None, query_params=None, headers=None, return response.json() - def media_upload(self, content, content_type): + def media_upload(self, content, content_type, filename=None): + query_params = {} + if filename is not None: + query_params['filename'] = filename + return self._send( "POST", "", content=content, headers={"Content-Type": content_type}, - api_path="/_matrix/media/r0/upload" + api_path="/_matrix/media/r0/upload", + query_params=query_params ) def get_display_name(self, user_id): diff --git a/matrix_client/client.py b/matrix_client/client.py index 70f78e49..cb962df5 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -538,19 +538,20 @@ def stop_listener_thread(self): self.sync_thread = None # TODO: move to User class. Consider creating lightweight Media class. - def upload(self, content, content_type): + def upload(self, content, content_type, filename=None): """ Upload content to the home server and recieve a MXC url. Args: content (bytes): The data of the content. content_type (str): The mimetype of the content. + filename (str): Optional. Filename of the content. Raises: MatrixUnexpectedResponse: If the homeserver gave a strange response MatrixRequestError: If the upload failed for some reason. """ try: - response = self.api.media_upload(content, content_type) + response = self.api.media_upload(content, content_type, filename) if "content_uri" in response: return response["content_uri"] else: