getContentOverrideNameAuthed method

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

This will download content from the content repository (same as the previous endpoint) but replaces the target file name with the one provided by the caller.

{{% 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).

fileName A filename to give in the Content-Disposition header.

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> getContentOverrideNameAuthed(
  String serverName,
  String mediaId,
  String fileName, {
  int? timeoutMs,
}) async {
  final requestUri = Uri(
    path:
        '_matrix/client/v1/media/download/${Uri.encodeComponent(serverName)}/${Uri.encodeComponent(mediaId)}/${Uri.encodeComponent(fileName)}',
    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,
  );
}