fix: Remove deprecated meomry caching of downloaded files

This commit is contained in:
Christian Pauly 2022-11-21 18:16:46 +01:00
parent b368bf31ce
commit 0c38f6a205
2 changed files with 2 additions and 40 deletions

View File

@ -60,9 +60,7 @@ class AudioPlayerState extends State<AudioPlayerWidget> {
if (status != AudioPlayerStatus.notDownloaded) return; if (status != AudioPlayerStatus.notDownloaded) return;
setState(() => status = AudioPlayerStatus.downloading); setState(() => status = AudioPlayerStatus.downloading);
try { try {
final matrixFile = final matrixFile = await widget.event.downloadAndDecryptAttachment();
await widget.event.downloadAndDecryptAttachmentCached();
if (matrixFile == null) throw ('Download failed');
final tempDir = await getTemporaryDirectory(); final tempDir = await getTemporaryDirectory();
final fileName = Uri.encodeComponent( final fileName = Uri.encodeComponent(
widget.event.attachmentOrThumbnailMxcUrl()!.pathSegments.last); widget.event.attachmentOrThumbnailMxcUrl()!.pathSegments.last);

View File

@ -1,7 +1,6 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart'; import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
@ -12,7 +11,7 @@ extension LocalizedBody on Event {
Future<LoadingDialogResult<MatrixFile?>> _getFile(BuildContext context) => Future<LoadingDialogResult<MatrixFile?>> _getFile(BuildContext context) =>
showFutureLoadingDialog( showFutureLoadingDialog(
context: context, context: context,
future: () => downloadAndDecryptAttachmentCached(), future: downloadAndDecryptAttachment,
); );
void saveFile(BuildContext context) async { void saveFile(BuildContext context) async {
@ -47,39 +46,4 @@ extension LocalizedBody on Event {
.tryGetMap<String, dynamic>('info') .tryGetMap<String, dynamic>('info')
?.tryGet<int>('size') ?.tryGet<int>('size')
?.sizeString; ?.sizeString;
static final _downloadAndDecryptFutures = <String, Future<MatrixFile>>{};
Future<bool> isAttachmentCached({bool getThumbnail = false}) async {
final mxcUrl = attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail);
if (mxcUrl == null) return false;
// 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
final url = mxcUrl.getDownloadLink(room.client);
final file = await DefaultCacheManager().getFileFromCache(url.toString());
return file != null;
}
Future<MatrixFile?> downloadAndDecryptAttachmentCached(
{bool getThumbnail = false}) async {
final mxcUrl =
attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail)?.toString() ??
eventId;
_downloadAndDecryptFutures[mxcUrl] ??= downloadAndDecryptAttachment(
getThumbnail: getThumbnail,
downloadCallback: (Uri url) async {
final file = await DefaultCacheManager().getSingleFile(url.toString());
return await file.readAsBytes();
},
);
final res = await _downloadAndDecryptFutures[mxcUrl];
return res;
}
} }