Skip to content

Commit

Permalink
feat: upload & download url decode object
Browse files Browse the repository at this point in the history
  • Loading branch information
Zvicii committed Dec 28, 2023
1 parent dcd1337 commit bf907db
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
Expand All @@ -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"
Expand Down
21 changes: 19 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -118,14 +119,22 @@ pub fn upload(
return;
}
};
let params = match serde_json::from_str::<basic::S3Params>(&params) {
let mut params = match serde_json::from_str::<basic::S3Params>(&params) {
Ok(params) => params,
Err(err) => {
result_callback(false, format!("parse params failed: {}", err));
error!("parse params failed: {}", err);
return;
}
};
params.object = match decode(&params.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(&params, progress_callback).await;
if result.is_ok() {
Expand Down Expand Up @@ -165,14 +174,22 @@ pub fn download(params: String, result_callback: basic::ResultCallback) {
return;
}
};
let params = match serde_json::from_str::<basic::S3Params>(&params) {
let mut params = match serde_json::from_str::<basic::S3Params>(&params) {
Ok(params) => params,
Err(err) => {
error!("parse params failed: {}", err);
result_callback(false, format!("parse params failed: {}", err));
return;
}
};
params.object = match decode(&params.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(&params).await;
if result.is_ok() {
Expand Down
30 changes: 24 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -35,8 +35,7 @@ struct Args {
log_path: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
let args = Args::parse();
init(
json!(
Expand All @@ -46,11 +45,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.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);
Expand All @@ -60,20 +66,32 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}

0 comments on commit bf907db

Please sign in to comment.