2020-05-07 11:19:29 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-01-19 15:07:42 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2020-11-17 12:59:34 +01:00
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
|
2022-07-22 11:04:47 +02:00
|
|
|
import 'package:fluffychat/utils/size_string.dart';
|
2020-05-16 08:02:33 +02:00
|
|
|
import 'matrix_file_extension.dart';
|
2020-01-19 15:07:42 +01:00
|
|
|
|
|
|
|
extension LocalizedBody on Event {
|
2022-04-30 17:43:38 +02:00
|
|
|
Future<LoadingDialogResult<MatrixFile?>> _getFile(BuildContext context) =>
|
|
|
|
showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => downloadAndDecryptAttachmentCached(),
|
|
|
|
);
|
|
|
|
|
2021-07-11 17:12:56 +02:00
|
|
|
void saveFile(BuildContext context) async {
|
2022-04-30 17:43:38 +02:00
|
|
|
final matrixFile = await _getFile(context);
|
2021-07-11 17:12:56 +02:00
|
|
|
|
|
|
|
matrixFile.result?.save(context);
|
2020-05-16 08:02:33 +02:00
|
|
|
}
|
|
|
|
|
2022-04-30 17:43:38 +02:00
|
|
|
void shareFile(BuildContext context) async {
|
|
|
|
final matrixFile = await _getFile(context);
|
|
|
|
|
|
|
|
matrixFile.result?.share(context);
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:59:34 +01:00
|
|
|
bool get isAttachmentSmallEnough =>
|
|
|
|
infoMap['size'] is int &&
|
2021-12-03 17:29:32 +01:00
|
|
|
infoMap['size'] < room.client.database!.maxFileSize;
|
2022-04-30 17:43:38 +02:00
|
|
|
|
2020-11-17 12:59:34 +01:00
|
|
|
bool get isThumbnailSmallEnough =>
|
|
|
|
thumbnailInfoMap['size'] is int &&
|
2021-12-03 17:29:32 +01:00
|
|
|
thumbnailInfoMap['size'] < room.client.database!.maxFileSize;
|
2020-11-17 12:59:34 +01:00
|
|
|
|
2020-05-07 11:19:29 +02:00
|
|
|
bool get showThumbnail =>
|
2021-08-08 17:55:00 +02:00
|
|
|
[MessageTypes.Image, MessageTypes.Sticker, MessageTypes.Video]
|
|
|
|
.contains(messageType) &&
|
2020-05-09 16:38:27 +02:00
|
|
|
(kIsWeb ||
|
2020-11-17 12:59:34 +01:00
|
|
|
isAttachmentSmallEnough ||
|
|
|
|
isThumbnailSmallEnough ||
|
2020-09-03 12:58:54 +02:00
|
|
|
(content['url'] is String));
|
2020-05-07 11:19:29 +02:00
|
|
|
|
2022-07-22 11:04:47 +02:00
|
|
|
String? get sizeString => content
|
|
|
|
.tryGetMap<String, dynamic>('info')
|
|
|
|
?.tryGet<int>('size')
|
|
|
|
?.sizeString;
|
2020-11-17 12:59:34 +01:00
|
|
|
|
|
|
|
static final _downloadAndDecryptFutures = <String, Future<MatrixFile>>{};
|
|
|
|
|
|
|
|
Future<bool> isAttachmentCached({bool getThumbnail = false}) async {
|
|
|
|
final mxcUrl = attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail);
|
2021-12-03 17:29:32 +01:00
|
|
|
if (mxcUrl == null) return false;
|
2020-11-17 12:59:34 +01:00
|
|
|
// check if we have it in-memory
|
|
|
|
if (_downloadAndDecryptFutures.containsKey(mxcUrl)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// check if it is stored
|
|
|
|
if (await isAttachmentInLocalStore(getThumbnail: getThumbnail)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// check if the url is cached
|
2021-08-26 19:03:08 +02:00
|
|
|
final url = mxcUrl.getDownloadLink(room.client);
|
2021-04-21 14:19:54 +02:00
|
|
|
final file = await DefaultCacheManager().getFileFromCache(url.toString());
|
2020-11-17 12:59:34 +01:00
|
|
|
return file != null;
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:29:32 +01:00
|
|
|
Future<MatrixFile?> downloadAndDecryptAttachmentCached(
|
2020-11-17 12:59:34 +01:00
|
|
|
{bool getThumbnail = false}) async {
|
2021-08-29 15:18:02 +02:00
|
|
|
final mxcUrl =
|
2022-03-30 11:46:24 +02:00
|
|
|
attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail)?.toString() ??
|
|
|
|
eventId;
|
2021-08-29 15:18:02 +02:00
|
|
|
_downloadAndDecryptFutures[mxcUrl] ??= downloadAndDecryptAttachment(
|
2020-11-17 12:59:34 +01:00
|
|
|
getThumbnail: getThumbnail,
|
2021-04-21 14:19:54 +02:00
|
|
|
downloadCallback: (Uri url) async {
|
|
|
|
final file = await DefaultCacheManager().getSingleFile(url.toString());
|
2020-11-17 12:59:34 +01:00
|
|
|
return await file.readAsBytes();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
final res = await _downloadAndDecryptFutures[mxcUrl];
|
|
|
|
return res;
|
|
|
|
}
|
2020-01-19 15:07:42 +01:00
|
|
|
}
|