1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use std::collections::HashMap;
use reqwest::header::{AUTHORIZATION, DATE, HeaderMap};
use strum_macros::Display;
use anyhow::Result;
use chrono::{DateTime, Utc};

use tracing::debug;

use crate::auth::AuthAPI;
use crate::request::{RequestBuilder, RequestType};

/// OSS配置
pub struct OSS {
    key_id: String,
    key_secret: String,
    endpoint: String,
    bucket: String,
}

pub trait OSSInfo {
    fn endpoint(&self) -> String;
    fn bucket(&self) -> String;
    fn key_id(&self) -> String;
    fn key_secret(&self) -> String;
}

pub trait API {
    fn sign_url<S: AsRef<str>>(&self, key: S, build: &mut RequestBuilder) -> String;
    fn key_urlencode<S: AsRef<str>>(&self, key: S) -> String {
        key
            .as_ref()
            .split("/")
            .map(|x| urlencoding::encode(x))
            .collect::<Vec<_>>()
            .join("/")
    }

    fn format_key<S: AsRef<str>>(&self, key: S) -> String {
        let key = key.as_ref();
        if key.starts_with("/") {
            key.to_string()
        } else {
            format!("/{}", key)
        }
    }

    fn format_oss_resource_str<S: AsRef<str>>(&self, bucket: S, key: S) -> String;
}

pub trait OSSAPI: OSSInfo + API {
    /// 签名下载URL
    ///
    /// # 使用例子
    ///
    /// ```
    /// use aliyun_oss_rust_sdk::oss::{OSS, OSSAPI, RequestBuilder};
    /// let oss = OSS::from_env();//也可以使用OSS::new()方法传递参数
    /// let build = RequestBuilder::new()
    ///    //.with_cdn("https://mydomain.com")
    ///    .expire(60) //60秒链接过期
    ///   .oss_download_speed_limit(30);//限速30kb
    /// let download_url = oss.sign_download_url(
    ///     "/ipas/cn/-10/ipadump.com_imem内存修改器_1.0.0.ipa",
    ///     &build
    ///     );
    ///  println!("download_url: {}", download_url);
    /// ```
    fn sign_download_url(&self, key: &str, build: &mut RequestBuilder) -> String {
        let sign = self.sign_url(key, build);
        if let Some(cdn) = &build.cdn {
            let download_url = format!("{}{}", cdn, sign);
            debug!("download_url: {}", download_url);
            download_url
        } else {
            let download_url = format!("{}.{}{}", self.bucket(), self.endpoint(), sign);
            debug!("download_url: {}", download_url);
            download_url
        }
    }
}

impl OSSAPI for OSS {}

impl OSSInfo for OSS {
    fn endpoint(&self) -> String {
        self.endpoint.clone()
    }
    fn bucket(&self) -> String {
        self.bucket.clone()
    }

    fn key_id(&self) -> String {
        self.key_id.clone()
    }

    fn key_secret(&self) -> String {
        self.key_secret.clone()
    }
}

impl API for OSS {
    fn sign_url<S: AsRef<str>>(&self, key: S, build: &mut RequestBuilder) -> String {
        let key = self.format_key(key);
        let expiration = chrono::Local::now() + chrono::Duration::seconds(build.expire);
        build.headers.insert(DATE.to_string(), expiration.timestamp().to_string());
        let signature = self.sign(
            key.as_str(),
            &build,
        );
        debug!("signature: {}", signature);
        let mut query_parameters = HashMap::new();
        query_parameters.insert("Expires".to_string(), expiration.timestamp().to_string());
        query_parameters.insert("OSSAccessKeyId".to_string(), self.key_id.to_string());
        query_parameters.insert("Signature".to_string(), urlencoding::encode(&signature).into_owned());
        build.parameters.iter().for_each(|(k, v)| {
            query_parameters.insert(k.to_string(), urlencoding::encode(v).into_owned());
        });

        let mut params = query_parameters
            .into_iter()
            .filter(|(k, _)| k != "x-oss-ac-source-ip")
            .collect::<Vec<_>>();

        params.sort_by(|a, b| a.0.cmp(&b.0));

        format!(
            "{}?{}",
            self.key_urlencode(key),
            params.into_iter().map(|(k, v)| format!("{}={}", k, v)).collect::<Vec<String>>().join("&")
        )
    }

