uploadContent method
filename
The name of the file being uploaded
content
The content to be uploaded.
contentType
The content type of the file being uploaded
returns content_uri
:
The MXC URI to the uploaded content.
Implementation
Future<Uri> uploadContent(Uint8List content,
{String? filename, String? contentType}) async {
final requestUri = Uri(path: '_api/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 = content;
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 Uri.parse(json['content_uri'] as String);
}