Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

fix(sync): align webdav history writes with compatible replace flow

Prediditc01088c3cccf
diff --git a/lib/services/sync/webdav.dart b/lib/services/sync/webdav.dart
index 5ebe50f5..28b0cdb8 100644
--- a/lib/services/sync/webdav.dart
+++ b/lib/services/sync/webdav.dart
@@ -7,6 +7,7 @@ import 'package:kazumi/services/logging/logger.dart';
import 'package:kazumi/modules/collect/collect_module.dart';
import 'package:kazumi/modules/collect/collect_change_module.dart';
import 'package:kazumi/services/sync/history_sync_service.dart';
+import 'package:kazumi/services/sync/webdav_remote_file_commit.dart';
import 'package:kazumi/utils/async_serial_queue.dart';
import 'package:kazumi/utils/async_single_flight.dart';
@@ -28,6 +29,8 @@ class WebDav {
final AsyncSingleFlight<void> _historySyncSingleFlight =
AsyncSingleFlight<void>();
final AsyncSerialQueue _webDavOperationQueue = AsyncSerialQueue();
+ final WebDavRemoteFileCommitter _remoteFileCommitter =
+ const WebDavRemoteFileCommitter();
bool get isHistorySyncing => _historySyncSingleFlight.isRunning;
@@ -42,7 +45,6 @@ class WebDav {
webDavUsername = GStorage.getSetting(SettingsKeys.webDavUsername);
webDavPassword = GStorage.getSetting(SettingsKeys.webDavPassword);
if (webDavURL.isEmpty) {
- //KazumiLogger().log(Level.warning, 'WebDAV URL is not set');
throw Exception('请先填写WebDAV URL');
}
client = webdav.newClient(
@@ -75,16 +77,11 @@ class WebDav {
final tempFilePath = '${webDavLocalTempDirectory.path}/$boxName.tmp';
final webDavPath = '$_syncRootPath/$boxName.tmp';
await File(localFilePath).copy(tempFilePath);
- try {
- await client.remove('$webDavPath.cache');
- } catch (_) {}
- await client.writeFromFile(tempFilePath, '$webDavPath.cache');
- try {
- await client.remove(webDavPath);
- } catch (_) {
- KazumiLogger().w('WebDav: former backup file not exist');
- }
- await client.rename('$webDavPath.cache', webDavPath, true);
+ await _publishRemoteFile(
+ sourceFilePath: tempFilePath,
+ destinationPath: webDavPath,
+ temporaryPath: '$webDavPath.cache',
+ );
try {
await File(tempFilePath).delete();
} catch (_) {}
@@ -424,7 +421,7 @@ class WebDav {
required String deviceId,
}) {
return _publishRemoteFile(
- sourceFile: snapshotFile,
+ sourceFilePath: snapshotFile.path,
destinationPath: _historySnapshotPath,
temporaryPath: '$_historySnapshotPath.$deviceId.cache',
);
@@ -436,29 +433,28 @@ class WebDav {
}) {
final destinationPath = '$_historyChangesPath/$deviceId.jsonl';
return _publishRemoteFile(
- sourceFile: sourceFile,
+ sourceFilePath: sourceFile.path,
destinationPath: destinationPath,
temporaryPath: '$destinationPath.cache',
);
}
Future<void> _publishRemoteFile({
- required File sourceFile,
+ required String sourceFilePath,
required String destinationPath,
required String temporaryPath,
}) async {
- try {
- await client.remove(temporaryPath);
- } catch (_) {}
- try {
- await client.writeFromFile(sourceFile.path, temporaryPath);
- await client.rename(temporaryPath, destinationPath, true);
- } catch (_) {
- try {
- await client.remove(temporaryPath);
- } catch (_) {}
- rethrow;
- }
+ await _remoteFileCommitter.replaceFile(
+ sourceFilePath: sourceFilePath,
+ temporaryPath: temporaryPath,
+ destinationPath: destinationPath,
+ remove: (path) => client.remove(path),
+ uploadFromFile: (sourceFilePath, remotePath) =>
+ client.writeFromFile(sourceFilePath, remotePath),
+ rename: (sourcePath, targetPath) =>
+ client.rename(sourcePath, targetPath, true),
+ exists: _remoteEntryExists,
+ );
}
Future<void> _removeDeviceHistoryChanges(String deviceId) async {
diff --git a/lib/services/sync/webdav_remote_file_commit.dart b/lib/services/sync/webdav_remote_file_commit.dart
new file mode 100644
index 00000000..84c9ea29
--- /dev/null
+++ b/lib/services/sync/webdav_remote_file_commit.dart
@@ -0,0 +1,60 @@
+typedef WebDavRemoveRemoteFile = Future<void> Function(String path);
+typedef WebDavUploadRemoteFile = Future<void> Function(
+ String sourceFilePath,
+ String remotePath,
+);
+typedef WebDavRenameRemoteFile = Future<void> Function(
+ String sourcePath,
+ String destinationPath,
+);
+typedef WebDavRemoteEntryExists = Future<bool> Function(String path);
+
+class WebDavRemoteFileCommitter {
+ const WebDavRemoteFileCommitter();
+
+ Future<void> replaceFile({
+ required String sourceFilePath,
+ required String temporaryPath,
+ required String destinationPath,
+ required WebDavRemoveRemoteFile remove,
+ required WebDavUploadRemoteFile uploadFromFile,
+ required WebDavRenameRemoteFile rename,
+ required WebDavRemoteEntryExists exists,
+ }) async {
+ await _removeIfExists(
+ temporaryPath,
+ remove: remove,
+ exists: exists,
+ );
+ try {
+ await uploadFromFile(sourceFilePath, temporaryPath);
+ await _removeIfExists(
+ destinationPath,
+ remove: remove,
+ exists: exists,
+ );
+ await rename(temporaryPath, destinationPath);
+ } catch (_) {
+ await _removeIfExists(
+ temporaryPath,
+ remove: remove,
+ exists: exists,
+ );
+ rethrow;
+ }
+ }
+
+ Future<void> _removeIfExists(
+ String path, {
+ required WebDavRemoveRemoteFile remove,
+ required WebDavRemoteEntryExists exists,
+ }) async {
+ try {
+ await remove(path);
+ } catch (_) {
+ if (await exists(path)) {
+ rethrow;
+ }
+ }
+ }
+}