From bf907dbdcf2a2724435521fe04a48136b41278fd Mon Sep 17 00:00:00 2001 From: zvicii Date: Thu, 28 Dec 2023 14:12:10 +0800 Subject: [PATCH] feat: upload & download url decode object --- Cargo.toml | 7 ++++++- src/lib.rs | 21 +++++++++++++++++++-- src/main.rs | 30 ++++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8e2947e..50110b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,13 @@ [package] name = "ne-s3" +description = "s3 client with C interface" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[package.metadata.bundle] +name = "ne-s3" +identifier = "com.netease.nmc.demo" +copyright = "Copyright (c) Jane Doe 2016. All rights reserved." [dependencies] aws-config = { version = "1.1.1", features = ["behavior-version-latest"] } @@ -26,6 +30,7 @@ log = "0.4.20" flexi_logger = "0.27.3" sysinfo = "0.30.1" clap = { version = "4.4.11", features = ["derive"] } +urlencoding = "2.1.3" [dependencies.hyper-rustls] version = "0.24.2" diff --git a/src/lib.rs b/src/lib.rs index 905c0a5..67c4706 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize}; use serde_json::json; use std::{sync::Mutex, path::Path}; use sysinfo::System; +use urlencoding::decode; pub use basic::S3Params; mod basic; mod download; @@ -118,7 +119,7 @@ pub fn upload( return; } }; - let params = match serde_json::from_str::(¶ms) { + let mut params = match serde_json::from_str::(¶ms) { Ok(params) => params, Err(err) => { result_callback(false, format!("parse params failed: {}", err)); @@ -126,6 +127,14 @@ pub fn upload( return; } }; + params.object = match decode(¶ms.object) { + Ok(object) => object.to_string(), + Err(err) => { + result_callback(false, format!("url decode param object failed: {}", err)); + error!("decode object failed: {}", err); + return; + } + }; runtime.spawn(async move { let result = upload::put_object(¶ms, progress_callback).await; if result.is_ok() { @@ -165,7 +174,7 @@ pub fn download(params: String, result_callback: basic::ResultCallback) { return; } }; - let params = match serde_json::from_str::(¶ms) { + let mut params = match serde_json::from_str::(¶ms) { Ok(params) => params, Err(err) => { error!("parse params failed: {}", err); @@ -173,6 +182,14 @@ pub fn download(params: String, result_callback: basic::ResultCallback) { return; } }; + params.object = match decode(¶ms.object) { + Ok(object) => object.to_string(), + Err(err) => { + result_callback(false, format!("url decode param object failed: {}", err)); + error!("decode object failed: {}", err); + return; + } + }; runtime.spawn(async move { let result = download::get_object(¶ms).await; if result.is_ok() { diff --git a/src/main.rs b/src/main.rs index fe0a598..1528328 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use log::info; use ne_s3::{download, init, uninit, upload}; use serde::{Deserialize, Serialize}; use serde_json::json; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, Condvar}; #[derive(Parser, Debug, Serialize, Deserialize)] #[command(author, version, about, long_about = None)] @@ -35,8 +35,7 @@ struct Args { log_path: String, } -#[tokio::main] -async fn main() -> Result<(), Box> { +fn main() { let args = Args::parse(); init( json!( @@ -46,11 +45,18 @@ async fn main() -> Result<(), Box> { ) .to_string(), ); + let flag = Arc::new(Mutex::new(false)); + let cond = Arc::new(Condvar::new()); match args.command.as_str() { "upload" => { - let result_callback = |success: bool, message: String| { + let cflag = flag.clone(); + let ccond = cond.clone(); + let result_callback = move |success: bool, message: String| { info!("upload finished: {}", success); info!("upload message: {}", message); + let mut lock = cflag.lock().unwrap(); + *lock = true; + ccond.notify_one(); }; let progress_callback = |progress: f64| { info!("put object progress: {:.2}%", progress); @@ -60,20 +66,32 @@ async fn main() -> Result<(), Box> { Box::new(result_callback), Arc::new(Mutex::new(progress_callback)), ); + let mut lock = flag.lock().unwrap(); + while !*lock { + lock = cond.wait(lock).unwrap(); + } } "download" => { + let cflag = flag.clone(); + let ccond = cond.clone(); download( serde_json::to_string(&args).unwrap(), - Box::new(|success: bool, message: String| { + Box::new(move |success: bool, message: String| { info!("download finished: {}", success); info!("download message: {}", message); + let mut lock = cflag.lock().unwrap(); + *lock = true; + ccond.notify_one(); }), ); + let mut lock = flag.lock().unwrap(); + while !*lock { + lock = cond.wait(lock).unwrap(); + } } _ => { println!("unknown command: {}", args.command); } } uninit(); - Ok(()) }