Skip to content

Commit

Permalink
Improve backup functionality and error handling
Browse files Browse the repository at this point in the history
- (Windows) Fix not selecting the right backup file in explorer
- Fix progress indicator not stopping on WebDAV failure

#188 #364
  • Loading branch information
PaperCube committed May 23, 2024
1 parent 17b8bbd commit 5888cb7
Showing 1 changed file with 74 additions and 55 deletions.
129 changes: 74 additions & 55 deletions lib/view/page/backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ class BackupPage extends StatelessWidget {
trailing: const Icon(Icons.save),
onTap: () async {
final path = await Backup.backup();
debugPrint("Backup path: $path");

/// Issue #188
final _ = switch (Pfs.type) {
Pfs.windows =>
await Process.run('explorer', ['/select,', path]),
Pfs.linux => await Process.run('xdg-open', [path]),
_ => await Pfs.sharePath(path),
};
switch (Pfs.type) {
case Pfs.windows:
final backslashPath = path.replaceAll('/', '\\');
await Process.run('explorer', ['/select,$backslashPath']);
case Pfs.linux:
await Process.run('xdg-open', [path]);
default:
await Pfs.sharePath(path);
}
},
),
ListTile(
Expand Down Expand Up @@ -263,64 +267,79 @@ class BackupPage extends StatelessWidget {

Future<void> _onTapWebdavDl(BuildContext context) async {
webdavLoading.value = true;
final files = await Webdav.list();
if (files.isEmpty) {
context.showSnackBar(l10n.dirEmpty);
webdavLoading.value = false;
return;
}
try {
final files = await Webdav.list();
if (files.isEmpty) {
context.showSnackBar(l10n.dirEmpty);
webdavLoading.value = false;
return;
}

final fileName = await context.showRoundDialog<String>(
title: 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),
final fileName = await context.showRoundDialog<String>(
title: 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),
);
},
),
),
],
);
if (fileName == null) {
webdavLoading.value = false;
return;
}
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');
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('${Paths.doc}/$fileName').readAsString();
final dlBak = await Computer.shared.start(Backup.fromJsonString, dlFile);
await dlBak.restore(force: true);
webdavLoading.value = false;
} catch (e) {
context.showSnackBar(e.toString());
rethrow;
} finally {
webdavLoading.value = false;
return;
}
final dlFile = await File('${Paths.doc}/$fileName').readAsString();
final dlBak = await Computer.shared.start(Backup.fromJsonString, dlFile);
await dlBak.restore(force: true);
webdavLoading.value = false;
}

Future<void> _onTapWebdavUp(BuildContext context) async {
webdavLoading.value = true;
final bakName = '${DateTime.now().ymdhms()}-${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 {
Loggers.app.info('Upload webdav backup success');
try {
webdavLoading.value = true;
final bakName =
'${DateTime.now().ymdhms(ymdSep: "-", hmsSep: "-", sep: "-")}-${Paths.bakName}';
await Backup.backup(bakName);
final uploadResult = await Webdav.upload(relativePath: bakName);
if (uploadResult != null) {
Loggers.app.warning('Upload webdav backup failed: $uploadResult');
context.showSnackBar(uploadResult.toString());
} else {
Loggers.app.info('Upload webdav backup success');
}
} catch (e) {
context.showSnackBar(e.toString());
rethrow;
} finally {
webdavLoading.value = false;
}
webdavLoading.value = false;
}

Future<void> _onTapWebdavSetting(BuildContext context) async {
Expand Down

0 comments on commit 5888cb7

Please sign in to comment.