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
use std::time::Duration;

#[derive(Debug,Clone)]
pub enum SignatureType {
    V2,
    V4,
    OBS,
}

#[derive(Debug, Default, Clone)]
pub struct SecurityHolder {
    ak: String,
    sk: String,
    security_token: String,
}
impl SecurityHolder {
    pub fn new(ak: String, sk: String, security_token: String) -> Self {
        Self {
            ak,
            sk,
            security_token,
        }
    }

    pub fn ak(&self) -> &str {
        self.ak.as_ref()
    }

    pub fn sk(&self) -> &str {
        self.sk.as_ref()
    }

    pub fn security_token(&self) -> &str {
        self.security_token.as_ref()
    }
}

#[derive(Debug,Clone)]
pub struct Config {
    pub(crate) security_providers: Vec<SecurityHolder>,
    pub(crate) endpoint: String,
    pub(crate) is_secure: bool,
    pub(crate) region: String,
    pub(crate) timeout: Duration,
    pub(crate) signature_type: SignatureType,
}

impl Config {
    pub fn security_providers(&self) -> &[SecurityHolder] {
        self.security_providers.as_ref()
    }

    /// 暂时不支持自定义域名
    pub fn canonicalized_url(&self, bucket_name: &str, uri: &str) -> String {
        if bucket_name.is_empty() {
            String::from("/")
        } else {
            match self.signature_type {
                SignatureType::V2 | SignatureType::OBS => {
                    if uri.is_empty() {
                        format!("/{}/", bucket_name)
                    } else {
                        format!("/{}/{}", bucket_name, uri)
                    }
                }
                SignatureType::V4 => String::from("/"),
            }
        }
    }

    pub(crate) fn set_security_providers(&mut self, security_providers: Vec<SecurityHolder>) {
        self.security_providers = security_providers;
    }

    pub(crate) fn set_endpoint(&mut self, endpoint: String) {
        self.endpoint = endpoint;
    }

    pub(crate) fn set_is_secure(&mut self, is_secure: bool) {
        self.is_secure = is_secure;
    }

    pub(crate) fn set_region(&mut self, region: String) {
        self.region = region;
    }

    pub(crate) fn set_timeout(&mut self, timeout: Duration) {
        self.timeout = timeout;
    }

    pub(crate) fn set_signature_type(&mut self, signature_type: SignatureType) {
        self.signature_type = signature_type;
    }

    pub(crate) fn timeout(&self) -> Duration {
        self.timeout
    }

    pub fn endpoint(&self) -> &str {
        self.endpoint.as_ref()
    }
}