Skip to content

Commit a77fa2f

Browse files
committed
feat: 防止下载的文件在清空缓存时被清除
1 parent e2d4c43 commit a77fa2f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/screen/cache_manager.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22
import 'dart:io';
33

44
import 'package:alist/l10n/intl_keys.dart';
5+
import 'package:alist/util/download/download_manager.dart';
56
import 'package:alist/widget/alist_scaffold.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
@@ -60,6 +61,7 @@ class CacheManagerController extends GetxController {
6061

6162
final Set<String> _imageCachePaths = {};
6263
final Set<String> _audioCachePaths = {};
64+
String _downloadDir = "";
6365

6466
@override
6567
void onInit() {
@@ -69,6 +71,7 @@ class CacheManagerController extends GetxController {
6971

7072
void _calculateCacheFilesSize() async {
7173
var temporaryDirectory = await getTemporaryDirectory();
74+
_downloadDir = (await DownloadManager.acquireDownloadDirectory()).path;
7275
if (isClosed) {
7376
return;
7477
}
@@ -100,6 +103,8 @@ class CacheManagerController extends GetxController {
100103
} else if (_checkIsAudioPath(path)) {
101104
_audioCacheSize += filesSize;
102105
audioCacheSizeStr.value = _formatBytes(_audioCacheSize);
106+
} else if (path.startsWith(_downloadDir)) {
107+
// do nothing
103108
} else {
104109
_otherCacheSize += filesSize;
105110
debugPrint(entity.path);
@@ -214,6 +219,7 @@ class CacheManagerController extends GetxController {
214219
var excludePaths = <String>[];
215220
excludePaths.addAll(_imageCachePaths);
216221
excludePaths.addAll(_audioCachePaths);
222+
excludePaths.add(_downloadDir);
217223
await _deleteFilesByDirectory(temporaryDirectory,
218224
excludePaths: excludePaths);
219225

lib/util/download/download_manager.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ class DownloadManager {
330330
if (dirs != null) {
331331
downloadDir = dirs;
332332
} else {
333-
downloadDir = await getTemporaryDirectory();
333+
downloadDir = await acquireDownloadDirectory();
334334
}
335335
} else {
336-
downloadDir = await getTemporaryDirectory();
336+
downloadDir = await acquireDownloadDirectory();
337337
}
338338
String subPath = userController.user().baseUrl.md5String();
339339
final username = userController.user().username;
@@ -346,6 +346,13 @@ class DownloadManager {
346346
return downloadDir;
347347
}
348348

349+
static Future<Directory> acquireDownloadDirectory() async {
350+
Directory downloadDir = await getTemporaryDirectory();
351+
downloadDir = Directory("${downloadDir.path}/Downloads");
352+
await downloadDir.create();
353+
return downloadDir;
354+
}
355+
349356
DownloadTask? findTaskBySavedPath(String path) {
350357
var task = _runningTasks
351358
.firstWhereOrNull((element) => element.record.localPath == path);

0 commit comments

Comments
 (0)