performCleanup method
Performs cleanup of orphaned files
Implementation
@override
Future<void> performCleanup() async {
await _ensureInitialized();
gemmaLog('UnifiedModelManager: Performing cleanup');
try {
// 1. Get protected files from ModelRepository
final protectedFiles = await _getAllProtectedFiles();
final downloader = FileDownloader();
// 2. Cancel every task in the group FIRST — cancellation deletes paused
// temp files (reset() only clears records), so this must precede both
// reset and the fragment sweep (#383/#5).
final groupTasks = await downloader.allTasks(
group: SmartDownloader.downloadGroup,
includeTasksWaitingToRetry: true,
);
await downloader.cancelTasksWithIds(
groupTasks.map((t) => t.taskId).toList(),
);
// 3. Reset residual records.
await downloader.reset(group: SmartDownloader.downloadGroup);
// 4. Filesystem cleanup last — now only truly-orphaned fragments remain.
await ModelFileSystemManager.cleanupOrphanedFiles(
protectedFiles: protectedFiles,
enableResumeDetection: true,
);
gemmaLog('UnifiedModelManager: Cleanup completed');
} catch (e) {
gemmaLog('UnifiedModelManager: Cleanup failed: $e');
// Don't rethrow - cleanup failures should not break the app
}
}