scanAssets method

Future<FileScanResult> scanAssets(
  1. List<String> assetPaths, {
  2. bool recursive = true,
  3. Set<String>? allowedExtensions,
})

Implementation

Future<FileScanResult> scanAssets(
  List<String> assetPaths, {
  bool recursive = true,
  Set<String>? allowedExtensions,
}) async {
  final assets = <AssetInfo>[];
  final errors = <String, String>{};
  final allowedExts = allowedExtensions ?? _supportedTypes.keys.toSet();

  for (final assetPath in assetPaths) {
    final directory = Directory(assetPath);

    if (!await directory.exists()) {
      errors[assetPath] = 'Directory does not exist';
      continue;
    }

    try {
      await for (final entity in directory.list(recursive: recursive)) {
        if (entity is! File) continue;

        final extension = path.extension(entity.path).toLowerCase();
        if (!allowedExts.contains(extension)) continue;

        try {
          final stat = await entity.stat();
          assets.add(AssetInfo(
            name: path.basename(entity.path),
            path: entity.path,
            size: stat.size,
            type: _supportedTypes[extension] ?? extension.substring(1),
            lastModified: stat.modified,
          ));
        } catch (e) {
          errors[entity.path] = 'Failed to read file: $e';
        }
      }
    } catch (e) {
      errors[assetPath] = 'Failed to scan directory: $e';
    }
  }

  return FileScanResult(
    assets: assets,
    errors: errors,
  );
}