From 05c112a5ebc88f87b74a53e828eda39e984dbd21 Mon Sep 17 00:00:00 2001 From: "923048992@qq.com" <923048992@qq.com> Date: Wed, 2 Oct 2024 19:30:14 +0800 Subject: [PATCH] chore: add video download test --- src-tauri/src/client/video.rs | 39 ++++++++++++++++++++++++++++++++++- src-tauri/src/main.rs | 2 +- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/client/video.rs b/src-tauri/src/client/video.rs index 0af4b9e..29f1ef7 100644 --- a/src-tauri/src/client/video.rs +++ b/src-tauri/src/client/video.rs @@ -327,6 +327,13 @@ impl Client { total: size, }; progress_handler(payload.clone()); + if size == 0 { + tracing::warn!( + "try to download video as {}, but size is 0, can't download", + save_path + ); + return Err(AppError::VideoDownloadError(save_path.to_owned())); + } let progress_handler = Arc::new(Mutex::new(progress_handler)); let payload = Arc::new(Mutex::new(payload)); @@ -336,9 +343,10 @@ impl Client { let chunk_size = size / nproc as u64; let mut tasks = JoinSet::new(); for i in 0..nproc { + tracing::info!("{}, {}, {}", i, chunk_size, size); let begin = i as u64 * chunk_size; let end = if i == nproc - 1 { - size + size - 1 } else { (i + 1) as u64 * chunk_size - 1 }; @@ -442,6 +450,8 @@ impl Client { #[cfg(test)] mod tests { + use std::fs; + use super::*; #[tokio::test] @@ -454,6 +464,33 @@ mod tests { Ok(()) } + #[tokio::test] + async fn test_download_video() -> Result<()> { + let cli = Arc::new(Client::new()); + let video_url = "https://www.w3schools.com/html/mov_bbb.mp4"; + let save_path = "test.mp4"; + let video_info = VideoPlayInfo { + rtmp_url_hdv: video_url.to_owned(), + ..Default::default() + }; + let cli_cloned = cli.clone(); + cli_cloned + .download_video(&video_info, save_path, |_| {}) + .await?; + + // download original video + let original = cli + .get_request(video_url, None::<&str>) + .await? + .bytes() + .await? + .to_vec(); + + let downloaded = fs::read(save_path)?; + assert_eq!(original, downloaded); + Ok(()) + } + #[test] fn test_get_oauth_signature() -> Result<()> { let cli = Client::new(); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 6a1abe8..63c62ab 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -38,7 +38,7 @@ fn read_log_content() -> Result { } #[tauri::command] -fn is_ffmpeg_installed() -> bool { +async fn is_ffmpeg_installed() -> bool { App::is_ffmpeg_installed() }