uploadContentToMXC method

Future<Map<String, Object?>> uploadContentToMXC(
  1. String serverName,
  2. String mediaId,
  3. Uint8List body, {
  4. String? filename,
  5. String? contentType,
})

This endpoint permits uploading content to an mxc:// URI that was created earlier via POST /_matrix/media/v1/create.

serverName The server name from the mxc:// URI returned by POST /_matrix/media/v1/create (the authority component).

mediaId The media ID from the mxc:// URI returned by POST /_matrix/media/v1/create (the path component).

filename The name of the file being uploaded

body

contentType Optional. The content type of the file being uploaded.

Clients SHOULD always supply this header.

Defaults to application/octet-stream if it is not set.

Implementation

Future<Map<String, Object?>> uploadContentToMXC(
  String serverName,
  String mediaId,
  Uint8List body, {
  String? filename,
  String? contentType,
}) async {
  final requestUri = Uri(
    path:
        '_matrix/media/v3/upload/${Uri.encodeComponent(serverName)}/${Uri.encodeComponent(mediaId)}',
    queryParameters: {
      if (filename != null) 'filename': filename,
    },
  );
  final request = Request('PUT', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  if (contentType != null) request.headers['content-type'] = contentType;
  request.bodyBytes = body;
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final responseString = utf8.decode(responseBody);
  final json = jsonDecode(responseString);
  return json as Map<String, Object?>;
}