From 11264e4be5b35eb2048712ccc0fc92e283f33735 Mon Sep 17 00:00:00 2001 From: Waradu Date: Sun, 20 Oct 2024 10:04:09 +0200 Subject: [PATCH] added progress indicator --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 3 ++- src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 36fee33..29ab4a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1084,7 +1084,7 @@ checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" [[package]] name = "streamshare" -version = "1.1.1" +version = "2.0.0" dependencies = [ "futures", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 87e9c90..cf3e3d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "streamshare" -version = "1.1.1" +version = "2.0.0" edition = "2021" description = "Upload to streamshare library" license = "MIT" diff --git a/README.md b/README.md index 0a88317..da47d3e 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ Upload files to [streamshare](https://streamshare.wireway.ch) Upload: ```rust -match upload(&file_path).await { +let show_progress = true; +match upload(&file_path, show_progress).await { Ok((file_identifier, _deletion_token)) => { let download_url = format!( "https://streamshare.wireway.ch/download/{}", diff --git a/src/lib.rs b/src/lib.rs index f1f47e1..19b17c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ use futures::{SinkExt, StreamExt}; use reqwest::Client; use serde::Deserialize; -use std::path::Path; +use std::{io::Write, path::Path, time::Instant}; use tokio::fs; use tokio::fs::File; use tokio::io::AsyncReadExt; @@ -17,13 +17,14 @@ struct CreateResponse { deletion_token: String, } -pub async fn upload(file_path: &str) -> Result<(String, String), Box> { +pub async fn upload(file_path: &str, show_progress: bool) -> Result<(String, String), Box> { let path = Path::new(file_path); let metadata = fs::metadata(path).await?; if !metadata.is_file() { return Err("Selected item is not a file".into()); } + let file_size = metadata.len(); let file_name = path .file_name() .and_then(|s| s.to_str()) @@ -43,7 +44,6 @@ pub async fn upload(file_path: &str) -> Result<(String, String), Box Result<(String, String), Box Result<(String, String), Box (), @@ -77,6 +91,15 @@ pub async fn upload(file_path: &str) -> Result<(String, String), Box Result<(String, String), Box Result<(), Box> { +pub async fn delete( + file_identifier: &str, + deletion_token: &str, +) -> Result<(), Box> { let client = Client::new(); let delete_url = format!( "https://streamshare.wireway.ch/api/delete/{}/{}", @@ -104,3 +130,19 @@ pub async fn delete(file_identifier: &str, deletion_token: &str) -> Result<(), B Err(format!("Failed to delete file: {}", res.status()).into()) } } + +fn bytes_to_human_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) + } +}