lookupImageNameForHash function
Implementation
Future<String?> lookupImageNameForHash(
String hash, {
String? scope,
}) async {
try {
await _ensureCollectionDirExists(IMAGE_INDEX_COLLECTION);
} catch (_) {}
Future<String?> tryScope(String? s) async {
final scopedId = _imageIndexDocId(hash, scope: s);
final scoped = await imageIndexCollection.doc(scopedId).get();
final scopedName = scoped?['storedName']?.toString();
if (scopedName != null && scopedName.isNotEmpty) return scopedName;
return null;
}
final s = _canonicalizeScope((scope ?? '').trim());
final direct = await tryScope(s);
if (direct != null) return direct;
// legacy lookup for old "null" folders
final legacyScope = _legacyScopeForCanonical(s);
if (legacyScope != s) {
final legacy = await tryScope(legacyScope);
if (legacy != null) return legacy;
}
// fallback: global entry (no scope)
final globalId = _imageIndexDocId(hash, scope: '');
final global = await imageIndexCollection.doc(globalId).get();
final globalName = global?['storedName']?.toString();
if (globalName != null && globalName.isNotEmpty) return globalName;
return null;
}