getOrphanedFiles static method

Future<List<OrphanedFileInfo>> getOrphanedFiles({
  1. List<String>? protectedFiles,
  2. List<String>? supportedExtensions,
})

Get information about orphaned files without deleting them

⚠️ This method only returns information, it does NOT delete files. Call cleanupOrphanedFiles() explicitly to delete them.

Implementation

static Future<List<OrphanedFileInfo>> getOrphanedFiles({
  List<String>? protectedFiles,
  List<String>? supportedExtensions,
}) async {
  final directory = await getApplicationDocumentsDirectory();
  final extensions = supportedExtensions ?? ModelFileSystemManager.supportedExtensions;
  final protected = protectedFiles ?? [];

  final orphaned = <OrphanedFileInfo>[];

  final files = directory
      .listSync()
      .whereType<File>()
      .where((file) => extensions.any((ext) => file.path.endsWith(ext)))
      .toList();

  for (final file in files) {
    final fileName = file.path.split('/').last;

    if (protected.contains(fileName)) {
      continue;
    }

    // Check if has active task
    final hasTask = await _hasActiveDownloadTask(fileName);
    if (hasTask) {
      continue;
    }

    // This file is orphaned
    final stat = await file.stat();
    orphaned.add(OrphanedFileInfo(
      filename: fileName,
      path: file.path,
      sizeBytes: stat.size,
      lastModified: stat.modified,
    ));
  }

  return orphaned;
}