Skip to content

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

fix : WebDAV collectibles handle missing files and delete actions (#1268)

floating1424833d8bb9da0
diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart
index d81c7f13..09be9dc6 100644
--- a/lib/utils/storage.dart
+++ b/lib/utils/storage.dart
@@ -130,18 +130,34 @@ class GStorage {
// Process local changes
for (var change in newLocalChanges) {
- // Lookup by bangumiID
- // CollectiblesChange only stores the bangumiID, so we need to lookup the corresponding CollectedBangumi.
- final changedBangumiID = change.bangumiID.toString();
- for (var localCollect in localCollectibles) {
- if (localCollect.bangumiItem.id.toString() == changedBangumiID) {
- if (change.action == 1) {
- // Action 1: add
- final exists = remoteCollectibles
- .any((b) => b.bangumiItem.id == localCollect.bangumiItem.id);
- if (!exists) {
- remoteCollectibles.add(localCollect);
- } else {
+ // For delete action, we don't need to look up the local collectible.
+ // We can directly remove the item from the remote list.
+ if (change.action == 3) {
+ // Action 3: delete
+ remoteCollectibles.removeWhere(
+ (b) => b.bangumiItem.id == change.bangumiID);
+ } else {
+ // For add/update, we still need to look up the local collectible.
+ final changedBangumiID = change.bangumiID.toString();
+ for (var localCollect in localCollectibles) {
+ if (localCollect.bangumiItem.id.toString() == changedBangumiID) {
+ if (change.action == 1) {
+ // Action 1: add
+ final exists = remoteCollectibles
+ .any((b) => b.bangumiItem.id == localCollect.bangumiItem.id);
+ if (!exists) {
+ remoteCollectibles.add(localCollect);
+ } else {
+ final index = remoteCollectibles.indexWhere(
+ (b) => b.bangumiItem.id == localCollect.bangumiItem.id);
+ localCollect.type = change.type;
+ if (index != -1) {
+ // Update the entry with local data.
+ remoteCollectibles[index] = localCollect;
+ }
+ }
+ } else if (change.action == 2) {
+ // Action 2: update
final index = remoteCollectibles.indexWhere(
(b) => b.bangumiItem.id == localCollect.bangumiItem.id);
localCollect.type = change.type;
@@ -150,24 +166,10 @@ class GStorage {
remoteCollectibles[index] = localCollect;
}
}
- } else if (change.action == 2) {
- // Action 2: update
- final index = remoteCollectibles.indexWhere(
- (b) => b.bangumiItem.id == localCollect.bangumiItem.id);
- localCollect.type = change.type;
- if (index != -1) {
- // Update the entry with local data.
- remoteCollectibles[index] = localCollect;
- }
- } else if (change.action == 3) {
- // Action 3: delete
- remoteCollectibles.removeWhere(
- (b) => b.bangumiItem.id == localCollect.bangumiItem.id);
+ break;
}
- break;
}
}
- // If the corresponding CollectedBangumi does not exist locally, skip the change.
}
// merge local changes with remote changes
diff --git a/lib/utils/webdav.dart b/lib/utils/webdav.dart
index 48437006..d7c6eb25 100644
--- a/lib/utils/webdav.dart
+++ b/lib/utils/webdav.dart
@@ -109,9 +109,14 @@ class WebDav {
// don't try muliti thread update here
// some webdav server may not support muliti thread write
// you will get 423 locked error
- await update('collectibles');
- if (GStorage.collectChanges.isNotEmpty) {
- await update('collectchanges');
+ try {
+ await update('collectibles');
+ if (GStorage.collectChanges.isNotEmpty) {
+ await update('collectchanges');
+ }
+ } catch (e) {
+ KazumiLogger().log(Level.error, 'webDav update collectibles failed $e');
+ rethrow;
}
}
@@ -151,30 +156,41 @@ class WebDav {
List<CollectedBangumi> remoteCollectibles = [];
List<CollectedBangumiChange> remoteChanges = [];
- // muliti thread download
- Future<void> collectiblesFuture = download('collectibles').catchError((e) {
- KazumiLogger().log(Level.error, 'webDav download collectibles failed $e');
- throw Exception('webDav download collectibles failed');
- });
- Future<void> changesFuture = download('collectchanges').catchError((e) {
- KazumiLogger()
- .log(Level.error, 'webDav download collect changes failed $e');
- throw Exception('webDav download collect changes failed');
- });
- await Future.wait([collectiblesFuture, changesFuture]);
-
-
- // we should block download changes when download collectibles failed
- // download changes failed but collectibles success means remote files broken or newwork error
- // we should force push local collectibles to remote to fix it
+ final files = await client.readDir('/kazumiSync');
+ final collectiblesExists = files.any((file) => file.name == 'collectibles.tmp');
+ final changesExists = files.any((file) => file.name == 'collectchanges.tmp');
+ if (!collectiblesExists && !changesExists) {
+ await updateCollectibles();
+ return;
+ }
+
+ List<Future<void>> downloadFutures = [];
+ if (collectiblesExists) {
+ downloadFutures.add(download('collectibles').catchError((e) {
+ KazumiLogger().log(Level.error, 'webDav download collectibles failed $e');
+ throw Exception('webDav download collectibles failed');
+ }));
+ }
+ if (changesExists) {
+ downloadFutures.add(download('collectchanges').catchError((e) {
+ KazumiLogger().log(Level.error, 'webDav download collectchanges failed $e');
+ throw Exception('webDav download collectchanges failed');
+ }));
+ }
+ if (downloadFutures.isNotEmpty) {
+ await Future.wait(downloadFutures);
+ }
try {
- remoteCollectibles = await GStorage.getCollectiblesFromFile(
+ if (collectiblesExists) {
+ remoteCollectibles = await GStorage.getCollectiblesFromFile(
'${webDavLocalTempDirectory.path}/collectibles.tmp');
- remoteChanges = await GStorage.getCollectChangesFromFile(
+ }
+ if (changesExists) {
+ remoteChanges = await GStorage.getCollectChangesFromFile(
'${webDavLocalTempDirectory.path}/collectchanges.tmp');
+ }
} catch (e) {
- KazumiLogger()
- .log(Level.error, 'webDav get collectibles from file failed $e');
+ KazumiLogger().log(Level.error, 'webDav get collectibles failed: $e');
throw Exception('webDav get collectibles from file failed');
}
if (remoteChanges.isNotEmpty || remoteCollectibles.isNotEmpty) {