mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 23:34:24 +01:00
opt.: auto add date for manual webdav backup
This commit is contained in:
@@ -2,4 +2,14 @@ extension DateTimeX on DateTime {
|
||||
String get hourMinute {
|
||||
return '${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
/// Format: 2021-01-01-0000
|
||||
String get numStr {
|
||||
final year = this.year.toString();
|
||||
final month = this.month.toString().padLeft(2, '0');
|
||||
final day = this.day.toString().padLeft(2, '0');
|
||||
final hour = this.hour.toString().padLeft(2, '0');
|
||||
final minute = this.minute.toString().padLeft(2, '0');
|
||||
return '$year-$month-$day-$hour$minute';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,21 @@ abstract final class Webdav {
|
||||
return null;
|
||||
}
|
||||
|
||||
static Future<List<String>> list() async {
|
||||
try {
|
||||
final list = await _client.readDir(_prefix);
|
||||
final names = <String>[];
|
||||
for (final item in list) {
|
||||
if ((item.isDir ?? true) || item.name == null) continue;
|
||||
names.add(item.name!);
|
||||
}
|
||||
return names;
|
||||
} catch (e, s) {
|
||||
_logger.warning('List failed', e, s);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
static void changeClient(String url, String user, String pwd) {
|
||||
_client = WebdavClient(url: url, user: user, pwd: pwd);
|
||||
Stores.setting.webdavUrl.put(url);
|
||||
|
||||
@@ -76,9 +76,9 @@ class Backup {
|
||||
lastModTime = Stores.lastModTime,
|
||||
history = Stores.history.box.toJson();
|
||||
|
||||
static Future<String> backup() async {
|
||||
static Future<String> backup([String? name]) async {
|
||||
final result = _diyEncrypt(json.encode(Backup.loadFromStore()));
|
||||
final path = await Paths.bak;
|
||||
final path = '${await Paths.doc}/${name ?? Paths.bakName}';
|
||||
await File(path).writeAsString(result);
|
||||
return path;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
class BuildData {
|
||||
static const String name = "ServerBox";
|
||||
static const int build = 700;
|
||||
static const int build = 701;
|
||||
static const String engine = "3.16.5";
|
||||
static const String buildAt = "2024-01-06 11:00:48";
|
||||
static const int modifications = 4;
|
||||
static const String buildAt = "2024-01-06 13:39:30";
|
||||
static const int modifications = 1;
|
||||
static const int script = 34;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:toolbox/core/extension/context/common.dart';
|
||||
import 'package:toolbox/core/extension/context/dialog.dart';
|
||||
import 'package:toolbox/core/extension/context/locale.dart';
|
||||
import 'package:toolbox/core/extension/context/snackbar.dart';
|
||||
import 'package:toolbox/core/extension/datetime.dart';
|
||||
import 'package:toolbox/core/utils/misc.dart';
|
||||
import 'package:toolbox/core/utils/sync/icloud.dart';
|
||||
import 'package:toolbox/core/utils/platform/base.dart';
|
||||
@@ -122,8 +123,7 @@ class BackupPage extends StatelessWidget {
|
||||
child: ExpandTile(
|
||||
leading: const Icon(Icons.storage),
|
||||
title: const Text('WebDAV'),
|
||||
initiallyExpanded:
|
||||
!(isIOS || isMacOS) && Stores.setting.webdavSync.fetch(),
|
||||
initiallyExpanded: true,
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(l10n.setting),
|
||||
@@ -241,21 +241,48 @@ class BackupPage extends StatelessWidget {
|
||||
|
||||
Future<void> _onTapWebdavDl(BuildContext context) async {
|
||||
webdavLoading.value = true;
|
||||
try {
|
||||
final result = await Webdav.download(
|
||||
relativePath: Paths.bakName,
|
||||
);
|
||||
if (result != null) {
|
||||
Loggers.app.warning('Download webdav backup failed: $result');
|
||||
return;
|
||||
}
|
||||
} catch (e, s) {
|
||||
Loggers.app.warning('Download webdav backup failed', e, s);
|
||||
context.showSnackBar(e.toString());
|
||||
final files = await Webdav.list();
|
||||
if (files.isEmpty) {
|
||||
context.showSnackBar(l10n.dirEmpty);
|
||||
webdavLoading.value = false;
|
||||
return;
|
||||
}
|
||||
final dlFile = await File(await Paths.bak).readAsString();
|
||||
|
||||
final fileName = await context.showRoundDialog<String>(
|
||||
title: Text(l10n.restore),
|
||||
child: SizedBox(
|
||||
width: 300,
|
||||
height: 300,
|
||||
child: ListView.builder(
|
||||
itemCount: files.length,
|
||||
itemBuilder: (_, index) {
|
||||
final file = files[index];
|
||||
return ListTile(
|
||||
title: Text(file),
|
||||
onTap: () => context.pop(file),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => context.pop(),
|
||||
child: Text(l10n.cancel),
|
||||
),
|
||||
],
|
||||
);
|
||||
if (fileName == null) {
|
||||
webdavLoading.value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await Webdav.download(relativePath: fileName);
|
||||
if (result != null) {
|
||||
Loggers.app.warning('Download webdav backup failed: $result');
|
||||
webdavLoading.value = false;
|
||||
return;
|
||||
}
|
||||
final dlFile = await File(fileName).readAsString();
|
||||
final dlBak = await compute(Backup.fromJsonString, dlFile);
|
||||
await dlBak.restore(force: true);
|
||||
webdavLoading.value = false;
|
||||
@@ -263,8 +290,9 @@ class BackupPage extends StatelessWidget {
|
||||
|
||||
Future<void> _onTapWebdavUp(BuildContext context) async {
|
||||
webdavLoading.value = true;
|
||||
await Backup.backup();
|
||||
final uploadResult = await Webdav.upload(relativePath: Paths.bakName);
|
||||
final bakName = '${DateTime.now().numStr}-${Paths.bakName}';
|
||||
await Backup.backup(bakName);
|
||||
final uploadResult = await Webdav.upload(relativePath: bakName);
|
||||
if (uploadResult != null) {
|
||||
Loggers.app.warning('Upload webdav backup failed: $uploadResult');
|
||||
} else {
|
||||
|
||||
@@ -59,7 +59,7 @@ class _InputState extends State<Input> {
|
||||
Widget build(BuildContext context) {
|
||||
return CardX(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 7),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextField(
|
||||
controller: widget.controller,
|
||||
maxLines: widget.maxLines,
|
||||
|
||||
Reference in New Issue
Block a user