137 lines
4.1 KiB
Dart
137 lines
4.1 KiB
Dart
import 'dart:io';
|
||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||
import 'package:path_provider/path_provider.dart';
|
||
import 'package:path/path.dart' as p;
|
||
|
||
typedef FileCacheManager = MediaCache;
|
||
class MediaCache {
|
||
late CacheManager _cacheManager;
|
||
static FileCacheManager? _instance;
|
||
static String temporaryPath = "";
|
||
static const musicPath = "/music";
|
||
static String soundPath = "";
|
||
final String cacheKey;
|
||
final Duration stalePeriod;
|
||
final int maxNrOfCacheObjects;
|
||
|
||
static String get videoCachePath => "$temporaryPath";
|
||
static String get imageCachePath => "$temporaryPath";
|
||
|
||
MediaCache._internal({
|
||
required this.cacheKey,
|
||
required this.stalePeriod,
|
||
required this.maxNrOfCacheObjects,
|
||
}) {
|
||
_cacheManager = CacheManager(
|
||
Config(
|
||
cacheKey,
|
||
stalePeriod: stalePeriod,
|
||
maxNrOfCacheObjects: maxNrOfCacheObjects,
|
||
),
|
||
);
|
||
}
|
||
|
||
factory MediaCache.getInstance({
|
||
String cacheKey = 'customCache',
|
||
Duration stalePeriod = const Duration(days: 7),
|
||
int maxNrOfCacheObjects = 100,
|
||
}) {
|
||
_instance ??= FileCacheManager._internal(
|
||
cacheKey: cacheKey,
|
||
stalePeriod: stalePeriod,
|
||
maxNrOfCacheObjects: maxNrOfCacheObjects,
|
||
);
|
||
return _instance!;
|
||
}
|
||
|
||
Future<String> getFilePath() async {
|
||
try {
|
||
Directory directory = await getTemporaryDirectory();
|
||
temporaryPath = p.join(directory.path, cacheKey);
|
||
|
||
// 使用递归创建目录,更简洁
|
||
await Directory(temporaryPath).create(recursive: true);
|
||
await Directory("$temporaryPath/sound").create(recursive: true);
|
||
await Directory("$temporaryPath/$musicPath").create(recursive: true);
|
||
|
||
soundPath = "$temporaryPath/sound";
|
||
print('语音文件夹路径: $soundPath');
|
||
return temporaryPath;
|
||
} catch (e) {
|
||
print('目录创建失败: $e');
|
||
rethrow; // 或者返回一个备用路径
|
||
}
|
||
}
|
||
|
||
/// 下载或获取缓存文件
|
||
/// [url] 文件网络地址
|
||
/// [ignoreCache] 是否忽略缓存强制重新下载
|
||
Future<File> getFile({
|
||
required String url,
|
||
bool ignoreCache = false,
|
||
}) async {
|
||
if (ignoreCache) {
|
||
// 强制重新下载
|
||
final fileInfo = await _cacheManager.downloadFile(url);
|
||
return fileInfo.file;
|
||
} else {
|
||
// 优先从缓存获取
|
||
final cacheFile = await _cacheManager.getSingleFile(url);
|
||
return cacheFile;
|
||
}
|
||
}
|
||
|
||
/// 获取缓存文件路径(如果不存在则返回 null)
|
||
Future<String?> getCachedFilePath(String url) async {
|
||
final fileInfo = await _cacheManager.getFileFromCache(url);
|
||
return fileInfo?.file.path;
|
||
}
|
||
|
||
/// 清除指定文件的缓存
|
||
Future<void> removeCachedFile(String url) async {
|
||
await _cacheManager.removeFile(url);
|
||
}
|
||
|
||
/// 清空所有缓存
|
||
Future<void> clearAllCache() async {
|
||
await _cacheManager.emptyCache();
|
||
}
|
||
|
||
/// 获取缓存大小(字节)
|
||
Future<int> getCacheSize() async {
|
||
final directory = await getTemporaryDirectory();
|
||
final cacheDir = Directory('${directory.path}/libCachedData');
|
||
|
||
if (!await cacheDir.exists()) return 0;
|
||
|
||
int totalSize = 0;
|
||
await for (var file in cacheDir.list(recursive: true)) {
|
||
if (file is File) {
|
||
totalSize += await file.length();
|
||
}
|
||
}
|
||
return totalSize;
|
||
}
|
||
|
||
Future<void> putFileFromFile(String cacheKey, File localFile) async {
|
||
final cacheManager = DefaultCacheManager();
|
||
|
||
try {
|
||
// 1. 将本地文件添加到缓存
|
||
await cacheManager.putFile(
|
||
cacheKey, // 缓存键(唯一标识)
|
||
localFile.readAsBytesSync(), // 文件字节数据
|
||
fileExtension: localFile.path.split('.').last, // 文件扩展名
|
||
);
|
||
|
||
// 2. 验证文件已缓存
|
||
final cachedFile = await cacheManager.getFileFromCache(cacheKey);
|
||
if (cachedFile != null) {
|
||
print('文件已成功添加到缓存: ${cachedFile.file.path}');
|
||
}
|
||
} catch (e) {
|
||
print('添加文件到缓存失败: $e');
|
||
}
|
||
}
|
||
|
||
} |