From c7e859badf1e21d294908db8a125d4f73a4b66d3 Mon Sep 17 00:00:00 2001 From: Waradu Date: Sun, 20 Oct 2024 13:06:35 +0200 Subject: [PATCH] updated to streamshare v3 --- Cargo.lock | 6 +++--- Cargo.toml | 8 ++++---- README.md | 1 + src/main.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3fb6a56..a03cbc6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1191,9 +1191,9 @@ checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" [[package]] name = "streamshare" -version = "2.0.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "723b8059b8580d19ea0bdd3202fc8ba38c01719ee5ad2d5b70e530e8f3c5e22e" +checksum = "d8a66009b98588531c068a6b1fc2345a05292dd279c73554d514fd47f926b2e7" dependencies = [ "futures", "reqwest", @@ -1306,7 +1306,7 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "to-streamshare" -version = "0.3.1" +version = "0.4.0" dependencies = [ "clap", "streamshare", diff --git a/Cargo.toml b/Cargo.toml index 6109e55..44f7271 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "to-streamshare" -version = "0.3.1" +version = "0.4.0" edition = "2021" description = "Upload to streamshare (to-ss > toss) from the terminal" license = "MIT" @@ -8,13 +8,13 @@ homepage = "https://waradu.dev" repository = "https://github.com/Waradu/to-streamshare" readme = "README.md" authors = ["Waradu"] -keywords = ["streamshare","file-sharing","upload"] +keywords = ["streamshare", "file-sharing", "upload"] [dependencies] clap = { version = "4.5.20", features = ["derive"] } -streamshare = "2" +streamshare = "3" tokio = { version = "1.40.0", features = ["full"] } [[bin]] name = "toss" -path = "src/main.rs" \ No newline at end of file +path = "src/main.rs" diff --git a/README.md b/README.md index 089f521..a3fc0c9 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,5 @@ Upload files to [streamshare](https://streamshare.wireway.ch) with the terminal. Run `cargo install to-streamshare` to install it and use it with `toss "filepath"`. + Delete a file with `toss --delete file_identifier/deletion_token` diff --git a/src/main.rs b/src/main.rs index 0c36555..8cef0eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::{io::Write, time::Instant}; + use clap::{CommandFactory, Parser}; use streamshare::{delete, upload}; @@ -29,13 +31,40 @@ async fn main() { eprintln!("Invalid format for --delete. Use 'file_identifier/deletion_token' (e.g., 'abc123/def456')"); } } else if let Some(file_path) = args.file { - match upload(&file_path, true).await { + let start_time = Instant::now(); + let mut file_size: u64 = 0; + + let show_progress = |uploaded_bytes, total_bytes| { + let percentage = (uploaded_bytes as f64 / total_bytes as f64) * 100.0; + let uploaded = readable(uploaded_bytes); + let total = readable(total_bytes); + let elapsed_secs = start_time.elapsed().as_secs_f64(); + let speed = readable((uploaded_bytes as f64 / elapsed_secs) as u64); + file_size = total_bytes; + + print!( + "\r\x1b[2K{:.2}% {}/{} ({}/s)", + percentage, uploaded, total, speed + ); + std::io::stdout().flush().unwrap(); + }; + + match upload(&file_path, show_progress).await { Ok((file_identifier, deletion_token)) => { let download_url = format!( "https://streamshare.wireway.ch/download/{}", file_identifier ); + let elapsed_secs = start_time.elapsed().as_secs_f64(); + println!( + "\r\x1b[2K100.00% {}/{} (Upload completed in {:.2}s)", + readable(file_size), + readable(file_size), + elapsed_secs + ); + println!(); + println!("File uploaded successfully"); println!("Download URL: {}", download_url); println!("File Identifier: {}", file_identifier); @@ -56,3 +85,19 @@ fn parse_delete_param(param: &str) -> Option<(&str, &str)> { None } } + +fn readable(bytes: u64) -> String { + const KB: f64 = 1024.0; + const MB: f64 = KB * 1024.0; + const GB: f64 = MB * 1024.0; + + if bytes as f64 >= GB { + format!("{:.2}gb", bytes as f64 / GB) + } else if bytes as f64 >= MB { + format!("{:.2}mb", bytes as f64 / MB) + } else if bytes as f64 >= KB { + format!("{:.2}kb", bytes as f64 / KB) + } else { + format!("{}b", bytes) + } +}