listFiles method

  1. @override
Future<List<CloudFile>> listFiles({
  1. required String path,
  2. bool recursive = false,
})
override

Lists all files and directories at the specified path.

Implementation

@override
Future<List<CloudFile>> listFiles({
  required String path,
  bool recursive = false,
}) async {
  // The package fetches all file metadata at once.
  final allFiles = await _icloudSync.gather(containerId: _containerId);
  final List<CloudFile> results = [];
  // Normalize the directory path for consistent comparison.
  final normalizedPath =
      path == '/' ? '' : path.replaceAll(RegExp(r'/$'), '');
  for (final icloudFile in allFiles) {
    final itemPath = icloudFile.relativePath;
    if (recursive) {
      // For recursive, check if the item path starts with the directory path.
      if (itemPath.startsWith(normalizedPath)) {
        results.add(_mapToCloudFile(icloudFile));
      }
    } else {
      // For non-recursive, check if the item is a direct child.
      // The parent directory of the item should be the same as the target path.
      final parentDir = p.dirname(itemPath);
      final rootEquivalent = parentDir == '.' &&
          (normalizedPath.isEmpty || normalizedPath == '/');
      if (parentDir == normalizedPath || rootEquivalent) {
        results.add(_mapToCloudFile(icloudFile));
      }
    }
  }
  return results;
}