cleanupOrphanedFiles static method
Smart cleanup: removes orphaned files immediately, keeps potential resume files
Implementation
static Future<void> cleanupOrphanedFiles({
required List<String> protectedFiles,
List<String>? supportedExtensions,
bool enableResumeDetection = false,
}) async {
try {
final directory = await getApplicationDocumentsDirectory();
final extensions = supportedExtensions ?? _supportedExtensions;
// Get all supported model files in directory
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;
// NEVER delete files that are protected
if (protectedFiles.contains(fileName)) {
debugPrint('Keeping protected file: $fileName');
continue;
}
// Check if this could be a partial download worth keeping for resume
if (enableResumeDetection && await _shouldKeepForResume(file)) {
debugPrint('Keeping potential partial download: $fileName');
continue;
}
// File is not protected and not resumable → delete immediately
debugPrint('Removing orphaned file: $fileName');
await file.delete();
}
} catch (e) {
debugPrint('Failed to cleanup orphaned files: $e');
}
}