fluffychat/lib/utils/client_download_content_extension.dart
2024-08-25 12:46:15 +02:00

53 lines
1.3 KiB
Dart

import 'dart:typed_data';
import 'package:matrix/matrix.dart';
extension ClientDownloadContentExtension on Client {
Future<Uint8List> downloadMxcCached(
Uri mxc, {
num? width,
num? height,
bool isThumbnail = false,
bool? animated,
ThumbnailMethod? thumbnailMethod,
}) async {
// To stay compatible with previous storeKeys:
final cacheKey = isThumbnail
// ignore: deprecated_member_use
? mxc.getThumbnail(
this,
width: width,
height: height,
animated: animated,
method: thumbnailMethod!,
)
: mxc;
final cachedData = await database?.getFile(cacheKey);
if (cachedData != null) return cachedData;
final httpUri = isThumbnail
? await mxc.getThumbnailUri(
this,
width: width,
height: height,
animated: animated,
method: thumbnailMethod,
)
: await mxc.getDownloadUri(this);
final response = await httpClient.get(
httpUri,
headers:
accessToken == null ? null : {'authorization': 'Bearer $accessToken'},
);
if (response.statusCode != 200) {
throw Exception();
}
final remoteData = response.bodyBytes;
await database?.storeFile(cacheKey, remoteData, 0);
return remoteData;
}
}