From db2c2395de4ebfc1bb758fea5791c083e9e87051 Mon Sep 17 00:00:00 2001 From: nyne Date: Mon, 11 Nov 2024 10:35:21 +0800 Subject: [PATCH] fix importing data on windows --- lib/utils/data.dart | 10 +++++++--- lib/utils/io.dart | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/utils/data.dart b/lib/utils/data.dart index ec55e9b..0865244 100644 --- a/lib/utils/data.dart +++ b/lib/utils/data.dart @@ -54,16 +54,19 @@ Future importAppData(File file, [bool checkVersion = false]) async { } if(await historyFile.exists()) { HistoryManager().close(); - historyFile.copySync(FilePath.join(App.dataPath, "history.db")); + File(FilePath.join(App.dataPath, "history.db")).deleteIfExistsSync(); + historyFile.renameSync(FilePath.join(App.dataPath, "history.db")); HistoryManager().init(); } if(await localFavoriteFile.exists()) { LocalFavoritesManager().close(); - localFavoriteFile.copySync(FilePath.join(App.dataPath, "local_favorite.db")); + File(FilePath.join(App.dataPath, "local_favorite.db")).deleteIfExistsSync(); + localFavoriteFile.renameSync(FilePath.join(App.dataPath, "local_favorite.db")); LocalFavoritesManager().init(); } if(await appdataFile.exists()) { - await appdataFile.copy(FilePath.join(App.dataPath, "appdata.json")); + File(FilePath.join(App.dataPath, "appdata.json")).deleteIfExistsSync(); + appdataFile.renameSync(FilePath.join(App.dataPath, "appdata.json")); appdata.init(); } var comicSourceDir = FilePath.join(cacheDirPath, "comic_source"); @@ -71,6 +74,7 @@ Future importAppData(File file, [bool checkVersion = false]) async { for(var file in Directory(comicSourceDir).listSync()) { if(file is File) { var targetFile = FilePath.join(App.dataPath, "comic_source", file.name); + File(targetFile).deleteIfExistsSync(); await file.copy(targetFile); } } diff --git a/lib/utils/io.dart b/lib/utils/io.dart index 5431f18..34377c5 100644 --- a/lib/utils/io.dart +++ b/lib/utils/io.dart @@ -45,6 +45,18 @@ extension FileSystemEntityExt on FileSystemEntity { // ignore } } + + Future deleteIfExists({bool recursive = false}) async { + if (existsSync()) { + await delete(recursive: recursive); + } + } + + void deleteIfExistsSync({bool recursive = false}) { + if (existsSync()) { + deleteSync(recursive: recursive); + } + } } extension FileExtension on File {