Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions matrix_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down