From 75f56cb5e26adf6a72d30cfc635c51b441310a09 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 30 Mar 2020 01:28:20 +0800 Subject: [PATCH] add remove http:// or https:// --- src/main.rs | 3 ++- src/oss_util.rs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c92351c..e7a6646 100644 --- a/src/main.rs +++ b/src/main.rs @@ -175,7 +175,8 @@ fn main() -> XResult<()> { print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index)); fs::remove_file(temp_zip_file).ok(); fs::remove_file(temp_pgp_file).ok(); - continue;}, + continue; + }, Ok(file_temp_pgp_file) => file_temp_pgp_file, }; diff --git a/src/oss_util.rs b/src/oss_util.rs index 9afb700..4f7c7af 100644 --- a/src/oss_util.rs +++ b/src/oss_util.rs @@ -77,7 +77,9 @@ impl<'a> OSSClient<'a> { pub fn generate_signed_url(&self, verb: &str, bucket_name: &str, key: &str, expire_in_seconds: u64, is_https: bool) -> String { let mut signed_url = String::with_capacity(1024); signed_url.push_str(if is_https { "https://" } else { "http://" }); - signed_url.push_str(&format!("{}.{}/{}", bucket_name, self.endpoint, key)); + + let endpoint = &remove_endpoint_http_or_s(self.endpoint); + signed_url.push_str(&format!("{}.{}/{}", bucket_name, endpoint, key)); let current_secs = get_current_secs(); let expire_secs = current_secs + expire_in_seconds; @@ -96,6 +98,16 @@ impl<'a> OSSClient<'a> { } } +fn remove_endpoint_http_or_s(endpoint: &str) -> String { + let mut endpoint = endpoint.to_owned(); + if endpoint.starts_with("http://") { + endpoint = endpoint.chars().skip("http://".chars().count()).collect::(); + } else if endpoint.starts_with("https://") { + endpoint = endpoint.chars().skip("https://".chars().count()).collect::(); + } + endpoint +} + fn get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str) -> String { let mut to_be_signed = String::with_capacity(512); to_be_signed.push_str(verb);