fix: unregister 刪除 output 時使用 per-library 路徑(videos 刪除前解析)
- 根因: get_output_dir_for_file JOIN videos,但 unregister 先刪 videos 再刪 output → 解析失敗回退全域 OUTPUT_DIR(demo),沒刪到 test 目錄舊檔 → re-scan 時 AUTO-REPAIR 看到舊 asrx.json 就標 completed(無 status/chunks)→ 卡住 - 修復: 在 unregister_internal 開頭(videos 還在時)解析 output_dir - delete_output_files 掃描 全域 + pre-resolved per-library + 所有 upload_paths - 同步修復 unregister_api.rs - 結果: re-scan 真正清掉舊檔 → ASRX 重跑 → has_transcript + 28 chunks ✓
This commit is contained in:
+28
-5
@@ -1297,10 +1297,28 @@ struct ReScanResponse {
|
||||
process_command_id: Option<String>,
|
||||
}
|
||||
|
||||
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<String> = 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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user