From 46d42440c7cb54ca7175bde5cb4984c6bd433e55 Mon Sep 17 00:00:00 2001 From: lollipopkit Date: Sun, 2 Jun 2024 16:16:59 +0800 Subject: [PATCH] fix: sync --- lib/core/util/sync/icloud.dart | 5 +++-- lib/core/util/sync/webdav.dart | 6 +++--- lib/data/res/misc.dart | 3 +++ lib/data/res/url.dart | 2 -- lib/main.dart | 3 ++- lib/view/page/backup/impl/webdav.dart | 24 +++++++++++++++--------- lib/view/page/backup/view.dart | 1 + 7 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 lib/data/res/misc.dart diff --git a/lib/core/util/sync/icloud.dart b/lib/core/util/sync/icloud.dart index 53b9634..0fcebe4 100644 --- a/lib/core/util/sync/icloud.dart +++ b/lib/core/util/sync/icloud.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:fl_lib/fl_lib.dart'; import 'package:flutter/foundation.dart'; +import 'package:gpt_box/data/res/misc.dart'; import 'package:icloud_storage/icloud_storage.dart'; import 'package:logging/logging.dart'; @@ -197,7 +198,7 @@ abstract final class ICloud { } static Future sync() async { - final result = await download(relativePath: Paths.bak); + final result = await download(relativePath: Miscs.bakFileName); if (result != null) return await backup(); final dlFile = await File(Paths.bak).readAsString(); @@ -212,7 +213,7 @@ abstract final class ICloud { static Future backup() async { final content = await Backup.backup(); await File(Paths.bak).writeAsString(content); - final uploadResult = await upload(relativePath: Paths.bak); + final uploadResult = await upload(relativePath: Miscs.bakFileName); if (uploadResult != null) { _logger.warning('Upload backup failed: $uploadResult'); } diff --git a/lib/core/util/sync/webdav.dart b/lib/core/util/sync/webdav.dart index 49e401d..c3b8919 100644 --- a/lib/core/util/sync/webdav.dart +++ b/lib/core/util/sync/webdav.dart @@ -2,7 +2,7 @@ import 'dart:io'; import 'package:fl_lib/fl_lib.dart'; import 'package:flutter/foundation.dart'; -import 'package:gpt_box/data/res/url.dart'; +import 'package:gpt_box/data/res/misc.dart'; import 'package:logging/logging.dart'; import 'package:webdav_client/webdav_client.dart'; @@ -98,7 +98,7 @@ abstract final class Webdav { } static Future sync() async { - final dlErr = await download(relativePath: Urls.bakFileName); + final dlErr = await download(relativePath: Miscs.bakFileName); if (dlErr != null) return await backup(); final dlFile = await File(Paths.bak).readAsString(); @@ -113,7 +113,7 @@ abstract final class Webdav { /// Create a local backup and upload it to WebDAV static Future backup() async { await Backup.backupToFile(); - final err = await upload(relativePath: Urls.bakFileName); + final err = await upload(relativePath: Miscs.bakFileName); if (err != null) { _logger.warning('Upload failed: $err'); } diff --git a/lib/data/res/misc.dart b/lib/data/res/misc.dart new file mode 100644 index 0000000..608dce2 --- /dev/null +++ b/lib/data/res/misc.dart @@ -0,0 +1,3 @@ +abstract final class Miscs { + static const bakFileName = 'gptbox_bak.json'; +} \ No newline at end of file diff --git a/lib/data/res/url.dart b/lib/data/res/url.dart index ff44a1d..b8bc69e 100644 --- a/lib/data/res/url.dart +++ b/lib/data/res/url.dart @@ -9,6 +9,4 @@ abstract final class Urls { static const backendBase = 'https://cdn.lolli.tech/gptbox/'; static const appUpdateCfg = '$backendBase/update.json'; - - static const bakFileName = 'gptbox_backup.json'; } diff --git a/lib/main.dart b/lib/main.dart index 3f9f2b6..b0a3233 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,6 +12,7 @@ import 'package:gpt_box/data/model/chat/config.dart'; import 'package:gpt_box/data/model/chat/history.dart'; import 'package:gpt_box/data/provider/all.dart'; import 'package:gpt_box/data/res/build.dart'; +import 'package:gpt_box/data/res/misc.dart'; import 'package:gpt_box/data/res/openai.dart'; import 'package:gpt_box/data/store/all.dart'; import 'package:hive_flutter/hive_flutter.dart'; @@ -45,7 +46,7 @@ Future _initApp() async { WidgetsFlutterBinding.ensureInitialized(); _setupLogger(); - await Paths.init(Build.name, bakName: 'gptbox'); + await Paths.init(Build.name, bakName: Miscs.bakFileName); await _initDb(); _initAppComponents(); diff --git a/lib/view/page/backup/impl/webdav.dart b/lib/view/page/backup/impl/webdav.dart index 1e50d21..bccffd9 100644 --- a/lib/view/page/backup/impl/webdav.dart +++ b/lib/view/page/backup/impl/webdav.dart @@ -66,19 +66,25 @@ Widget _buildWebdav(BuildContext context) { Future _onTapWebdavDl(BuildContext context) async { _webdavLoading.value = true; try { - final result = await Webdav.download(relativePath: Urls.bakFileName); + final files = await Webdav.list(); + if (files.isEmpty) return context.showSnackBar(l10n.empty); + + final fileName = await context.showPickSingleDialog( + title: l10n.choose, + items: files, + ); + if (fileName == null) return; + + final result = await Webdav.download(relativePath: fileName); if (result != null) { - Loggers.app.warning('Download webdav backup failed: $result'); - context.showSnackBar(l10n.backupRestorationFailed(result.toString())); - return; + return Loggers.app.warning('Download webdav backup failed: $result'); } - final dlFile = await File(Paths.bak).readAsString(); + final dlFile = await File('${Paths.doc}/$fileName').readAsString(); final dlBak = await compute(Backup.fromJsonString, dlFile); await dlBak?.merge(force: true); - context.showSnackBar(l10n.backupRestorationSuccessful); - } catch (e, s) { - Loggers.app.warning('Download webdav backup failed', e, s); + } catch (e) { context.showSnackBar(e.toString()); + rethrow; } finally { _webdavLoading.value = false; } @@ -88,7 +94,7 @@ Future _onTapWebdavUp(BuildContext context) async { _webdavLoading.value = true; final content = await Backup.backup(); await File(Paths.bak).writeAsString(content); - final uploadResult = await Webdav.upload(relativePath: Urls.bakFileName); + final uploadResult = await Webdav.upload(relativePath: Miscs.bakFileName); if (uploadResult != null) { Loggers.app.warning('Upload webdav backup failed: $uploadResult'); context.showSnackBar(l10n.backupFailed(uploadResult.toString())); diff --git a/lib/view/page/backup/view.dart b/lib/view/page/backup/view.dart index 7f07287..b6c366d 100644 --- a/lib/view/page/backup/view.dart +++ b/lib/view/page/backup/view.dart @@ -11,6 +11,7 @@ import 'package:gpt_box/data/model/chat/gpt_next.dart'; import 'package:gpt_box/data/model/chat/history.dart'; import 'package:gpt_box/data/model/chat/openai.dart'; import 'package:gpt_box/data/res/l10n.dart'; +import 'package:gpt_box/data/res/misc.dart'; import 'package:gpt_box/data/res/url.dart'; import 'package:gpt_box/data/store/all.dart'; import 'package:gpt_box/view/page/home/home.dart';