getContentThumbnailAuthed method
Download a thumbnail of content from the content repository. See the Thumbnails section for more information.
{{% 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).
width
The desired width of the thumbnail. The actual thumbnail may be
larger than the size specified.
height
The desired height of the thumbnail. The actual thumbnail may be
larger than the size specified.
method
The desired resizing method. See the Thumbnails
section for more information.
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.
animated
Indicates preference for an animated thumbnail from the server, if possible. Animated
thumbnails typically use the content types image/gif
, image/png
(with APNG format),
image/apng
, and image/webp
instead of the common static image/png
or image/jpeg
content types.
When true
, the server SHOULD return an animated thumbnail if possible and supported.
When false
, the server MUST NOT return an animated thumbnail. For example, returning a
static image/png
or image/jpeg
thumbnail. When not provided, the server SHOULD NOT
return an animated thumbnail.
Servers SHOULD prefer to return image/webp
thumbnails when supporting animation.
When true
and the media cannot be animated, such as in the case of a JPEG or PDF, the
server SHOULD behave as though animated
is false
.
Implementation
Future<FileResponse> getContentThumbnailAuthed(
String serverName,
String mediaId,
int width,
int height, {
Method? method,
int? timeoutMs,
bool? animated,
}) async {
final requestUri = Uri(
path:
'_matrix/client/v1/media/thumbnail/${Uri.encodeComponent(serverName)}/${Uri.encodeComponent(mediaId)}',
queryParameters: {
'width': width.toString(),
'height': height.toString(),
if (method != null) 'method': method.name,
if (timeoutMs != null) 'timeout_ms': timeoutMs.toString(),
if (animated != null) 'animated': animated.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,
);
}