mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 15:24:35 +01:00
opt.: backup restore
This commit is contained in:
@@ -78,12 +78,30 @@ extension BoxX on Box {
|
||||
}
|
||||
|
||||
extension StoreX on PersistentStore {
|
||||
_StoreProperty<T> property<T>(String key, T defaultValue) {
|
||||
return _StoreProperty<T>(box, key, defaultValue);
|
||||
_StoreProperty<T> property<T>(
|
||||
String key,
|
||||
T defaultValue, {
|
||||
bool updateLastModified = true,
|
||||
}) {
|
||||
return _StoreProperty<T>(
|
||||
box,
|
||||
key,
|
||||
defaultValue,
|
||||
updateLastModified: updateLastModified,
|
||||
);
|
||||
}
|
||||
|
||||
_StoreListProperty<T> listProperty<T>(String key, List<T> defaultValue) {
|
||||
return _StoreListProperty<T>(box, key, defaultValue);
|
||||
_StoreListProperty<T> listProperty<T>(
|
||||
String key,
|
||||
List<T> defaultValue, {
|
||||
bool updateLastModified = true,
|
||||
}) {
|
||||
return _StoreListProperty<T>(
|
||||
box,
|
||||
key,
|
||||
defaultValue,
|
||||
updateLastModified: updateLastModified,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,11 +113,17 @@ abstract class StorePropertyBase<T> {
|
||||
}
|
||||
|
||||
class _StoreProperty<T> implements StorePropertyBase<T> {
|
||||
_StoreProperty(this._box, this._key, this.defaultValue);
|
||||
_StoreProperty(
|
||||
this._box,
|
||||
this._key,
|
||||
this.defaultValue, {
|
||||
this.updateLastModified = true,
|
||||
});
|
||||
|
||||
final Box _box;
|
||||
final String _key;
|
||||
T defaultValue;
|
||||
bool updateLastModified;
|
||||
|
||||
@override
|
||||
ValueListenable<T> listenable() {
|
||||
@@ -117,7 +141,7 @@ class _StoreProperty<T> implements StorePropertyBase<T> {
|
||||
|
||||
@override
|
||||
Future<void> put(T value) {
|
||||
_box.updateLastModified();
|
||||
if (updateLastModified) _box.updateLastModified();
|
||||
return _box.put(_key, value);
|
||||
}
|
||||
|
||||
@@ -128,11 +152,17 @@ class _StoreProperty<T> implements StorePropertyBase<T> {
|
||||
}
|
||||
|
||||
class _StoreListProperty<T> implements StorePropertyBase<List<T>> {
|
||||
_StoreListProperty(this._box, this._key, this.defaultValue);
|
||||
_StoreListProperty(
|
||||
this._box,
|
||||
this._key,
|
||||
this.defaultValue, {
|
||||
this.updateLastModified = true,
|
||||
});
|
||||
|
||||
final Box _box;
|
||||
final String _key;
|
||||
List<T> defaultValue;
|
||||
bool updateLastModified;
|
||||
|
||||
@override
|
||||
ValueListenable<List<T>> listenable() {
|
||||
@@ -152,6 +182,7 @@ class _StoreListProperty<T> implements StorePropertyBase<List<T>> {
|
||||
|
||||
@override
|
||||
Future<void> put(List<T> value) {
|
||||
if (updateLastModified) _box.updateLastModified();
|
||||
return _box.put(_key, value);
|
||||
}
|
||||
|
||||
|
||||
@@ -205,23 +205,13 @@ abstract final class ICloud {
|
||||
}
|
||||
final dlFile = await File(await Paths.bak).readAsString();
|
||||
final dlBak = await Computer.shared.start(Backup.fromJsonString, dlFile);
|
||||
final restore = await dlBak.restore();
|
||||
switch (restore) {
|
||||
case true:
|
||||
_logger.info('Restore from ${dlBak.lastModTime} success');
|
||||
break;
|
||||
case false:
|
||||
await Backup.backup();
|
||||
final uploadResult = await upload(relativePath: Paths.bakName);
|
||||
if (uploadResult != null) {
|
||||
_logger.warning('Upload backup failed: $uploadResult');
|
||||
} else {
|
||||
_logger.info('Upload backup success');
|
||||
}
|
||||
break;
|
||||
case null:
|
||||
_logger.info('Skip sync');
|
||||
break;
|
||||
await dlBak.restore();
|
||||
await Backup.backup();
|
||||
final uploadResult = await upload(relativePath: Paths.bakName);
|
||||
if (uploadResult != null) {
|
||||
_logger.warning('Upload backup failed: $uploadResult');
|
||||
} else {
|
||||
_logger.info('Upload backup success');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,18 +117,8 @@ abstract final class Webdav {
|
||||
await backup();
|
||||
return;
|
||||
}
|
||||
final restore = await dlFile.restore();
|
||||
switch (restore) {
|
||||
case true:
|
||||
_logger.info('Restore from ${dlFile.lastModTime} success');
|
||||
break;
|
||||
case false:
|
||||
await backup();
|
||||
break;
|
||||
case null:
|
||||
_logger.info('Skip sync');
|
||||
break;
|
||||
}
|
||||
await dlFile.restore();
|
||||
await backup();
|
||||
}
|
||||
|
||||
/// Create a local backup and upload it to WebDAV
|
||||
|
||||
Reference in New Issue
Block a user