feat: real service test endpoints (proxy 7-route round-trip, core/chat/vlm/markbase/embedding), unified log paths + errorLog, pipeline tools/import/export/folder APIs, face pipeline check, dynamic concurrency tuning
Core Test / lint-and-test (push) Has been cancelled
Cross-Project Integration / integration (push) Has been cancelled

This commit is contained in:
Accusys
2026-11-16 18:16:46 +08:00
parent d21d1ab960
commit 083d73d287
88 changed files with 12216 additions and 1096 deletions
+177 -6
View File
@@ -250,12 +250,12 @@ async fn scan_files(
State(state): State<AppState>,
Query(params): Query<ScanFilesQuery>,
) -> Result<Json<ScanFilesResponse>, StatusCode> {
// 用 momentry root 而非 current_exe(部署時 binary 在 <root>/bin/ 下,
// 直接取 parent 會多一層 bin 導致掃描到錯誤目錄)。
let demo_dir_str = std::env::var("MOMENTRY_SFTP_ROOT").unwrap_or_else(|_| {
std::env::current_exe()
.ok()
.and_then(|p| p.parent().map(|d| d.join("var/sftpgo/data/demo")))
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|| "var/sftpgo/data/demo".to_string())
crate::core::path_utils::demo_dir()
.to_string_lossy()
.to_string()
});
let demo_dir = std::path::Path::new(&demo_dir_str);
@@ -1311,6 +1311,7 @@ async fn count_edges(pool: &sqlx::PgPool, table: &str, file_uuid: &str, edge_typ
pub fn scan_routes() -> Router<AppState> {
Router::new()
.route("/api/v1/files/scan", get(scan_files))
.route("/api/v1/files/scan-path", get(scan_specific_path))
.route("/api/v1/stats/sftpgo", get(get_sftpgo_status))
.route(
"/api/v1/stats/ingestion-status/:file_uuid",
@@ -1644,7 +1645,7 @@ async fn get_pipeline_progress_handler(
}
/// Quick scene VLM stats endpoint (fast, no Qdrant queries)
async fn get_scene_vlm_stats(
pub async fn get_scene_vlm_stats(
State(state): State<AppState>,
Path(file_uuid): Path<String>,
) -> Result<Json<SceneVlmStats>, StatusCode> {
@@ -1698,3 +1699,173 @@ async fn get_scene_vlm_stats(
vlm_total: total_scenes.max(scenes_array),
}))
}
// 掃描指定路徑
#[derive(Debug, Deserialize)]
pub struct ScanPathQuery {
pub path: String,
}
#[derive(Debug, Serialize)]
pub struct ScanPathResponse {
pub scanned_files: usize,
pub new_files: usize,
pub path: String,
}
fn scan_dir_recursive(dir: &std::path::Path, files: &mut Vec<std::path::PathBuf>) {
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.filter_map(|e| e.ok()) {
let path = entry.path();
if path.is_dir() {
scan_dir_recursive(&path, files);
} else if path.is_file() {
files.push(path);
}
}
}
}
async fn scan_specific_path(
State(state): State<AppState>,
axum::extract::ConnectInfo(_addr): axum::extract::ConnectInfo<std::net::SocketAddr>,
Query(params): Query<ScanPathQuery>,
) -> Result<Json<ScanPathResponse>, StatusCode> {
use std::collections::HashSet;
let scan_root = std::path::Path::new(&params.path);
if !scan_root.exists() || !scan_root.is_dir() {
return Err(StatusCode::NOT_FOUND);
}
let allowed_extensions = vec!["mp4", "mov", "mkv", "avi", "webm", "jpg", "jpeg", "png", "gif", "webp"];
let table = schema::table_name("videos");
let registered: Vec<String> = sqlx::query_scalar(&format!("SELECT file_path FROM {}", table))
.fetch_all(state.db.pool())
.await
.unwrap_or_default();
let registered_set: HashSet<String> = registered.into_iter().collect();
// ── 收集候選檔案 (path, name, ext) ──────────────────────────────
// 優先透過 MarkBase 虛擬層 (/api/v2/myfiles/:user/files) 取得檔案清單;
// MarkBase 管不到的路徑(upload root 之外、外接碟等)回退本地遞迴掃描。
let mut candidates: Vec<(String, String, String)> = Vec::new(); // (path, name, ext)
// 依 upload_paths 推導 MarkBase username(user root = <upload>/<name>)
let mut username: Option<String> = None;
let mut rel_base = String::new();
let paths_table = schema::table_name("upload_paths");
if let Ok(rows) = sqlx::query_as::<_, (String, String)>(
&format!("SELECT name, full_path FROM {} WHERE enabled = true", paths_table),
)
.fetch_all(state.db.pool())
.await
{
for (name, full_path) in rows {
if params.path == full_path {
username = Some(name);
rel_base = String::new();
break;
}
if params.path.starts_with(&format!("{}/", full_path)) {
username = Some(name);
rel_base = params.path[full_path.len()..].trim_start_matches('/').to_string();
break;
}
}
}
let mut used_markbase = false;
if let Some(user) = username {
let markbase_base =
std::env::var("MOMENTRY_MARKBASE_URL").unwrap_or_else(|_| "http://localhost:11438".to_string());
let mut url = format!("{}/api/v2/myfiles/{}/files", markbase_base, user);
if !rel_base.is_empty() {
url.push_str(&format!("?folder={}", rel_base));
}
let client = reqwest::Client::new();
if let Ok(resp) = client.get(&url).send().await {
if resp.status().is_success() {
if let Ok(files) = resp.json::<Vec<serde_json::Value>>().await {
for file in files {
let name = file.get("name").and_then(|v| v.as_str()).unwrap_or("");
if name.is_empty() {
continue;
}
let folder = file.get("folder").and_then(|v| v.as_str()).unwrap_or("");
// MarkBase 回傳的 folder 相對於 user root;換算成相對於掃描路徑
let rel_to_scan = folder.strip_prefix(&rel_base).unwrap_or(folder);
let rel_to_scan = rel_to_scan.trim_start_matches('/');
let file_path = if rel_to_scan.is_empty() {
format!("{}/{}", params.path, name)
} else {
format!("{}/{}/{}", params.path, rel_to_scan, name)
};
let ext = name.rsplit('.').next().unwrap_or("").to_lowercase();
if !allowed_extensions.contains(&ext.as_str()) {
continue;
}
candidates.push((file_path, name.to_string(), ext));
}
used_markbase = true;
}
}
}
}
if !used_markbase {
// 回退:本地遞迴掃描(MarkBase 無法服務此路徑)
let mut all_files = Vec::new();
scan_dir_recursive(scan_root, &mut all_files);
for file_path in all_files {
let path_str = file_path.to_string_lossy().to_string();
let ext = file_path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_lowercase();
if !allowed_extensions.contains(&ext.as_str()) {
continue;
}
let file_name = file_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("unknown")
.to_string();
candidates.push((path_str, file_name, ext));
}
}
let mut scanned = 0usize;
let mut new_files = 0usize;
for (path_str, file_name, ext) in candidates {
scanned += 1;
if registered_set.contains(&path_str) {
continue;
}
let file_uuid = uuid::Uuid::new_v4().to_string().replace("-", "");
let _ = sqlx::query(&format!(
"INSERT INTO {} (file_uuid, file_path, file_name, file_type, status) VALUES ($1, $2, $3, $4, 'pending')",
table
))
.bind(&file_uuid)
.bind(&path_str)
.bind(&file_name)
.bind(&ext)
.execute(state.db.pool())
.await;
new_files += 1;
}
Ok(Json(ScanPathResponse {
scanned_files: scanned,
new_files,
path: params.path,
}))
}