[mob][photos] Caching fix (#5531)

## Description

Fix incorrect cache reading for incorrect utf8 encoding for memories and
magic cache.

## Tests

Tested in debug mode on my pixel phone.
This commit is contained in:
Laurens Priem
2025-04-08 14:24:41 +05:30
committed by GitHub
2 changed files with 27 additions and 10 deletions

View File

@@ -305,15 +305,22 @@ class MagicCacheService {
_logger.info("No magic cache found");
return [];
}
final jsonString = file.readAsStringSync();
return MagicCache.decodeJsonToList(jsonString);
try {
final bytes = await file.readAsBytes();
final jsonString = String.fromCharCodes(bytes);
return MagicCache.decodeJsonToList(jsonString);
} catch (e, s) {
_logger.severe("Error reading or decoding cache file", e, s);
await file.delete();
rethrow;
}
}
Future<void> clearMagicCache() async {
final file = File(await _getCachePath());
if (file.existsSync()) {
await file.delete();
}
if (file.existsSync()) {
await file.delete();
}
}
Future<List<GenericSearchResult>> getMagicGenericSearchResult(

View File

@@ -428,15 +428,25 @@ class MemoriesCacheService {
allFileIdsToFile[file.uploadedFileID!] = file;
}
}
final jsonString = file.readAsStringSync();
return MemoriesCache.decodeFromJsonString(jsonString, allFileIdsToFile);
try {
final bytes = await file.readAsBytes();
final jsonString = String.fromCharCodes(bytes);
final cache =
MemoriesCache.decodeFromJsonString(jsonString, allFileIdsToFile);
_logger.info("Reading memories cache result from disk done");
return cache;
} catch (e, s) {
_logger.severe("Error reading or decoding cache file", e, s);
await file.delete();
return null;
}
}
Future<void> clearMemoriesCache() async {
final file = File(await _getCachePath());
if (file.existsSync()) {
await file.delete();
}
if (file.existsSync()) {
await file.delete();
}
_cachedMemories = null;
}
}