refresh cloud file index on startup

This commit is contained in:
ngfchl
2026-07-18 22:08:17 +08:00
parent 58e324ba22
commit b5e4b0e441
6 changed files with 173 additions and 8 deletions
+81 -1
View File
@@ -120,6 +120,8 @@ class MediaLibraryNotifier extends StateNotifier<MediaLibraryState> {
bool _loaded = false;
bool _cancelScan = false;
bool _cancelDetailSync = false;
bool _refreshingCloudIndex = false;
Timer? _cloudIndexTimer;
MediaLibraryNotifier() : super(const MediaLibraryState());
@@ -151,6 +153,7 @@ class MediaLibraryNotifier extends StateNotifier<MediaLibraryState> {
if (selectedID != null) {
unawaited(_hydrateMissingArtwork(selectedID, items));
}
unawaited(refreshGlobalCloudIndex());
} catch (e) {
state = state.copyWith(errorMessage: e.toString());
} finally {
@@ -430,7 +433,8 @@ class MediaLibraryNotifier extends StateNotifier<MediaLibraryState> {
if (forceRemote) {
_appendScanLog('正在获取云盘全量文件索引…');
final globalFiles = await _allGlobalRemoteFiles();
await refreshGlobalCloudIndex(force: true);
final globalFiles = await _cachedGlobalFiles();
final seen = <String>{};
final mediaFiles = <CloudFile>[];
for (final source in library.sources) {
@@ -544,6 +548,76 @@ class MediaLibraryNotifier extends StateNotifier<MediaLibraryState> {
_appendScanLog('[同步识别][DEBUG] 已请求取消同步识别');
}
Future<void> refreshGlobalCloudIndex({bool force = false}) async {
if (_api == null || _refreshingCloudIndex) return;
final minutes =
(int.tryParse(
StorageManager.get<String>(
StorageKeys.cloudIndexRefreshMinutes,
) ??
'30',
) ??
30)
.clamp(5, 1440);
final lastUpdated = int.tryParse(
StorageManager.get<String>(StorageKeys.cloudIndexLastUpdatedAt) ?? '',
);
if (!force && lastUpdated != null) {
final elapsed = DateTime.now().difference(
DateTime.fromMillisecondsSinceEpoch(lastUpdated),
);
if (elapsed < Duration(minutes: minutes)) {
_scheduleCloudIndexRefresh(minutes);
return;
}
}
_refreshingCloudIndex = true;
try {
final files = await _allGlobalRemoteFiles();
final folders = <String?, List<CloudFile>>{};
for (final file in files) {
(folders[file.parentID] ??= []).add(file);
}
await FileMetadataCache.cacheFolderChildrenBatch(folders);
await StorageManager.set(
StorageKeys.cloudIndexLastUpdatedAt,
DateTime.now().millisecondsSinceEpoch.toString(),
);
_appendScanLog('[云盘索引] 已刷新 ${files.length} 个文件与目录缓存');
} catch (error) {
_appendScanLog('[云盘索引] 刷新失败:$error', isError: true);
} finally {
_refreshingCloudIndex = false;
_scheduleCloudIndexRefresh(minutes);
}
}
void _scheduleCloudIndexRefresh(int minutes) {
_cloudIndexTimer?.cancel();
_cloudIndexTimer = Timer(Duration(minutes: minutes), () {
unawaited(refreshGlobalCloudIndex(force: true));
});
}
void updateCloudIndexRefreshSchedule() {
final minutes =
(int.tryParse(
StorageManager.get<String>(
StorageKeys.cloudIndexRefreshMinutes,
) ??
'30',
) ??
30)
.clamp(5, 1440);
_scheduleCloudIndexRefresh(minutes);
}
@override
void dispose() {
_cloudIndexTimer?.cancel();
super.dispose();
}
void _appendScanLog(String message, {bool isError = false}) {
final logs = [
...state.scanLogs,
@@ -1494,6 +1568,12 @@ class MediaLibraryNotifier extends StateNotifier<MediaLibraryState> {
return values;
}
Future<List<CloudFile>> _cachedGlobalFiles() async {
final values = await FileMetadataCache.allCachedFolderChildren();
if (values.isEmpty) return _allGlobalRemoteFiles();
return values;
}
Future<List<CloudFile>> _allGlobalRemoteFilesByType({int? resType}) async {
const pageSize = 10000;
final values = <CloudFile>[];