Skip to content

Commit

Permalink
fix: sync
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Jun 2, 2024
1 parent cf65b8e commit 46d4244
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/core/util/sync/icloud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -197,7 +198,7 @@ abstract final class ICloud {
}

static Future<void> 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();
Expand All @@ -212,7 +213,7 @@ abstract final class ICloud {
static Future<void> 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');
}
Expand Down
6 changes: 3 additions & 3 deletions lib/core/util/sync/webdav.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -98,7 +98,7 @@ abstract final class Webdav {
}

static Future<void> 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();
Expand All @@ -113,7 +113,7 @@ abstract final class Webdav {
/// Create a local backup and upload it to WebDAV
static Future<void> 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');
}
Expand Down
3 changes: 3 additions & 0 deletions lib/data/res/misc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract final class Miscs {
static const bakFileName = 'gptbox_bak.json';
}
2 changes: 0 additions & 2 deletions lib/data/res/url.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -45,7 +46,7 @@ Future<void> _initApp() async {
WidgetsFlutterBinding.ensureInitialized();

_setupLogger();
await Paths.init(Build.name, bakName: 'gptbox');
await Paths.init(Build.name, bakName: Miscs.bakFileName);

await _initDb();
_initAppComponents();
Expand Down
24 changes: 15 additions & 9 deletions lib/view/page/backup/impl/webdav.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,25 @@ Widget _buildWebdav(BuildContext context) {
Future<void> _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;
}
Expand All @@ -88,7 +94,7 @@ Future<void> _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()));
Expand Down
1 change: 1 addition & 0 deletions lib/view/page/backup/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 46d4244

Please sign in to comment.