Stream list()

in pkg/fake_gcloud/lib/mem_storage.dart [251:298]


  Stream<BucketEntry> list({String? prefix, String? delimiter}) async* {
    _validateObjectName(prefix, allowNull: true);
    prefix ??= '';
    delimiter ??= '/';
    final isDirPrefix =
        prefix.isEmpty || (delimiter.isNotEmpty && prefix.endsWith(delimiter));
    final segments = <String>{};
    for (String name in _files.keys) {
      bool matchesPrefix() {
        // without prefix, return everything
        if (prefix!.isEmpty) return true;
        // exclude everything that does not match the prefix
        if (!name.startsWith(prefix)) return false;
        // prefix does not end with directory separator, only files are matched
        if (!isDirPrefix &&
            delimiter!.isNotEmpty &&
            name.substring(prefix.length).contains(delimiter)) {
          return false;
        }
        return true;
      }

      if (matchesPrefix()) {
        final lastPartOfName = name.substring(prefix.length);
        final subDirSegments = delimiter.isEmpty
            ? [lastPartOfName]
            : lastPartOfName.split(delimiter);
        final isSubDirMatch = subDirSegments.length > 1;

        if (isDirPrefix && isSubDirMatch) {
          // extract path
          segments.add(subDirSegments.first);
        } else if (isDirPrefix && !isSubDirMatch) {
          // directory match
          yield _BucketEntry(name, true);
        } else if (!isDirPrefix && isSubDirMatch) {
          // ignore prefix match
        } else if (!isDirPrefix && !isSubDirMatch) {
          // file prefix match
          yield _BucketEntry(name, true);
        }
      }
    }

    for (final s in segments) {
      yield _BucketEntry('$prefix$s$delimiter', false);
    }
  }