fix(flutter): cache usage display reflects actual disk, not just index
The Storage card has been showing "0 B" for users who only stream (never explicitly pin or download an album). usageBytes() summed SUM(size_bytes) from audio_cache_index — but the streaming path through audio_handler writes files via LockCachingAudioSource without ever inserting an index row, so the index undercounts (often to zero) for normal use. Walk the cache directory instead. Catches everything on disk: manually pinned tracks (registered in the index), stream-cached tracks (LockCaching), partial downloads. Falls back gracefully when a file is racing against concurrent writes / deletes. Eviction still operates on the index (it needs the source/recency metadata to pick eviction order). Stream-cached files aren't subject to eviction today — separate problem; addressed when we wire a download-complete hook from LockCaching back into the index.
This commit is contained in:
+18
-6
@@ -96,13 +96,25 @@ class AudioCacheManager {
|
||||
.go();
|
||||
}
|
||||
|
||||
/// Total bytes used by the cache.
|
||||
/// Total bytes used by the cache. Walks the cache directory directly
|
||||
/// instead of summing the index, because the streaming-as-you-play
|
||||
/// path (audio_handler's LockCachingAudioSource) writes files
|
||||
/// without registering an index row. The index sum would always
|
||||
/// understate (often to zero) for users who only stream.
|
||||
Future<int> usageBytes() async {
|
||||
final result = await _db.customSelect(
|
||||
'SELECT COALESCE(SUM(size_bytes), 0) AS total FROM audio_cache_index',
|
||||
readsFrom: {_db.audioCacheIndex},
|
||||
).getSingle();
|
||||
return result.read<int>('total');
|
||||
final dir = Directory(await _tracksDir());
|
||||
if (!await dir.exists()) return 0;
|
||||
var total = 0;
|
||||
await for (final entity in dir.list(followLinks: false)) {
|
||||
if (entity is File) {
|
||||
try {
|
||||
total += await entity.length();
|
||||
} catch (_) {
|
||||
// Race against concurrent writes / deletes — just skip.
|
||||
}
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
/// Evicts files until usage ≤ targetBytes. Eviction order:
|
||||
|
||||
Reference in New Issue
Block a user