-
-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3524d92
commit 2dc86a9
Showing
9 changed files
with
385 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:toolbox/data/model/app/backup.dart'; | ||
import 'package:toolbox/data/model/app/error.dart'; | ||
import 'package:toolbox/data/res/logger.dart'; | ||
import 'package:toolbox/data/res/path.dart'; | ||
import 'package:toolbox/data/res/store.dart'; | ||
// ignore: implementation_imports | ||
import 'package:webdav_client/src/client.dart'; | ||
|
||
abstract final class Webdav { | ||
static var _client = WebdavClient( | ||
url: Stores.setting.webdavUrl.fetch(), | ||
user: Stores.setting.webdavUser.fetch(), | ||
pwd: Stores.setting.webdavPwd.fetch(), | ||
); | ||
|
||
static Future<WebdavErr?> upload({ | ||
required String relativePath, | ||
String? localPath, | ||
}) async { | ||
try { | ||
await _client.writeFile( | ||
localPath ?? '${await Paths.doc}/$relativePath', | ||
relativePath, | ||
); | ||
} catch (e, s) { | ||
Loggers.app.warning('Webdav upload failed', e, s); | ||
return WebdavErr(type: WebdavErrType.generic, message: '$e'); | ||
} | ||
return null; | ||
} | ||
|
||
static Future<WebdavErr?> delete(String relativePath) async { | ||
try { | ||
await _client.remove(relativePath); | ||
} catch (e, s) { | ||
Loggers.app.warning('Webdav delete failed', e, s); | ||
return WebdavErr(type: WebdavErrType.generic, message: '$e'); | ||
} | ||
return null; | ||
} | ||
|
||
static Future<WebdavErr?> download({ | ||
required String relativePath, | ||
String? localPath, | ||
}) async { | ||
try { | ||
await _client.readFile( | ||
relativePath, | ||
localPath ?? '${await Paths.doc}/$relativePath', | ||
); | ||
} catch (e, s) { | ||
Loggers.app.warning('Webdav download failed', e, s); | ||
return WebdavErr(type: WebdavErrType.generic, message: '$e'); | ||
} | ||
return null; | ||
} | ||
|
||
static void changeClient(String url, String user, String pwd) { | ||
_client = WebdavClient(url: url, user: user, pwd: pwd); | ||
} | ||
|
||
static Future<void> sync() async { | ||
try { | ||
final result = await download(relativePath: Paths.bakName); | ||
if (result != null) { | ||
Loggers.app.warning('Download backup failed: $result'); | ||
return; | ||
} | ||
} catch (e, s) { | ||
Loggers.app.warning('Download backup failed', e, s); | ||
} | ||
final dlFile = await File(await Paths.bak).readAsString(); | ||
final dlBak = await compute(Backup.fromJsonString, dlFile); | ||
final restore = await dlBak.restore(); | ||
switch (restore) { | ||
case true: | ||
Loggers.app.info('Restore from iCloud (${dlBak.lastModTime}) success'); | ||
break; | ||
case false: | ||
await Backup.backup(); | ||
final uploadResult = await upload(relativePath: Paths.bakName); | ||
if (uploadResult != null) { | ||
Loggers.app.warning('Upload iCloud backup failed: $uploadResult'); | ||
} else { | ||
Loggers.app.info('Upload iCloud backup success'); | ||
} | ||
break; | ||
case null: | ||
Loggers.app.info('Skip iCloud sync'); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
abstract class RemoteStorage { | ||
Future<Error> upload({ | ||
required String relativePath, | ||
String? localPath | ||
}); | ||
|
||
Future<Error> download({ | ||
required String relativePath, | ||
String? localPath | ||
}); | ||
|
||
Future<Error> delete(String relativePath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.