Files
PiliPlus/lib/utils/cache_manager.dart
My-Responsitories 9d94c72e95 tweaks (#2426)
* opt: danmaku weight

* opt: cache clean

* opt: level img

* opt: play icon

* opt: svg big-vip

* opt: webview ua

* opt: simple dialog

* feat: export vtt

* tweak

* opt: mapIndexed

* feat: more subtitle

* refa: settings page

* feat: codec list options

* drawPath

Signed-off-by: dom <githubaccount56556@proton.me>

* custom dialog option

Signed-off-by: dom <githubaccount56556@proton.me>

* update

Signed-off-by: dom <githubaccount56556@proton.me>

* Revert "drawPath"

This reverts commit e8a4b19f0f.

* opt: _initStreamIndex

* fix: avoid gap

* fix: scale [skip ci]

* fix: hide repost menu not login

* tweaks

Signed-off-by: dom <githubaccount56556@proton.me>

---------

Co-authored-by: dom <githubaccount56556@proton.me>
2026-06-26 10:51:41 +08:00

85 lines
2.5 KiB
Dart

import 'dart:io' show Directory, File;
import 'package:PiliPlus/utils/platform_utils.dart';
import 'package:PiliPlus/utils/storage_pref.dart';
import 'package:cached_network_image_ce/cached_network_image.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
abstract final class CacheManager {
static late final DefaultCacheManager manager;
static Future<void> ensureInitialized() => DefaultCacheManager.init(
maxNrOfCacheLength: Pref.maxCacheSize.toInt(),
).then((i) => manager = i);
// 获取缓存目录
@pragma('vm:notify-debugger-on-exception')
static Future<int> loadApplicationCache() async {
try {
if (PlatformUtils.isDesktop) {
return manager.getTotalLength();
}
final Directory tempDirectory = await getTemporaryDirectory();
if (tempDirectory.existsSync()) {
return await getTotalSizeOfFilesInDir(tempDirectory);
}
} catch (_) {}
return 0;
}
// 循环计算文件的大小
@pragma('vm:notify-debugger-on-exception')
static Future<int> getTotalSizeOfFilesInDir(final Directory file) async {
int total = 0;
await for (final child in file.list(recursive: false)) {
if (child is File) {
total += await child.length();
} else if (child is Directory) {
if (path.equals(child.path, manager.cacheDir)) {
total += manager.getTotalLength();
} else {
await for (final i in child.list(recursive: true)) {
if (i is File) {
total += await i.length();
}
}
}
}
}
return total;
}
// 缓存大小格式转换
static String formatSize(num value) {
const unitArr = ['B', 'K', 'M', 'G', 'T', 'P'];
int index = 0;
while (value >= 1024) {
index++;
value = value / 1024;
}
String size = value.toStringAsFixed(2);
return size + (unitArr.elementAtOrNull(index) ?? '');
}
// 清除 Library/Caches 目录及文件缓存
@pragma('vm:notify-debugger-on-exception')
static Future<void> clearLibraryCache() async {
try {
await manager.emptyCache();
if (PlatformUtils.isDesktop) return;
final tempDirectory = await getTemporaryDirectory();
if (tempDirectory.existsSync()) {
await for (final file in tempDirectory.list(recursive: false)) {
if (file is Directory && path.equals(file.path, manager.cacheDir)) {
continue;
}
await file.delete(recursive: true);
}
}
} catch (_) {}
}
}