添加删除机制
This commit is contained in:
parent
521bed4ec3
commit
84e518820e
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue