feat: custom pwd of bak (#827)

This commit is contained in:
lollipopkit🏳️‍⚧️
2025-07-25 16:38:28 +08:00
committed by GitHub
parent 8c3302cf0d
commit 682a6e4f2d
37 changed files with 779 additions and 236 deletions

View File

@@ -8,7 +8,8 @@ import 'package:icons_plus/icons_plus.dart';
import 'package:server_box/core/extension/context/locale.dart';
import 'package:server_box/core/sync.dart';
import 'package:server_box/data/model/app/bak/backup2.dart';
import 'package:server_box/data/model/app/bak/utils.dart';
import 'package:server_box/data/model/app/bak/backup_service.dart';
import 'package:server_box/data/model/app/bak/backup_source.dart';
import 'package:server_box/data/model/server/server_private_info.dart';
import 'package:server_box/data/model/server/snippet.dart';
import 'package:server_box/data/provider/snippet.dart';
@@ -22,10 +23,7 @@ class BackupPage extends StatefulWidget {
@override
State<BackupPage> createState() => _BackupPageState();
static const route = AppRouteNoArg(
page: BackupPage.new,
path: '/backup',
);
static const route = AppRouteNoArg(page: BackupPage.new, path: '/backup');
}
final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveClientMixin {
@@ -40,33 +38,27 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
body: _buildBody(context),
);
return Scaffold(body: _buildBody);
}
Widget _buildBody(BuildContext context) {
Widget get _buildBody {
return MultiList(
widthDivider: 2,
children: [
[
CenterGreyTitle(libL10n.sync),
_buildTip(),
if (isMacOS || isIOS) _buildIcloud(context),
_buildWebdav(context),
_buildFile(context),
_buildClipboard(context),
],
[
CenterGreyTitle(libL10n.import),
_buildBulkImportServers(context),
_buildImportSnippet(context),
_buildTip,
if (isMacOS || isIOS) _buildIcloud,
_buildWebdav,
_buildFile,
_buildClipboard,
],
[CenterGreyTitle(libL10n.import), _buildBulkImportServers, _buildImportSnippet],
],
);
}
Widget _buildTip() {
Widget get _buildTip {
return CardX(
child: ListTile(
leading: const Icon(Icons.warning),
@@ -76,7 +68,7 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
);
}
Widget _buildFile(BuildContext context) {
Widget get _buildFile {
return CardX(
child: ExpandTile(
leading: const Icon(Icons.file_open),
@@ -84,24 +76,21 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
initiallyExpanded: false,
children: [
ListTile(
title: Text(libL10n.backup),
trailing: const Icon(Icons.save),
onTap: () async {
final path = await BackupV2.backup();
await Pfs.sharePaths(paths: [path]);
},
title: Text(libL10n.backup),
trailing: const Icon(Icons.save),
onTap: () => BackupService.backup(context, FileBackupSource())
),
ListTile(
trailing: const Icon(Icons.restore),
title: Text(libL10n.restore),
onTap: () async => _onTapFileRestore(context),
onTap: () => BackupService.restore(context, FileBackupSource()),
),
],
),
);
}
Widget _buildIcloud(BuildContext context) {
Widget get _buildIcloud {
return CardX(
child: ListTile(
leading: const Icon(Icons.cloud),
@@ -123,7 +112,7 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
);
}
Widget _buildWebdav(BuildContext context) {
Widget get _buildWebdav {
return CardX(
child: ExpandTile(
leading: const Icon(Icons.storage),
@@ -171,33 +160,25 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
),
ListTile(
title: Text(l10n.manual),
trailing: webdavLoading.listenVal(
(loading) {
if (loading) return SizedLoading.small;
trailing: webdavLoading.listenVal((loading) {
if (loading) return SizedLoading.small;
return Row(
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () async => _onTapWebdavDl(context),
child: Text(libL10n.restore),
),
UIs.width7,
TextButton(
onPressed: () async => _onTapWebdavUp(context),
child: Text(libL10n.backup),
),
],
);
},
),
return Row(
mainAxisSize: MainAxisSize.min,
children: [
TextButton(onPressed: () async => _onTapWebdavDl(context), child: Text(libL10n.restore)),
UIs.width7,
TextButton(onPressed: () async => _onTapWebdavUp(context), child: Text(libL10n.backup)),
],
);
}),
),
],
),
);
}
Widget _buildClipboard(BuildContext context) {
Widget get _buildClipboard {
return CardX(
child: ExpandTile(
leading: const Icon(Icons.content_paste),
@@ -206,23 +187,19 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
ListTile(
title: Text(libL10n.backup),
trailing: const Icon(Icons.save),
onTap: () async {
final path = await BackupV2.backup();
Pfs.copy(await File(path).readAsString());
context.showSnackBar(libL10n.success);
},
onTap: () => BackupService.backup(context, ClipboardBackupSource()),
),
ListTile(
trailing: const Icon(Icons.restore),
title: Text(libL10n.restore),
onTap: () async => _onTapClipboardRestore(context),
onTap: () => BackupService.restore(context, ClipboardBackupSource()),
),
],
),
);
}
Widget _buildBulkImportServers(BuildContext context) {
Widget get _buildBulkImportServers {
return CardX(
child: ListTile(
title: Text(l10n.server),
@@ -233,16 +210,13 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
);
}
Widget _buildImportSnippet(BuildContext context) {
Widget get _buildImportSnippet {
return ListTile(
title: Text(l10n.snippet),
leading: const Icon(MingCute.code_line),
trailing: const Icon(Icons.keyboard_arrow_right),
onTap: () async {
final data = await context.showImportDialog(
title: l10n.snippet,
modelDef: Snippet.example.toJson(),
);
final data = await context.showImportDialog(title: l10n.snippet, modelDef: Snippet.example.toJson());
if (data == null) return;
final str = String.fromCharCodes(data);
final (list, _) = await context.showLoadingDialog(
@@ -275,11 +249,7 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
final snippetNames = snippets.map((e) => e.name).join(', ');
context.showRoundDialog(
title: libL10n.attention,
child: SingleChildScrollView(
child: Text(
libL10n.askContinue('${libL10n.import} [$snippetNames]'),
),
),
child: SingleChildScrollView(child: Text(libL10n.askContinue('${libL10n.import} [$snippetNames]'))),
actions: Btn.ok(
onTap: () {
for (final snippet in snippets) {
@@ -294,33 +264,6 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
).cardx;
}
Future<void> _onTapFileRestore(BuildContext context) async {
final text = await Pfs.pickFileString();
if (text == null) return;
try {
final (backup, err) = await context.showLoadingDialog(
fn: () => Computer.shared.start(MergeableUtils.fromJsonString, text.trim()),
);
if (err != null || backup == null) return;
await context.showRoundDialog(
title: libL10n.restore,
child: Text(libL10n.askContinue(
'${libL10n.restore} ${libL10n.backup}(${backup.$2})',
)),
actions: Btn.ok(
onTap: () async {
await backup.$1.merge(force: true);
context.pop();
},
).toList,
);
} catch (e, s) {
Loggers.app.warning('Import backup failed', e, s);
context.showErrDialog(e, s, libL10n.restore);
}
}
Future<void> _onTapWebdavDl(BuildContext context) async {
webdavLoading.value = true;
@@ -328,16 +271,12 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
final files = await Webdav.shared.list();
if (files.isEmpty) return context.showSnackBar(l10n.dirEmpty);
final fileName = await context.showPickSingleDialog(
title: libL10n.restore,
items: files,
);
final fileName = await context.showPickSingleDialog(title: libL10n.restore, items: files);
if (fileName == null) return;
await Webdav.shared.download(relativePath: fileName);
final dlFile = await File('${Paths.doc}/$fileName').readAsString();
final dlBak = await Computer.shared.start(BackupV2.fromJsonString, dlFile);
await dlBak.merge(force: true);
await BackupService.restoreFromText(context, dlFile);
} catch (e, s) {
context.showErrDialog(e, s, libL10n.restore);
Loggers.app.warning('Download webdav backup failed', e, s);
@@ -351,7 +290,8 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
final date = DateTime.now().ymdhms(ymdSep: '-', hmsSep: '-', sep: '-');
final bakName = '$date-${Miscs.bakFileName}';
try {
await BackupV2.backup(bakName);
final savedPassword = await Stores.setting.backupasswd.read();
await BackupV2.backup(bakName, savedPassword);
await Webdav.shared.upload(relativePath: bakName);
Loggers.app.info('Upload webdav backup success');
} catch (e, s) {
@@ -388,7 +328,7 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
onSubmitted: (p0) => FocusScope.of(context).requestFocus(nodePwd),
),
Input(
label: l10n.pwd,
label: libL10n.pwd,
controller: pwd,
node: nodePwd,
suggestion: false,
@@ -417,42 +357,9 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
}
}
void _onTapClipboardRestore(BuildContext context) async {
final text = await Pfs.paste();
if (text == null || text.isEmpty) {
context.showSnackBar(libL10n.empty);
return;
}
try {
final (backup, err) = await context.showLoadingDialog(
fn: () => Computer.shared.start(MergeableUtils.fromJsonString, text.trim()),
);
if (err != null || backup == null) return;
await context.showRoundDialog(
title: libL10n.restore,
child: Text(libL10n.askContinue(
'${libL10n.restore} ${libL10n.backup}(${backup.$2})',
)),
actions: Btn.ok(
onTap: () async {
await backup.$1.merge(force: true);
context.pop();
},
).toList,
);
} catch (e, s) {
Loggers.app.warning('Import backup failed', e, s);
context.showErrDialog(e, s, libL10n.restore);
}
}
void _onBulkImportServers(BuildContext context) async {
final data = await context.showImportDialog(
title: l10n.server,
modelDef: Spix.example.toJson(),
);
final data = await context.showImportDialog(title: l10n.server, modelDef: Spix.example.toJson());
if (data == null) return;
final text = String.fromCharCodes(data);
@@ -487,6 +394,11 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
}
}
@override
bool get wantKeepAlive => true;
}