feat: add progress bar to the transferring process

This commit is contained in:
VC
2023-10-17 09:26:07 +02:00
parent 58f36a56f7
commit 1d4ae8f152
2 changed files with 44 additions and 12 deletions

View File

@@ -1,10 +1,17 @@
use crate::{config::YoutubeConfig, error::TootubeError, peertube::PeerTubeVideo};
use async_stream::stream;
use bytes::Bytes;
use futures_core::stream::Stream;
use futures_util::{Stream, StreamExt};
use indicatif::{ProgressBar, ProgressStyle};
use log::debug;
use reqwest::{multipart::Form, Body, Client};
use serde::{Deserialize, Serialize};
use std::{error::Error, io::stdin};
use std::{
cmp::min,
error::Error,
io::stdin,
marker::{Send, Sync, Unpin},
};
use tokio::sync::OnceCell;
static ACCESS_TOKEN: OnceCell<String> = OnceCell::const_new();
@@ -341,22 +348,42 @@ pub async fn create_resumable_upload(
/// This takes the PT stream for download, connects it to YT stream for upload
pub async fn now_kiss<'a>(
stream: impl Stream<Item = Result<Bytes, reqwest::Error>>
+ std::marker::Send
+ std::marker::Sync
+ 'a + 'static,
size: u64,
mut stream: impl Stream<Item = Result<Bytes, reqwest::Error>> + Send + Sync + Unpin + 'a + 'static,
r_url: &'a str,
config: &'a YoutubeConfig,
) -> Result<String, Box<dyn Error>> {
// Get access token
let access_token = refresh_token(config).await?;
// Create the progress bar
let pb = ProgressBar::new(size);
pb.set_style(ProgressStyle::default_bar()
.template("{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")?
.progress_chars("#>-"));
pb.set_message("Transferring…");
let mut transferring: u64 = 0;
let async_stream = stream! {
while let Some(chunk) = stream.next().await {
if let Ok(chunk) = &chunk {
let new = min(transferring + (chunk.len() as u64), size);
transferring = new;
pb.set_position(new);
if transferring >= size {
pb.finish();
}
}
yield chunk;
}
};
// Create client
let client = Client::new();
let res = client
.put(r_url)
.header("Authorization", format!("Bearer {}", access_token))
.body(Body::wrap_stream(stream))
.body(Body::wrap_stream(async_stream))
.send()
.await?;