getContentAuthed method

Future<FileResponse> getContentAuthed(
  1. String serverName,
  2. String mediaId, {
  3. int? timeoutMs,
})

{{% boxes/note %}} Clients SHOULD NOT generate or use URLs which supply the access token in the query string. These URLs may be copied by users verbatim and provided in a chat message to another user, disclosing the sender's access token. {{% /boxes/note %}}

Clients MAY be redirected using the 307/308 responses below to download the request object. This is typical when the homeserver uses a Content Delivery Network (CDN).

serverName The server name from the mxc:// URI (the authority component).

mediaId The media ID from the mxc:// URI (the path component).

timeoutMs The maximum number of milliseconds that the client is willing to wait to start receiving data, in the case that the content has not yet been uploaded. The default value is 20000 (20 seconds). The content repository SHOULD impose a maximum value for this parameter. The content repository MAY respond before the timeout.

Implementation

Future<FileResponse> getContentAuthed(
  String serverName,
  String mediaId, {
  int? timeoutMs,
}) async {
  final requestUri = Uri(
    path:
        '_matrix/client/v1/media/download/${Uri.encodeComponent(serverName)}/${Uri.encodeComponent(mediaId)}',
    queryParameters: {
      if (timeoutMs != null) 'timeout_ms': timeoutMs.toString(),
    },
  );
  final request = Request('GET', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  return FileResponse(
    contentType: response.headers['content-type'],
    data: responseBody,
  );
}