45 lines
1.3 KiB
Dart
45 lines
1.3 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
import 'package:path/path.dart' as path;
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import '../tools_base/cache/dio_file_service.dart';
|
|
|
|
/// 视频缓存
|
|
class VideoCacheManager extends CacheManager {
|
|
static const key = "videoCache";
|
|
|
|
static VideoCacheManager? _instance;
|
|
|
|
factory VideoCacheManager() {
|
|
_instance ??= VideoCacheManager._(fileService: DioFileService());
|
|
return _instance!;
|
|
}
|
|
|
|
VideoCacheManager._({required FileService fileService})
|
|
: super(Config(
|
|
key,
|
|
stalePeriod: const Duration(days: 100),
|
|
maxNrOfCacheObjects: 200,
|
|
fileService: fileService,
|
|
));
|
|
|
|
/// 缓存存储路径
|
|
Future<String?> getFilePath() async {
|
|
final dir = await getCommonDir();
|
|
return dir == null ? null : path.join(dir.path, key);
|
|
}
|
|
|
|
/// 获取公用的dir目录
|
|
static Future<Directory?> getCommonDir() {
|
|
if (Platform.isIOS) return getTemporaryDirectory();
|
|
if (Platform.isAndroid) return getExternalStorageDirectory();
|
|
//桌面/其它平台用 app support 目录
|
|
if (Platform.isFuchsia || Platform.isMacOS || Platform.isLinux || Platform.isWindows) {
|
|
return getApplicationSupportDirectory();
|
|
}
|
|
return getExternalStorageDirectory();
|
|
}
|
|
}
|