From 36f4dcf807cd5960a53da6bb11fb26f6d19a2e7f Mon Sep 17 00:00:00 2001 From: Accusys Date: Thu, 19 Nov 2026 13:23:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20unregister=20=E5=88=AA=E9=99=A4=20output?= =?UTF-8?q?=20=E6=99=82=E4=BD=BF=E7=94=A8=20per-library=20=E8=B7=AF?= =?UTF-8?q?=E5=BE=91=EF=BC=88videos=20=E5=88=AA=E9=99=A4=E5=89=8D=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=EF=BC=89=20-=20=E6=A0=B9=E5=9B=A0:=20get=5Foutput=5Fd?= =?UTF-8?q?ir=5Ffor=5Ffile=20JOIN=20videos=EF=BC=8C=E4=BD=86=20unregister?= =?UTF-8?q?=20=E5=85=88=E5=88=AA=20videos=20=E5=86=8D=E5=88=AA=20output=20?= =?UTF-8?q?=20=20=E2=86=92=20=E8=A7=A3=E6=9E=90=E5=A4=B1=E6=95=97=E5=9B=9E?= =?UTF-8?q?=E9=80=80=E5=85=A8=E5=9F=9F=20OUTPUT=5FDIR(demo)=EF=BC=8C?= =?UTF-8?q?=E6=B2=92=E5=88=AA=E5=88=B0=20test=20=E7=9B=AE=E9=8C=84?= =?UTF-8?q?=E8=88=8A=E6=AA=94=20=20=20=E2=86=92=20re-scan=20=E6=99=82=20AU?= =?UTF-8?q?TO-REPAIR=20=E7=9C=8B=E5=88=B0=E8=88=8A=20asrx.json=20=E5=B0=B1?= =?UTF-8?q?=E6=A8=99=20completed=EF=BC=88=E7=84=A1=20status/chunks?= =?UTF-8?q?=EF=BC=89=E2=86=92=20=E5=8D=A1=E4=BD=8F=20-=20=E4=BF=AE?= =?UTF-8?q?=E5=BE=A9:=20=E5=9C=A8=20unregister=5Finternal=20=E9=96=8B?= =?UTF-8?q?=E9=A0=AD=EF=BC=88videos=20=E9=82=84=E5=9C=A8=E6=99=82=EF=BC=89?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=20output=5Fdir=20-=20delete=5Foutput=5Ffiles?= =?UTF-8?q?=20=E6=8E=83=E6=8F=8F=20=E5=85=A8=E5=9F=9F=20+=20pre-resolved?= =?UTF-8?q?=20per-library=20+=20=E6=89=80=E6=9C=89=20upload=5Fpaths=20-=20?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BF=AE=E5=BE=A9=20unregister=5Fapi.rs=20-?= =?UTF-8?q?=20=E7=B5=90=E6=9E=9C:=20re-scan=20=E7=9C=9F=E6=AD=A3=E6=B8=85?= =?UTF-8?q?=E6=8E=89=E8=88=8A=E6=AA=94=20=E2=86=92=20ASRX=20=E9=87=8D?= =?UTF-8?q?=E8=B7=91=20=E2=86=92=20has=5Ftranscript=20+=2028=20chunks=20?= =?UTF-8?q?=E2=9C=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/files.rs | 33 ++++++++++++++++++++++++++++----- src/api/unregister_api.rs | 19 ++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) 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