mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-06-08 20:14:51 +08:00
77 lines
2.2 KiB
Dart
77 lines
2.2 KiB
Dart
import 'dart:io' show Directory, File;
|
|
|
|
import 'package:PiliPlus/utils/platform_utils.dart';
|
|
import 'package:PiliPlus/utils/storage_pref.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
abstract final class CacheManager {
|
|
// 获取缓存目录
|
|
@pragma('vm:notify-debugger-on-exception')
|
|
static Future<int> loadApplicationCache([
|
|
final num maxSize = double.infinity,
|
|
]) async {
|
|
try {
|
|
final Directory tempDirectory = await getTemporaryDirectory();
|
|
if (PlatformUtils.isDesktop) {
|
|
final dir = Directory('${tempDirectory.path}/libCachedImageData');
|
|
if (dir.existsSync()) {
|
|
return await getTotalSizeOfFilesInDir(dir, maxSize);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
if (tempDirectory.existsSync()) {
|
|
return await getTotalSizeOfFilesInDir(tempDirectory, maxSize);
|
|
}
|
|
} catch (_) {}
|
|
return 0;
|
|
}
|
|
|
|
// 循环计算文件的大小
|
|
@pragma('vm:notify-debugger-on-exception')
|
|
static Future<int> getTotalSizeOfFilesInDir(
|
|
final Directory file, [
|
|
final num maxSize = double.infinity,
|
|
]) async {
|
|
final children = file.list(recursive: true);
|
|
int total = 0;
|
|
await for (final child in children) {
|
|
if (child is File) {
|
|
total += await child.length();
|
|
if (total >= maxSize) break;
|
|
}
|
|
}
|
|
return total;
|
|
}
|
|
|
|
// 清除 Library/Caches 目录及文件缓存
|
|
static Future<void> clearLibraryCache() async {
|
|
try {
|
|
final Directory tempDirectory = await getTemporaryDirectory();
|
|
if (PlatformUtils.isDesktop) {
|
|
final dir = Directory('${tempDirectory.path}/libCachedImageData');
|
|
if (dir.existsSync()) {
|
|
await dir.delete(recursive: true);
|
|
}
|
|
return;
|
|
}
|
|
if (tempDirectory.existsSync()) {
|
|
final children = tempDirectory.list(recursive: false);
|
|
await for (final file in children) {
|
|
await file.delete(recursive: true);
|
|
}
|
|
}
|
|
} catch (_) {}
|
|
}
|
|
|
|
static Future<void> autoClearCache() async {
|
|
final maxCacheSize = Pref.maxCacheSize;
|
|
if (maxCacheSize != 0) {
|
|
final currCache = await loadApplicationCache(maxCacheSize);
|
|
if (currCache >= maxCacheSize) {
|
|
await clearLibraryCache();
|
|
}
|
|
}
|
|
}
|
|
}
|