uploadContent method
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.
returns content_uri
:
The mxc://
URI to the uploaded content.
Implementation
Future<Uri> uploadContent(
Uint8List body, {
String? filename,
String? contentType,
}) async {
final requestUri = Uri(
path: '_matrix/media/v3/upload',
queryParameters: {
if (filename != null) 'filename': filename,
},
);
final request = Request('POST', 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['content_uri'] as String).startsWith('mxc://')
? Uri.parse(json['content_uri'] as String)
: throw Exception('Uri not an mxc URI'));
}