Exported data should contain cookies

This commit is contained in:
2024-11-12 16:36:02 +08:00
parent 6c076bfc7a
commit c4f531a463
2 changed files with 28 additions and 12 deletions

View File

@@ -10,6 +10,7 @@ import 'package:venera/foundation/app.dart';
import 'package:venera/foundation/comic_type.dart'; import 'package:venera/foundation/comic_type.dart';
import 'package:venera/foundation/history.dart'; import 'package:venera/foundation/history.dart';
import 'package:venera/foundation/res.dart'; import 'package:venera/foundation/res.dart';
import 'package:venera/utils/data_sync.dart';
import 'package:venera/utils/ext.dart'; import 'package:venera/utils/ext.dart';
import 'package:venera/utils/io.dart'; import 'package:venera/utils/io.dart';
import 'package:venera/utils/translations.dart'; import 'package:venera/utils/translations.dart';
@@ -236,6 +237,7 @@ class ComicSource {
} }
await file.writeAsString(jsonEncode(data)); await file.writeAsString(jsonEncode(data));
_isSaving = false; _isSaving = false;
DataSync().uploadData();
} }
Future<bool> reLogin() async { Future<bool> reLogin() async {

View File

@@ -6,6 +6,7 @@ import 'package:venera/foundation/appdata.dart';
import 'package:venera/foundation/comic_source/comic_source.dart'; import 'package:venera/foundation/comic_source/comic_source.dart';
import 'package:venera/foundation/favorites.dart'; import 'package:venera/foundation/favorites.dart';
import 'package:venera/foundation/history.dart'; import 'package:venera/foundation/history.dart';
import 'package:venera/network/cookie_jar.dart';
import 'package:zip_flutter/zip_flutter.dart'; import 'package:zip_flutter/zip_flutter.dart';
import 'io.dart'; import 'io.dart';
@@ -15,7 +16,7 @@ Future<File> exportAppData() async {
var cacheFilePath = FilePath.join(App.cachePath, '$time.venera'); var cacheFilePath = FilePath.join(App.cachePath, '$time.venera');
var cacheFile = File(cacheFilePath); var cacheFile = File(cacheFilePath);
var dataPath = App.dataPath; var dataPath = App.dataPath;
if(await cacheFile.exists()) { if (await cacheFile.exists()) {
await cacheFile.delete(); await cacheFile.delete();
} }
await Isolate.run(() { await Isolate.run(() {
@@ -23,11 +24,14 @@ Future<File> exportAppData() async {
var historyFile = FilePath.join(dataPath, "history.db"); var historyFile = FilePath.join(dataPath, "history.db");
var localFavoriteFile = FilePath.join(dataPath, "local_favorite.db"); var localFavoriteFile = FilePath.join(dataPath, "local_favorite.db");
var appdata = FilePath.join(dataPath, "appdata.json"); var appdata = FilePath.join(dataPath, "appdata.json");
var cookies = FilePath.join(dataPath, "cookie.db");
zipFile.addFile("history.db", historyFile); zipFile.addFile("history.db", historyFile);
zipFile.addFile("local_favorite.db", localFavoriteFile); zipFile.addFile("local_favorite.db", localFavoriteFile);
zipFile.addFile("appdata.json", appdata); zipFile.addFile("appdata.json", appdata);
for(var file in Directory(FilePath.join(dataPath, "comic_source")).listSync()) { zipFile.addFile("cookie.db", cookies);
if(file is File) { for (var file
in Directory(FilePath.join(dataPath, "comic_source")).listSync()) {
if (file is File) {
zipFile.addFile("comic_source/${file.name}", file.path); zipFile.addFile("comic_source/${file.name}", file.path);
} }
} }
@@ -45,26 +49,28 @@ Future<void> importAppData(File file, [bool checkVersion = false]) async {
var historyFile = cacheDir.joinFile("history.db"); var historyFile = cacheDir.joinFile("history.db");
var localFavoriteFile = cacheDir.joinFile("local_favorite.db"); var localFavoriteFile = cacheDir.joinFile("local_favorite.db");
var appdataFile = cacheDir.joinFile("appdata.json"); var appdataFile = cacheDir.joinFile("appdata.json");
if(checkVersion && appdataFile.existsSync()) { var cookieFile = cacheDir.joinFile("cookie.db");
if (checkVersion && appdataFile.existsSync()) {
var data = jsonDecode(await appdataFile.readAsString()); var data = jsonDecode(await appdataFile.readAsString());
var version = data["settings"]["dataVersion"]; var version = data["settings"]["dataVersion"];
if(version is int && version <= appdata.settings["dataVersion"]) { if (version is int && version <= appdata.settings["dataVersion"]) {
return; return;
} }
} }
if(await historyFile.exists()) { if (await historyFile.exists()) {
HistoryManager().close(); HistoryManager().close();
File(FilePath.join(App.dataPath, "history.db")).deleteIfExistsSync(); File(FilePath.join(App.dataPath, "history.db")).deleteIfExistsSync();
historyFile.renameSync(FilePath.join(App.dataPath, "history.db")); historyFile.renameSync(FilePath.join(App.dataPath, "history.db"));
HistoryManager().init(); HistoryManager().init();
} }
if(await localFavoriteFile.exists()) { if (await localFavoriteFile.exists()) {
LocalFavoritesManager().close(); LocalFavoritesManager().close();
File(FilePath.join(App.dataPath, "local_favorite.db")).deleteIfExistsSync(); File(FilePath.join(App.dataPath, "local_favorite.db")).deleteIfExistsSync();
localFavoriteFile.renameSync(FilePath.join(App.dataPath, "local_favorite.db")); localFavoriteFile
.renameSync(FilePath.join(App.dataPath, "local_favorite.db"));
LocalFavoritesManager().init(); LocalFavoritesManager().init();
} }
if(await appdataFile.exists()) { if (await appdataFile.exists()) {
// proxy settings should be kept // proxy settings should be kept
var proxySettings = appdata.settings["proxy"]; var proxySettings = appdata.settings["proxy"];
File(FilePath.join(App.dataPath, "appdata.json")).deleteIfExistsSync(); File(FilePath.join(App.dataPath, "appdata.json")).deleteIfExistsSync();
@@ -73,10 +79,18 @@ Future<void> importAppData(File file, [bool checkVersion = false]) async {
appdata.settings["proxy"] = proxySettings; appdata.settings["proxy"] = proxySettings;
appdata.saveData(); appdata.saveData();
} }
if (await cookieFile.exists()) {
SingleInstanceCookieJar.instance?.dispose();
File(FilePath.join(App.dataPath, "cookie.db")).deleteIfExistsSync();
cookieFile.renameSync(FilePath.join(App.dataPath, "cookie.db"));
SingleInstanceCookieJar.instance =
SingleInstanceCookieJar(FilePath.join(App.dataPath, "cookie.db"))
..init();
}
var comicSourceDir = FilePath.join(cacheDirPath, "comic_source"); var comicSourceDir = FilePath.join(cacheDirPath, "comic_source");
if(Directory(comicSourceDir).existsSync()) { if (Directory(comicSourceDir).existsSync()) {
for(var file in Directory(comicSourceDir).listSync()) { for (var file in Directory(comicSourceDir).listSync()) {
if(file is File) { if (file is File) {
var targetFile = FilePath.join(App.dataPath, "comic_source", file.name); var targetFile = FilePath.join(App.dataPath, "comic_source", file.name);
File(targetFile).deleteIfExistsSync(); File(targetFile).deleteIfExistsSync();
await file.copy(targetFile); await file.copy(targetFile);