Skip to content

Commit

Permalink
chore: optimize video upload progress period
Browse files Browse the repository at this point in the history
  • Loading branch information
Okabe-Rintarou-0 committed Oct 1, 2024
1 parent 828f2e5 commit 7884840
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/client/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub const OAUTH_RANDOM_P2: &str = "oauth_VWXYZ";
pub const OAUTH_RANDOM_P1_VAL: &str = "ABCDEFGH";
pub const OAUTH_RANDOM_P2_VAL: &str = "STUVWXYZ";
pub const CHUNK_SIZE: u64 = 16 * 1024 * 1024;
// pub const VIDEO_CHUNK_SIZE: u64 = 32 * 1024 * 1024;
pub const VIDEO_CHUNK_SIZE: u64 = 4 * 1024 * 1024;

pub const JBOX_LOGIN_URL: &str =
"https://pan.sjtu.edu.cn/user/v1/sign-in/sso-login-redirect/xpw8ou8y";
Expand Down
46 changes: 28 additions & 18 deletions src-tauri/src/client/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use super::{
use crate::{
client::constants::{
OAUTH_PATH, OAUTH_RANDOM, OAUTH_RANDOM_P1, OAUTH_RANDOM_P1_VAL, OAUTH_RANDOM_P2,
OAUTH_RANDOM_P2_VAL, VIDEO_INFO_URL,
OAUTH_RANDOM_P2_VAL, VIDEO_CHUNK_SIZE, VIDEO_INFO_URL,
},
error::{AppError, Result},
model::{
Expand Down Expand Up @@ -349,24 +349,34 @@ impl Client {
let payload = payload.clone();
let progress_handler = progress_handler.clone();
tasks.spawn(async move {
let response = self_clone.download_video_partial(&url, begin, end).await?;
let status = response.status();
if !(status == StatusCode::OK || status == StatusCode::PARTIAL_CONTENT) {
tracing::error!("status not ok: {}", status);
return Err(AppError::VideoDownloadError(save_path));
}
let bytes = response.bytes().await?;
let read_bytes = bytes.len() as u64;
tracing::info!("read_bytes: {:?}", read_bytes);
{
let mut file = output_file.lock().await;
write_file_at_offset(file.by_ref(), &bytes, begin)?;
// release lock automatically after scope release
}
let mut current_begin = begin;
while current_begin < end {
let response = self_clone
.download_video_partial(
&url,
current_begin,
current_begin + VIDEO_CHUNK_SIZE,
)
.await?;
let status = response.status();
if !(status == StatusCode::OK || status == StatusCode::PARTIAL_CONTENT) {
tracing::error!("status not ok: {}", status);
return Err(AppError::VideoDownloadError(save_path));
}
let bytes = response.bytes().await?;
let read_bytes = bytes.len() as u64;
current_begin += read_bytes;
tracing::info!("read_bytes: {:?}", read_bytes);
{
let mut file = output_file.lock().await;
write_file_at_offset(file.by_ref(), &bytes, begin)?;
// release lock automatically after scope release
}

let mut payload_guard = payload.lock().await;
payload_guard.processed += read_bytes;
progress_handler.lock().await(payload_guard.clone());
let mut payload_guard = payload.lock().await;
payload_guard.processed += read_bytes;
progress_handler.lock().await(payload_guard.clone());
}
Ok(())
});
}
Expand Down

0 comments on commit 7884840

Please sign in to comment.