feat: updates

This commit is contained in:
2024-08-24 14:56:08 +08:00
parent bb5f88a775
commit cda5e9197d
3 changed files with 646 additions and 56 deletions

View File

@@ -4,7 +4,8 @@ use base64::Engine;
use clap::Parser;
use reqwest::{Client, Response};
use rust_util::util_io::DEFAULT_BUF_SIZE;
use rust_util::{debugging, information, opt_result, opt_value_result, simple_error, success, util_file, XResult};
use rust_util::util_time::get_current_secs;
use rust_util::{debugging, iff, information, opt_result, opt_value_result, simple_error, success, util_file, util_time, warning, XResult};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::Digest;
@@ -14,6 +15,7 @@ use std::fs;
use std::fs::File;
use std::io::{BufReader, ErrorKind, Read, Write};
use std::path::{Path, PathBuf};
use xt_oss::oss;
const OSS_SEND_FILE_CONFIG_FILE: &str = "~/.jssp/config/osssendfile.json";
const CREATE_STS_URL: &str = "https://hatter.ink/oidc/create_sts.json";
@@ -84,6 +86,7 @@ async fn main() -> XResult<()> {
let source_file = args.file.clone();
let filename = source_file.file_name().unwrap().to_str().unwrap();
let filename_ext = get_filename_ext(filename);
let temp_file = match source_file.parent() {
None => PathBuf::from(format!("{}.tmp", filename)),
Some(parent_source_file) => {
@@ -93,10 +96,41 @@ async fn main() -> XResult<()> {
information!("Encrypt file {:?} -> {:?}", source_file, temp_file);
let temp_key = encrypt(&source_file, &temp_file)?;
debugging!("Encryption temp key: {:?}", temp_key);
information!("Encrypt file: {:?} success", temp_file);
debugging!("Encryption temp key: {:?}", &temp_key);
information!("Encrypt file: {:?} success", &temp_file);
// TODO send file to OSS
let options = oss::Options::new()
.with_access_key_id(&sts.access_key_id)
.with_access_key_secret(&sts.access_key_secret)
.with_sts_token(&sts.security_token)
.with_bucket(&oss_send_file_config.bucket)
.with_region("oss-cn-shanghai");
let client = oss::Client::new(options);
let temp_oss_filename = format!("tempfiles/temp_transfer_{}.{}", util_time::get_current_millis(), filename_ext);
information!("Put object {:?} -> {}", &temp_file, &temp_oss_filename);
let put_object_data = opt_result!(opt_result!(
client.PutObject(&temp_oss_filename).execute().await,
"Put object failed: {}"), "Put object failed-2: {:?}");
if !put_object_data.status().is_success() {
return simple_error!("Put object failed, status: {}", put_object_data.status().as_u16());
}
let object_url = client.object_url(&temp_oss_filename);
println!(">> {}", object_url);
let delete_object_data = opt_result!(opt_result!(
client.DeleteObject(&temp_oss_filename).execute().await,
"Delete object failed: {}"), "Delete object failed-2: {:?}");;
if !delete_object_data.status().is_success() {
return simple_error!("Delete object failed, status: {}", delete_object_data.status().as_u16());
}
match fs::remove_file(&temp_file) {
Ok(_) => information!("Delete temp file: {:?}", &temp_file),
Err(e) => warning!("Delete temp file failed: {}", e),
}
Ok(())
}
@@ -114,6 +148,11 @@ fn load_config(config: &Option<String>) -> XResult<OssSendFileConfig> {
Ok(oss_send_config)
}
fn get_filename_ext(filename: &str) -> String {
let mut split_filename_parts: Vec<&str> = filename.split(".").collect();
split_filename_parts[split_filename_parts.len() - 1].to_string()
}
fn new_aes_key_and_nonce() -> ([u8; 16], Vec<u8>) {
let temp_key: [u8; 16] = rand::random();
let mut sha256 = Sha256::new();