Edit this to stream stdout and stderr lines in a json array to the client while rclone…
- User
Edit this to stream stdout and stderr lines in a json array to the client while rclone command is running so i can track progress instead of waiting for command to end: async fn handle_push_path( &self, path: &Path, req_path: &str, is_dir: bool, rclone_path: Option, head_only: bool, user: Option, res: &mut Response, ) -> Result<()> { let parent_path = Path::new(req_path).parent().unwrap(); println!(“local:{}”, req_path); println!(“remote:{}”, parent_path.to_str().unwrap()); let out = if is_dir {req_path} else {parent_path.to_str().unwrap()};
let output = Command::new(rclone_path.unwrap().to_str().unwrap()) .arg(“copy”) .arg(format!(“local:{}”, req_path)) .arg(format!(“remote:{}”, out)) .arg(“-P”) .arg(“-M”) .arg(“–create-empty-src-dirs”) .stdout(Stdio::piped()) .output().unwrap();
let stdout = String::from_utf8(output.stdout)?; let json_output: Value = json!({ “status”: output.status.success(), “stdout”: stdout.trim(), “stderr”: String::from_utf8_lossy(&output.stderr).trim(), });
res.headers_mut() .typed_insert(ContentType::from(mime_guess::mime::APPLICATION_JSON)); res.headers_mut() .typed_insert(ContentLength(json_output.to_string().as_bytes().len() as u64)); if head_only { return Ok(()); } *res.body_mut() = json_output.to_string().into(); Ok(()) }
- Assistant
You can use
tokio::process::Commandinstead ofstd::process::Commandand utilize itsspawn()method to stream the command's output. Here's the modified version of yourhandle_push_pathfunction:use tokio::io::AsyncReadExt; use tokio::process::Command; async fn handle_push_path( &self, path: &Path, req_path: &str, is_dir: bool, rclone_path: Option<PathBuf>, head_only: bool, user: Option<String>, res: &mut Response, ) -> Result<()> { let parent_path = Path::new(req_path).parent().unwrap(); println!("local:{}", req_path); println!("remote:{}", parent_path.to_str().unwrap()); let out = if is_dir { req_path } else { parent_path.to_str().unwrap() }; let mut command = Command::new(rclone_path.unwrap().to_str().unwrap()) .arg("copy") .arg(format!("local:{}", req_path)) .arg(format!("remote:{}", out)) .arg("-P") .arg("-M") .arg("--create-empty-src-dirs") .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn()?; let mut stdout = String::new(); let mut stderr = String::new(); if let Some(mut stdout_stream) = command.stdout.take() { stdout_stream.read_to_string(&mut stdout).await?; } if let Some(mut stderr_stream) = command.stderr.take() { stderr_stream.read_to_string(&mut stderr).await?; } let status = command.wait().await?; let json_output: Value = json!({ "status": status.success(), "stdout": stdout.trim(), "stderr": stderr.trim(), }); res.headers_mut() .typed_insert(ContentType::from(mime_guess::mime::APPLICATION_JSON)); res.headers_mut() .typed_insert(ContentLength(json_output.to_string().as_bytes().len() as u64)); if head_only { return Ok(()); } *res.body_mut() = json_output.to_string().into(); Ok(()) }