diff --git a/src/api/files.rs b/src/api/files.rs index c0f50126..0d5430b4 100644 --- a/src/api/files.rs +++ b/src/api/files.rs @@ -1297,10 +1297,28 @@ struct ReScanResponse { process_command_id: Option, } -fn delete_output_files(uuid: &str) -> u64 { +async fn delete_output_files(uuid: &str, pool: &sqlx::PgPool, pre_resolved_dir: Option<&str>) -> u64 { let mut deleted_count = 0u64; - // Check multiple output directories - let output_dirs = [config::OUTPUT_DIR.to_string()]; + + // Build output dirs: global OUTPUT_DIR + pre-resolved per-library dir + all upload_paths + let mut output_dirs: Vec = vec![config::OUTPUT_DIR.to_string()]; + if let Some(dir) = pre_resolved_dir { + if !dir.is_empty() && !output_dirs.contains(&dir.to_string()) { + output_dirs.push(dir.to_string()); + } + } + // All enabled upload_paths output paths (covers all libraries regardless of videos row) + if let Ok(rows) = sqlx::query_scalar::<_, String>( + "SELECT output_path FROM upload_paths WHERE enabled = true AND output_path IS NOT NULL" + ).fetch_all(pool).await { + for dir in rows { + if !dir.is_empty() && !output_dirs.contains(&dir) { + output_dirs.push(dir); + } + } + } + + tracing::info!("[UNREGISTER] Scanning output dirs for {}: {:?}", uuid, output_dirs); for output_dir in &output_dirs { if let Ok(entries) = std::fs::read_dir(output_dir) { @@ -1659,6 +1677,11 @@ async fn re_scan( /// Internal unregister function - can be called from both API and register pub(crate) async fn unregister_internal(db: &PostgresDb, uuid: &str) -> Result<(), StatusCode> { + // Resolve per-library output dir BEFORE deleting the videos row + // (get_output_dir_for_file JOINs videos → must run while the row still exists) + let resolved_output_dir = crate::core::config::get_output_dir_for_file(uuid, db.pool()).await; + tracing::info!("[UNREGISTER] Resolved output dir for {}: {}", uuid, resolved_output_dir); + let videos_table = schema::table_name("videos"); let processor_table = schema::table_name("processor_results"); let chunks_table = schema::table_name("chunk"); @@ -1783,8 +1806,8 @@ pub(crate) async fn unregister_internal(db: &PostgresDb, uuid: &str) -> Result<( tracing::info!("[UNREGISTER] Deleted all data for {}", uuid); - // Delete output files - delete_output_files(uuid); + // Delete output files (pass pre-resolved dir since videos row is already deleted) + delete_output_files(uuid, db.pool(), Some(&resolved_output_dir)).await; // Delete Qdrant vectors — iterate all collections that carry per-file points. // The main collection holds text/trace chunks (incl. vlm_summary); `_faces` (face diff --git a/src/api/unregister_api.rs b/src/api/unregister_api.rs index 01dab0b0..bc5fe773 100644 --- a/src/api/unregister_api.rs +++ b/src/api/unregister_api.rs @@ -146,8 +146,21 @@ async fn unregister_file_from_system( // 1. Delete from Qdrant (best effort) // TODO: Implement Qdrant deletion - // 2. Delete output files - delete_output_files(uuid, output_dir); + // 2. Delete output files (all libraries: per-library + global + all upload_paths) + delete_output_files(uuid, output_dir).await; + let per_lib = crate::core::config::get_output_dir_for_file(uuid, pool).await; + if !per_lib.is_empty() && per_lib != output_dir { + delete_output_files(uuid, &per_lib).await; + } + if let Ok(rows) = sqlx::query_scalar::<_, String>( + "SELECT output_path FROM upload_paths WHERE enabled = true AND output_path IS NOT NULL" + ).fetch_all(pool).await { + for dir in rows { + if !dir.is_empty() && dir != output_dir && dir != per_lib { + delete_output_files(uuid, &dir).await; + } + } + } // 3. Delete original video (if not keeping) if !keep_video { @@ -218,7 +231,7 @@ async fn unregister_file_from_system( Ok(deleted) } -fn delete_output_files(uuid: &str, output_dir: &str) { +async fn delete_output_files(uuid: &str, output_dir: &str) { let dir = std::path::PathBuf::from(output_dir); // Delete JSON files: {uuid}.*.json