From 946673c6996876e958a51742111b06a4587ff924 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 10 Jul 2025 19:27:14 +0800 Subject: [PATCH] fix --- src-tauri/src/commands/ai_video.rs | 58 ++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/src-tauri/src/commands/ai_video.rs b/src-tauri/src/commands/ai_video.rs index d1961ce..ac65c07 100644 --- a/src-tauri/src/commands/ai_video.rs +++ b/src-tauri/src/commands/ai_video.rs @@ -1,6 +1,6 @@ use serde::Deserialize; use std::process::Command; -use std::io::{BufRead, BufReader}; +use std::io::{BufRead, BufReader, Read}; use std::time::{Duration, Instant}; use std::thread; use std::sync::{Arc, Mutex}; @@ -81,8 +81,8 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu let stderr = child.stderr.take().unwrap(); // Use channels for concurrent reading - let (stdout_tx, stdout_rx) = mpsc::channel(); - let (stderr_tx, stderr_rx) = mpsc::channel(); + let (stdout_tx, _stdout_rx) = mpsc::channel(); + let (stderr_tx, _stderr_rx) = mpsc::channel(); let (result_tx, result_rx) = mpsc::channel(); let progress_messages = Arc::new(Mutex::new(Vec::new())); @@ -93,10 +93,22 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu let progress_messages_clone = Arc::clone(&progress_messages); let final_result_clone = Arc::clone(&final_result); thread::spawn(move || { - let reader = BufReader::new(stdout); - for line in reader.lines() { - match line { - Ok(line) => { + let mut reader = BufReader::new(stdout); + let mut buffer = Vec::new(); + + // Read raw bytes and handle encoding issues + loop { + buffer.clear(); + match reader.read_until(b'\n', &mut buffer) { + Ok(0) => break, // EOF + Ok(_) => { + // Convert bytes to string, handling invalid UTF-8 + let line = String::from_utf8_lossy(&buffer); + let line = line.trim_end_matches('\n').trim_end_matches('\r'); + + if line.is_empty() { + continue; + } println!("Python stdout: {}", line); // Parse JSON-RPC messages @@ -133,7 +145,7 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu println!("Python other: {}", line); } - if stdout_tx.send(line).is_err() { + if stdout_tx.send(line.to_string()).is_err() { break; } } @@ -148,15 +160,28 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu // Spawn thread for reading stderr let error_messages_clone = Arc::clone(&error_messages); thread::spawn(move || { - let reader = BufReader::new(stderr); - for line in reader.lines() { - match line { - Ok(line) => { + let mut reader = BufReader::new(stderr); + let mut buffer = Vec::new(); + + // Read raw bytes and handle encoding issues + loop { + buffer.clear(); + match reader.read_until(b'\n', &mut buffer) { + Ok(0) => break, // EOF + Ok(_) => { + // Convert bytes to string, handling invalid UTF-8 + let line = String::from_utf8_lossy(&buffer); + let line = line.trim_end_matches('\n').trim_end_matches('\r'); + + if line.is_empty() { + continue; + } + println!("Python stderr: {}", line); if let Ok(mut messages) = error_messages_clone.lock() { - messages.push(line.clone()); + messages.push(line.to_string()); } - if stderr_tx.send(line).is_err() { + if stderr_tx.send(line.to_string()).is_err() { break; } } @@ -215,6 +240,10 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu println!("Process was likely terminated by antivirus or system security"); } else if code == 1 { println!("Process exited with error code 1 (general error)"); + } else if code == 120 { + println!("Process exited with error code 120 (module import failure or unsupported function)"); + } else { + println!("Process exited with unknown error code: {}", code); } } } else { @@ -253,6 +282,7 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu let unsigned_code = code as u32; match code { 1 => "Python script failed with general error. Check if all dependencies are installed.".to_string(), + 120 => "Python module import failed or function not supported. This may be due to missing dependencies or incompatible Python environment. Please run the environment test first.".to_string(), -1073741510 => "Python process was terminated by antivirus or system security. Please add the application to your antivirus whitelist.".to_string(), _ if unsigned_code == 3221225786 => "Python process was terminated by antivirus or system security. Please add the application to your antivirus whitelist.".to_string(), _ => format!("Python script failed with exit code: {}. This may indicate a system or environment issue.", code)