Skip to content

Commit dd81640

Browse files
authored
Stabilize async uploads (#149)
1 parent cedf2aa commit dd81640

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
for relative paths, and `sqlite:/path.db` instead of `sqlite:////path.db`
1010
for absolute paths.
1111
* Bridge configs do this migration automatically.
12+
* *(client)* Stabilized support for asynchronous uploads.
13+
* `unstable_create_msc` was renamed to `create_mxc`, and the `max_stall_ms`
14+
parameters for downloading were renamed to `timeout_ms`..
1215

1316
## v0.19.16 (2023-05-26)
1417

mautrix/client/api/modules/media_repository.py

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,19 @@ class MediaRepositoryMethods(BaseClientAPI):
4646
downloading content from the media repository and for getting URL previews without leaking
4747
client IPs.
4848
49-
See also: `API reference <https://matrix.org/docs/spec/client_server/r0.4.0.html#id112>`__
50-
51-
There are also methods for supporting `MSC2246
52-
<https://github.com/matrix-org/matrix-spec-proposals/pull/2246>`__ which allows asynchronous
53-
uploads of media.
49+
See also: `API reference <https://spec.matrix.org/v1.7/client-server-api/#content-repository>`__
5450
"""
5551

56-
async def unstable_create_mxc(self) -> MediaCreateResponse:
52+
async def create_mxc(self) -> MediaCreateResponse:
5753
"""
58-
Create a media ID for uploading media to the homeserver. Requires the homeserver to have
59-
`MSC2246 <https://github.com/matrix-org/matrix-spec-proposals/pull/2246>`__ support.
54+
Create a media ID for uploading media to the homeserver.
55+
56+
See also: `API reference <https://spec.matrix.org/unstable/client-server-api/#post_matrixmediav1create>`__
6057
6158
Returns:
62-
MediaCreateResponse Containing the MXC URI that can be used to upload a file to later, as well as an optional upload URL
59+
MediaCreateResponse Containing the MXC URI that can be used to upload a file to later
6360
"""
64-
resp = await self.api.request(Method.POST, MediaPath.unstable["fi.mau.msc2246"].create)
61+
resp = await self.api.request(Method.POST, MediaPath.v1.create)
6562
return MediaCreateResponse.deserialize(resp)
6663

6764
@contextmanager
@@ -87,21 +84,18 @@ async def upload_media(
8784
"""
8885
Upload a file to the content repository.
8986
90-
See also: `API reference <https://spec.matrix.org/v1.2/client-server-api/#post_matrixmediav3upload>`__
87+
See also: `API reference <https://spec.matrix.org/v1.7/client-server-api/#post_matrixmediav3upload>`__
9188
9289
Args:
9390
data: The data to upload.
9491
mime_type: The MIME type to send with the upload request.
9592
filename: The filename to send with the upload request.
9693
size: The file size to send with the upload request.
97-
mxc: An existing MXC URI which doesn't have content yet to upload into. Requires the
98-
homeserver to have MSC2246_ support.
99-
async_upload: Should the media be uploaded in the background (using MSC2246_)?
100-
If ``True``, this will create a MXC URI, start uploading in the background and then
101-
immediately return the created URI. This is mutually exclusive with manually
102-
passing the ``mxc`` parameter.
103-
104-
.. _MSC2246: https://github.com/matrix-org/matrix-spec-proposals/pull/2246
94+
mxc: An existing MXC URI which doesn't have content yet to upload into.
95+
async_upload: Should the media be uploaded in the background?
96+
If ``True``, this will create a MXC URI using :meth:`create_mxc`, start uploading
97+
in the background, and then immediately return the created URI. This is mutually
98+
exclusive with manually passing the ``mxc`` parameter.
10599
106100
Returns:
107101
The MXC URI to the uploaded file.
@@ -128,19 +122,21 @@ async def upload_media(
128122
if async_upload:
129123
if mxc:
130124
raise ValueError("async_upload and mxc can't be provided simultaneously")
131-
create_response = await self.unstable_create_mxc()
125+
create_response = await self.create_mxc()
132126
mxc = create_response.content_uri
133-
upload_url = create_response.upload_url
127+
upload_url = create_response.unstable_upload_url
134128

135129
path = MediaPath.v3.upload
136130
method = Method.POST
137131
if mxc:
138132
server_name, media_id = self.api.parse_mxc_uri(mxc)
139133
if upload_url is None:
140-
path = MediaPath.unstable["fi.mau.msc2246"].upload[server_name][media_id]
134+
path = MediaPath.v3.upload[server_name][media_id]
141135
method = Method.PUT
142136
else:
143-
path = MediaPath.unstable["fi.mau.msc2246"].upload[server_name][media_id].complete
137+
path = (
138+
MediaPath.unstable["com.beeper.msc3870"].upload[server_name][media_id].complete
139+
)
144140

145141
if upload_url is not None:
146142
task = self._upload_to_url(upload_url, path, headers, data, post_upload_query=query)
@@ -168,25 +164,24 @@ async def _try_upload():
168164
except KeyError:
169165
raise MatrixResponseError("`content_uri` not in response.")
170166

171-
async def download_media(self, url: ContentURI, max_stall_ms: int | None = None) -> bytes:
167+
async def download_media(self, url: ContentURI, timeout_ms: int | None = None) -> bytes:
172168
"""
173169
Download a file from the content repository.
174170
175-
See also: `API reference <https://matrix.org/docs/spec/client_server/r0.4.0.html#get-matrix-media-r0-download-servername-mediaid>`__
171+
See also: `API reference <https://spec.matrix.org/v1.7/client-server-api/#get_matrixmediav3downloadservernamemediaid>`__
176172
177173
Args:
178174
url: The MXC URI to download.
179-
max_stall_ms: The maximum number of milliseconds that the client is willing to wait to
180-
start receiving data. Used for MSC2246 Asynchronous Uploads.
175+
timeout_ms: The maximum number of milliseconds that the client is willing to wait to
176+
start receiving data. Used for asynchronous uploads.
181177
182178
Returns:
183179
The raw downloaded data.
184180
"""
185181
url = self.api.get_download_url(url)
186182
query_params: dict[str, Any] = {"allow_redirect": "true"}
187-
if max_stall_ms is not None:
188-
query_params["max_stall_ms"] = max_stall_ms
189-
query_params["fi.mau.msc2246.max_stall_ms"] = max_stall_ms
183+
if timeout_ms is not None:
184+
query_params["timeout_ms"] = timeout_ms
190185
async with self.api.session.get(url, params=query_params) as response:
191186
return await response.read()
192187

@@ -197,12 +192,12 @@ async def download_thumbnail(
197192
height: int | None = None,
198193
resize_method: Literal["crop", "scale"] = None,
199194
allow_remote: bool = True,
200-
max_stall_ms: int | None = None,
195+
timeout_ms: int | None = None,
201196
):
202197
"""
203198
Download a thumbnail for a file in the content repository.
204199
205-
See also: `API reference <https://matrix.org/docs/spec/client_server/r0.4.0.html#get-matrix-media-r0-thumbnail-servername-mediaid>`__
200+
See also: `API reference <https://spec.matrix.org/v1.7/client-server-api/#get_matrixmediav3thumbnailservernamemediaid>`__
206201
207202
Args:
208203
url: The MXC URI to download.
@@ -214,8 +209,8 @@ async def download_thumbnail(
214209
allow_remote: Indicates to the server that it should not attempt to fetch the media if
215210
it is deemed remote. This is to prevent routing loops where the server contacts
216211
itself.
217-
max_stall_ms: The maximum number of milliseconds that the client is willing to wait to
218-
start receiving data. Used for MSC2246 Asynchronous Uploads.
212+
timeout_ms: The maximum number of milliseconds that the client is willing to wait to
213+
start receiving data. Used for asynchronous Uploads.
219214
220215
Returns:
221216
The raw downloaded data.
@@ -230,9 +225,8 @@ async def download_thumbnail(
230225
query_params["method"] = resize_method
231226
if allow_remote is not None:
232227
query_params["allow_remote"] = allow_remote
233-
if max_stall_ms is not None:
234-
query_params["max_stall_ms"] = max_stall_ms
235-
query_params["fi.mau.msc2246.max_stall_ms"] = max_stall_ms
228+
if timeout_ms is not None:
229+
query_params["timeout_ms"] = timeout_ms
236230
async with self.api.session.get(url, params=query_params) as response:
237231
return await response.read()
238232

mautrix/types/media.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ class MediaCreateResponse(SerializableAttrs):
7171

7272
content_uri: ContentURI
7373
unused_expired_at: Optional[int] = None
74-
upload_url: Optional[str] = None
74+
unstable_upload_url: Optional[str] = field(default=None, json="com.beeper.msc3870.upload_url")

0 commit comments

Comments
 (0)