From 948b0f653b9088cce5ed00b316f56973bebb7756 Mon Sep 17 00:00:00 2001 From: Accusys Date: Mon, 5 Oct 2026 03:58:06 +0800 Subject: [PATCH] feat: implement register command for batch 3 **Implementation**: - Uuid::new_v4() for file_uuid generation - Local file path support only - File existence check (exit 1 on failure) - ffprobe metadata extraction - Videos table insertion **Specs**: - videos.status: pending (from schema default) - file_uuid: 32 chars, UNIQUE - content_hash: no UNIQUE constraint **Decision**: Use defaults from M4 --- src/main.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 54178b66..0dec3b7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -151,9 +151,110 @@ async fn main() -> Result<()> { /// Handle register command async fn handle_register(path: &str) -> Result<()> { + use std::path::Path; + use uuid::Uuid; + use momentry_core::core::probe::probe_video; + use momentry_core::core::db::{Database, PostgresDb, VideoRecord, VideoStatus}; + use momentry_core::core::db::postgres_db::StorageStatus; + println!("Registering video: {}", path); - // TODO: Implement registration logic + // 檔案存在性檢查 + let file_path = Path::new(path); + if !file_path.exists() { + anyhow::bail!("File not found: {}", path); + } + + // 產生 file_uuid (Uuid::new_v4()) + let file_uuid = Uuid::new_v4().simple().to_string(); + println!("Generated UUID: {}", file_uuid); + + // ffprobe 提取 metadata + let probe_result = probe_video(path)?; + let duration = probe_result + .format + .duration + .as_ref() + .and_then(|s| s.parse::().ok()) + .unwrap_or(0.0); + + let mut width = 0u32; + let mut height = 0u32; + let mut fps = 0.0; + + for stream in &probe_result.streams { + if stream.codec_type.as_deref() == Some("video") { + width = stream.width.unwrap_or(0); + height = stream.height.unwrap_or(0); + if let Some(fps_str) = &stream.r_frame_rate { + if let Some((num, den)) = fps_str.split_once('/') { + if let (Ok(n), Ok(d)) = (num.parse::(), den.parse::()) { + if d > 0.0 { + fps = n / d; + } + } + } + } + break; + } + } + + println!(" Duration: {}s", duration); + println!(" Video: {}x{}", width, height); + println!(" FPS: {}", fps); + + // 準備 VideoRecord + let file_path_abs = file_path + .canonicalize() + .map(|p| p.to_string_lossy().to_string()) + .unwrap_or_else(|_| path.to_string()); + + let file_name = file_path + .file_name() + .map(|n| n.to_string_lossy().to_string()) + .unwrap_or_default(); + + let probe_json = serde_json::to_value(&probe_result).ok(); + + let record = VideoRecord { + id: 0, + file_uuid: file_uuid.clone(), + file_path: file_path_abs, + file_name, + file_type: Some("video".to_string()), + duration, + width, + height, + fps, + probe_json, + storage: StorageStatus::default(), + status: VideoStatus::Pending, + processing_status: Some(serde_json::json!({"phase": "REGISTERED"})), + birth_registration: None, + user_id: None, + job_id: None, + created_at: String::new(), + registration_time: None, + total_frames: 0, + parent_uuid: None, + cut_done: false, + cut_count: 0, + cut_max_duration: 0.0, + scene_done: false, + audio_tracks: None, + proxy_path: None, + audio_track: None, + }; + + // 寫入資料庫 + let db = PostgresDb::init().await?; + let video_id = db.register_video(&record).await?; + + println!("\n✅ Video registered successfully!"); + println!(" Video ID: {}", video_id); + println!(" File UUID: {}", file_uuid); + println!(" Status: pending"); + Ok(()) }