    fn format_oss_resource_str<S: AsRef<str>>(&self, bucket: S, key: S) -> String {
        let bucket = bucket.as_ref();
        if bucket == "" {
            format!("/{}", bucket)
        } else {
            format!("/{}{}", bucket, key.as_ref())
        }
    }
}

impl<'a> OSS {
    pub fn new<S: Into<String>>(key_id: S, key_secret: S, endpoint: S, bucket: S) -> Self {
        OSS {
            key_id: key_id.into(),
            key_secret: key_secret.into(),
            endpoint: endpoint.into(),
            bucket: bucket.into(),
        }
    }

    pub fn from_env() -> Self {
        dotenvy::dotenv().ok();
        let key_id = dotenvy::var("OSS_KEY_ID").expect("OSS_KEY_ID not found");
        let key_secret = dotenvy::var("OSS_KEY_SECRET").expect("OSS_KEY_SECRET not found");
        let endpoint = dotenvy::var("OSS_ENDPOINT").expect("OSS_ENDPOINT not found");
        let bucket = dotenvy::var("OSS_BUCKET").expect("OSS_BUCKET not found");
        OSS::new(key_id, key_secret, endpoint, bucket)
    }
    pub fn format_host<S: AsRef<str>>(&self, bucket: S, key: S) -> String {
        let key = if key.as_ref().starts_with("/") {
            key.as_ref().to_string()
        } else {
            format!("/{}", key.as_ref())
        };
        if self.endpoint().starts_with("https") {
            format!(
                "https://{}.{}{}",
                bucket.as_ref(),
                self.endpoint().replacen("https://", "", 1),
                key,
            )
        } else {
            format!(
                "http://{}.{}{}",
                bucket.as_ref(),
                self.endpoint().replacen("http://", "", 1),
                key,
            )
        }
    }

    pub fn build_request<S: AsRef<str>>(&self, key: S, mut build: RequestBuilder) -> Result<(String, HeaderMap)> {
        let host = self.format_host(self.bucket(), key.as_ref().to_string());
        let mut header = HeaderMap::new();
        let date = self.date();
        header.insert(DATE, date.parse()?);
        build.headers.insert(DATE.to_string(), date);
        let key = key.as_ref();
        let authorization = self.oss_sign(
            key,
            &build,
        );
        header.insert(AUTHORIZATION, authorization.parse()?);
        Ok((host, header))
    }
    pub fn date(&self) -> String {
        let now: DateTime<Utc> = Utc::now();
        now.format("%a, %d %b %Y %T GMT").to_string()
    }
}

// use thiserror::Error;
//
// #[derive(Error, Debug, Display)]
// pub enum FileError {
//     IoError(#[from] std::io::Error),
// }
//
// #[derive(Error, Debug, Display)]
// pub enum MyError {
//     IoError(#[from] FileError)
// }


// impl From<std::io::Error> for MyError {
//     fn from(err: std::io::Error) -> Self {
//         MyError::IoError(FileError::IoError(err))
//     }
// }

#[cfg(test)]
mod tests {
    use std::io::Read;
    use chrono::{DateTime, Utc};
    use strum_macros::Display;
    use crate::object::ObjectAPI;

    use super::*;

    #[inline]
    fn init_log() {
        tracing_subscriber::fmt()
            .with_max_level(tracing::Level::DEBUG)
            .with_line_number(true)
            .init();
    }

    pub fn date() -> String {
        let now: chrono::DateTime<chrono::Local> = chrono::Local::now();
        now.format("%a, %d %b %Y %T GMT").to_string()
    }

    fn pp<S: AsRef<str>>(s: S) {
        println!("{}", s.as_ref());
    }


    fn open_file(file_name: &str) -> anyhow::Result<String, anyhow::Error> {
        let mut file = std::fs::File::open(file_name)?;
        let mut contents = String::new();
        file.read_to_string(&mut contents)?;
        Ok(contents)
    }

    #[test]
    fn test_read_file() {
        open_file("a").unwrap();
    }

    #[test]
    fn test_sign() {
        let aa = &"".to_string();
        pp(aa);
        println!("{}", date());
        init_log();
        let oss = OSS::from_env();
        let mut build = RequestBuilder::new()
            .with_cdn("http://cdn.ipadump.com")
            .expire(60)
            .oss_download_speed_limit(30);
        oss.sign_download_url(
            "/ipas/cn/-10/ipadump.com_imem内存修改器_1.0.0.ipa",
            &mut build,
        );
    }
}