resolveImageFileByHash function

Future<File?> resolveImageFileByHash(
  1. String hash, {
  2. String? scope,
})

Tries to resolve an existing on-disk image file for a given hash/name. Supports scoped paths (<scope>/<hash>) and legacy naming.

Implementation

Future<File?> resolveImageFileByHash(
  String hash, {
  String? scope,
}) async {
  final isPath = hash.contains('/');
  final names = <String>[];

  void add(String n) {
    if (n.isEmpty) return;
    names.add(n);
  }

  if (!isPath && scope != null && scope.isNotEmpty) {
    add('$scope/$hash');
  }
  add(hash);

  for (final name in names) {
    try {
      final f = await localFile(name);
      if (f.existsSync()) return f;
    } catch (_) {}
  }
  return null;
}