fix: synchronize folder caches after file changes
This commit is contained in:
@@ -78,6 +78,29 @@ class FileMetadataCache {
|
||||
});
|
||||
}
|
||||
|
||||
static Future<void> removeFilesFromAllFolders(Iterable<String> fileIDs) {
|
||||
final removed = fileIDs.toSet();
|
||||
if (removed.isEmpty) return Future.value();
|
||||
return _enqueue(() async {
|
||||
final folders = _map(StorageKeys.folderChildrenIndex);
|
||||
for (final entry in folders.entries.toList()) {
|
||||
final snapshot = entry.value;
|
||||
if (snapshot is! Map || snapshot['children'] is! List) continue;
|
||||
final children = (snapshot['children'] as List)
|
||||
.whereType<Map>()
|
||||
.where((child) => !removed.contains(_fileID(child)))
|
||||
.toList();
|
||||
final original = snapshot['children'] as List;
|
||||
if (children.length == original.length) continue;
|
||||
folders[entry.key] = {
|
||||
'childIds': children.map(_fileID).whereType<String>().toList(),
|
||||
'children': children,
|
||||
};
|
||||
}
|
||||
await StorageManager.set(StorageKeys.folderChildrenIndex, folders);
|
||||
});
|
||||
}
|
||||
|
||||
static List<CloudFile>? folderChildren(String? folderID) {
|
||||
final folders = _map(StorageKeys.folderChildrenIndex);
|
||||
final entry = folders[_folderKey(folderID)];
|
||||
@@ -125,6 +148,16 @@ class FileMetadataCache {
|
||||
}
|
||||
}
|
||||
|
||||
static String? _fileID(Map value) {
|
||||
for (final key in const ['id', 'fileId', 'file_id', 'resId', 'res_id']) {
|
||||
final candidate = value[key];
|
||||
if (candidate != null && candidate.toString().isNotEmpty) {
|
||||
return candidate.toString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static Future<void> _enqueue(Future<void> Function() operation) {
|
||||
_writes = _writes.then((_) => operation());
|
||||
return _writes;
|
||||
|
||||
Reference in New Issue
Block a user