use crate::config::get_config; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct VideosResponse { pub videos: Vec, pub total: i64, pub page: usize, pub page_size: usize, } #[tauri::command] pub async fn get_videos( query: Option, status: Option, page: Option, page_size: Option, uuid: Option, ) -> Result { let config = get_config(); let client = reqwest::Client::new(); // Use /api/v1/files endpoint let mut url = format!("{}/api/v1/files", config.api_base_url); let mut params = Vec::new(); if let Some(q) = query { params.push(format!("q={}", q)); } if let Some(s) = status { params.push(format!("status={}", s)); } if let Some(p) = page { params.push(format!("page={}", p)); } if let Some(ps) = page_size { params.push(format!("page_size={}", ps)); } if let Some(u) = uuid { params.push(format!("uuid={}", u)); } if !params.is_empty() { url.push('?'); url.push_str(¶ms.join("&")); } let response = client .get(&url) .header("x-api-key", &config.api_key) .send() .await .map_err(|e| format!("Request to API failed: {}", e))?; if !response.status().is_success() { return Err(format!("API returned error: {}", response.status())); } let json: serde_json::Value = response .json() .await .map_err(|e| format!("Failed to parse API response: {}", e))?; // Extract fields from the new API response format let data = json .get("data") .and_then(|v| v.as_array()) .cloned() .unwrap_or_else(|| vec![]); let total = json.get("total").and_then(|v| v.as_i64()).unwrap_or(0); let page_val = json.get("page").and_then(|v| v.as_u64()).unwrap_or(1) as usize; let page_size_val = json .get("page_size") .and_then(|v| v.as_u64()) .unwrap_or(20) as usize; Ok(VideosResponse { videos: data, total, page: page_val, page_size: page_size_val, }) } #[tauri::command] pub async fn list_videos( query: Option, page: Option, page_size: Option, ) -> Result { get_videos(query, None, page, page_size, None).await } #[tauri::command] pub async fn get_video_faces(file_uuid: String) -> Result { let config = get_config(); let client = reqwest::Client::new(); // Use new endpoint: /api/v1/face/list?file_uuid=... let url = format!( "{}/api/v1/face/list?file_uuid={}", config.api_base_url, file_uuid ); let response = client .get(&url) .header("x-api-key", &config.api_key) .send() .await .map_err(|e| format!("Request failed: {}", e))?; if !response.status().is_success() { return Err(format!("API error: {}", response.status())); } response .json() .await .map_err(|e| format!("Failed to parse response: {}", e)) } #[tauri::command] pub async fn get_chunk_detail(uuid: String, chunk_id: String) -> Result { let config = get_config(); let client = reqwest::Client::new(); let url = format!( "{}/api/v1/videos/{}/details?chunk_id={}", config.api_base_url, uuid, chunk_id ); let response = client .get(&url) .header("x-api-key", &config.api_key) .send() .await .map_err(|e| format!("Request failed: {}", e))?; if !response.status().is_success() { return Err(format!("API error: {}", response.status())); } response .json() .await .map_err(|e| format!("Failed to parse response: {}", e)) } #[tauri::command] pub async fn unregister_video(file_uuid: String) -> Result { let config = get_config(); let client = reqwest::Client::new(); // Use new endpoint: POST /api/v1/unregister let url = format!("{}/api/v1/unregister", config.api_base_url); let response = client .post(&url) .header("x-api-key", &config.api_key) .header("Content-Type", "application/json") .body(serde_json::to_string(&serde_json::json!({ "uuid": file_uuid })).unwrap()) .send() .await .map_err(|e| format!("Request failed: {}", e))?; if !response.status().is_success() { return Err(format!("API error: {}", response.status())); } let mut result: serde_json::Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; // Add file_uuid to match frontend expectation if let Some(obj) = result.as_object_mut() { obj.insert("file_uuid".to_string(), serde_json::Value::String(file_uuid.clone())); } Ok(result) }