feat: integrate MarkBase API for file list

- Core API now calls MarkBase API instead of scanning filesystem
- Returns file_uuid and file_path from MarkBase
- Merges Core database registration status
- Returns 176 files (116 from MarkBase + 60 registered but deleted)
- Tested and verified on M5Max128

Related: #markbase-integration
This commit is contained in:
Accusys
2026-10-10 02:40:08 +08:00
parent 42edfedb3f
commit 3d7d5cf684
3 changed files with 637 additions and 45 deletions
+100 -45
View File
@@ -133,43 +133,87 @@ async fn list_files(
}));
}
// Default: List files with pagination
let offset = ((page - 1) as i64) * (page_size as i64);
let records = state
.db
.list_videos(page_size as i32, offset)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let total = records.1;
// Call MarkBase API to get file list
let markbase_url = "http://localhost:11438/api/v2/myfiles/demo/files";
let client = reqwest::Client::new();
let sync = params.sync_status.unwrap_or(false);
let mut data: Vec<FileItem> = records
.0
.into_iter()
.map(|r| FileItem {
file_uuid: r.file_uuid.clone(),
file_name: r.file_name,
file_path: r.file_path,
status: r.status.as_str().to_string(),
is_registered: r.status.as_str() != "pending",
let markbase_files: Vec<MarkBaseFile> = client
.get(markbase_url)
.send()
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("MarkBase API error: {}", e)))?
.json()
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("MarkBase API parse error: {}", e)))?;
let mut all_files: Vec<FileItem> = Vec::new();
// Convert MarkBase files to FileItem
for mb_file in markbase_files {
all_files.push(FileItem {
file_uuid: mb_file.file_uuid.unwrap_or_default(),
file_name: mb_file.name,
file_path: mb_file.file_path.unwrap_or_default(),
status: "unregistered".to_string(),
is_registered: false,
total_chunks: 0,
})
});
}
// Get registered files from database
let table = crate::core::db::schema::table_name("videos");
let registered: Vec<(String, String, String, String)> = sqlx::query_as(&format!(
"SELECT file_uuid, file_name, file_path, status FROM {} ORDER BY id",
table
))
.fetch_all(state.db.pool())
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
// Build registered map
let registered_map: std::collections::HashMap<String, (String, String, String)> = registered
.into_iter()
.map(|(uuid, name, path, status)| (path, (uuid, name, status)))
.collect();
// If sync_status=true, compute status for each file
if sync {
for item in &mut data {
if let Ok(computed) = compute_file_status(&state, &item.file_uuid).await {
item.status = computed;
}
// Merge: update registration status for matching files
for file in &mut all_files {
if let Some((uuid, name, status)) = registered_map.get(&file.file_path) {
file.file_uuid = uuid.clone();
file.status = status.clone();
file.is_registered = status != "pending";
}
}
// Add files that are in database but not in MarkBase (registered but moved/deleted)
let all_paths: std::collections::HashSet<String> = all_files.iter().map(|f| f.file_path.clone()).collect();
for (path, (uuid, name, status)) in &registered_map {
if !all_paths.contains(path) {
all_files.push(FileItem {
file_uuid: uuid.clone(),
file_name: name.clone(),
file_path: path.clone(),
status: status.clone(),
is_registered: status != "pending",
total_chunks: 0,
});
}
}
let total = all_files.len() as i64;
// Apply pagination
let offset = ((page - 1) as usize) * page_size;
let data: Vec<FileItem> = all_files
.into_iter()
.skip(offset)
.take(page_size)
.collect();
// Fetch chunk counts for all files in one query
let uuids: Vec<String> = data.iter().map(|f| f.file_uuid.clone()).collect();
let uuids: Vec<String> = data.iter().map(|f| f.file_uuid.clone()).filter(|u| !u.is_empty()).collect();
let mut count_map: std::collections::HashMap<String, i64> = std::collections::HashMap::new();
if !uuids.is_empty() {
let chunk_table = crate::core::db::schema::table_name("chunk");
let placeholders: Vec<String> = (1..=uuids.len()).map(|i| format!("${}", i)).collect();
@@ -184,12 +228,13 @@ async fn list_files(
.await
.unwrap_or_default();
let count_map: std::collections::HashMap<String, i64> = chunk_counts.into_iter().collect();
for item in &mut data {
if let Some(cnt) = count_map.get(&item.file_uuid) {
item.total_chunks = *cnt;
}
count_map = chunk_counts.into_iter().collect();
}
let mut data = data;
for item in &mut data {
if let Some(cnt) = count_map.get(&item.file_uuid) {
item.total_chunks = *cnt;
}
}
@@ -202,6 +247,16 @@ async fn list_files(
}))
}
#[derive(Debug, Deserialize)]
struct MarkBaseFile {
file_uuid: Option<String>,
file_path: Option<String>,
name: String,
size: u64,
modified: String,
folder: String,
}
#[derive(Debug, Serialize)]
pub struct FilesResponse {
pub success: bool,
@@ -240,10 +295,10 @@ async fn compute_file_status(
file_uuid: &str,
) -> Result<String, StatusCode> {
use crate::core::config::OUTPUT_DIR;
let output_dir = OUTPUT_DIR.as_str();
let videos_table = crate::core::db::schema::table_name("videos");
// Check processing_status->>'phase'
let phase: String = sqlx::query_scalar(&format!(
"SELECT COALESCE(processing_status->>'phase', '') FROM {} WHERE file_uuid = $1",
@@ -255,9 +310,9 @@ async fn compute_file_status(
.ok()
.flatten()
.unwrap_or_default();
let phase_completed = phase == "COMPLETED";
// Check if any processor JSON exists
let processors = [
("asr", "asr.json"),
@@ -267,7 +322,7 @@ async fn compute_file_status(
("face", "face.json"),
("face_cluster", "face_cluster.json"),
];
let mut any_json_exists = false;
for (_name, filename) in &processors {
let path = std::path::Path::new(output_dir).join(format!("{}.{}", file_uuid, filename));
@@ -276,7 +331,7 @@ async fn compute_file_status(
break;
}
}
// Derive status
let status = if phase_completed && any_json_exists {
"completed"
@@ -285,7 +340,7 @@ async fn compute_file_status(
} else {
"registered"
};
Ok(status.to_string())
}
@@ -298,14 +353,14 @@ async fn get_file_detail(
.get_video_by_uuid(&file_uuid)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
match file {
Some(f) => {
// Compute status instead of using DB value
let computed_status = compute_file_status(&state, &file_uuid)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(FileDetailResponse {
success: true,
file_uuid: f.file_uuid,
@@ -319,7 +374,7 @@ async fn get_file_detail(
.ok()
.map(|d| d.into()),
}))
},
}
None => Err((
StatusCode::NOT_FOUND,
format!("File not found: {}", file_uuid),