添加删除机制

This commit is contained in:
DESKTOP-I3JPKHK\wy 2025-11-27 14:47:17 +08:00
parent 521bed4ec3
commit 84e518820e
1 changed files with 35 additions and 0 deletions

View File

@ -284,6 +284,9 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform {
} }
debugPrint('下载路径: $savePath'); debugPrint('下载路径: $savePath');
// APK文件
await _clearHistoryApkFiles(savePath);
// //
final fileName = 'app_${DateTime.now().millisecondsSinceEpoch}.apk'; final fileName = 'app_${DateTime.now().millisecondsSinceEpoch}.apk';
final filePath = '$savePath/$fileName'; final filePath = '$savePath/$fileName';
@ -349,6 +352,38 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform {
} }
} }
/// APK文件
/// [downloadPath]
Future<void> _clearHistoryApkFiles(String downloadPath) async {
try {
final dir = Directory(downloadPath);
if (!await dir.exists()) {
debugPrint('下载目录不存在,跳过清理: $downloadPath');
return;
}
final files = await dir.list().toList();
int deletedCount = 0;
for (final file in files) {
if (file is File && file.path.endsWith('.apk')) {
try {
await file.delete();
deletedCount++;
debugPrint('已删除历史APK文件: ${file.path}');
} catch (e) {
debugPrint('删除历史APK文件失败: ${file.path}, 错误: $e');
}
}
}
if (deletedCount > 0) {
debugPrint('清理完成,共删除 $deletedCount 个历史APK文件');
}
} catch (e) {
debugPrint('清理历史APK文件失败: $e');
//
}
}
@override @override
Future<bool> installApk(String filePath) async { Future<bool> installApk(String filePath) async {
if (!Platform.isAndroid) { if (!Platform.isAndroid) {