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
use std::collections::HashMap;
use std::fs;
use json::{JsonValue, object};
use crate::mode::OssMode;

mod mode;

#[derive(Clone)]
pub struct Oss {
    default: String,
    connections: HashMap<String, Connection>,
}

impl Oss {
    pub fn new(path: &str) -> Self {
        let text = fs::read_to_string(path).unwrap_or("".to_string());
        let json = {
            if text == "" {
                let mut def = Oss::default();
                let json = def.json();
                fs::write(path, json.to_string()).unwrap();
                json
            } else {
                json::parse(&*text.clone()).unwrap()
            }
        };
        Oss::from(json)
    }
    pub fn from(data: JsonValue) -> Self {
        let default = data["default"].to_string();
        let mut connections = HashMap::new();
        for (key, value) in data["connections"].entries() {
            let connection = Connection::default().from(value.clone()).clone();
            connections.insert(key.to_string(), connection.clone());
        }
        Self {
            default,
            connections,
        }
    }
    pub fn default() -> Self {
        let mut connections = HashMap::new();
        connections.insert("my_name".to_string(), Connection::default());
        Self {
            default: "my_name".to_string(),
            connections,
        }
    }
    fn json(&mut self) -> JsonValue {
        let mut data = object! {};
        data["default"] = self.default.clone().into();
        let mut connections = object! {};
        for (name, connection) in self.connections.iter_mut() {
            connections[name.clone()] = connection.to_json().clone();
        }
        data["connections"] = connections;
        data
    }
    pub fn connection(&mut self, name: &str) -> &mut Self {
        self.default = name.to_string();
        self
    }

    /// 推送
    /// path 地址路径
    pub fn put(&mut self, filename: &str, path: &str) -> Result<String, String> {
        match self.connections.get(&*self.default).unwrap().mode {
            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).put(filename, path)
        }
    }
    /// 获取
    pub fn get(&mut self, name: &str) -> Result<Vec<u8>, String> {
        match self.connections.get(&*self.default).unwrap().mode {
            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).get(name)
        }
    }
    /// 删除
    pub fn del(&mut self, name: &str) -> Result<bool, String> {
        match self.connections.get(&*self.default).unwrap().mode {
            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).del(name)
        }
    }
    /// 判断文件是否存在
    pub fn get_head(&mut self, name: &str) -> Result<String, String> {
        match self.connections.get(&*self.default).unwrap().mode {
            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).get_head(name)
        }
    }
    pub fn get_head_meta(&mut self, name: &str) ->Result<JsonValue, String>  {
        match self.connections.get(&*self.default).unwrap().mode {
            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).get_head_meta(name)
        }
    }
}


#[derive(Clone)]
pub struct Connection {
    // Endpoint以杭州为例,其它Region请按实际情况填写。 https://oss-cn-hangzhou.aliyuncs.com
    pub endpoint: String,
    pub access_key_id: String,
    pub access_key_secret: String,
    pub mode: Mode,
    pub bucket_name: String,
}

impl Connection {
    pub fn default() -> Connection {
        Self {
            mode: Mode::Aliyun,
            endpoint: "".to_string(),
            access_key_id: "".to_string(),
            access_key_secret: "".to_string(),
            bucket_name: "".to_string(),
        }
    }
    pub fn to_json(&mut self) -> JsonValue {
        let mut data = object! {};
        data["mode"] = self.mode.to_str().into();
        data["endpoint"] = self.endpoint.clone().into();
        data["access_key_id"] = self.access_key_id.clone().into();
        data["access_key_secret"] = self.access_key_secret.clone().into();
        data["bucket_name"] = self.bucket_name.clone().into();
        data
    }
    pub fn from(&mut self, data: JsonValue) -> &mut Connection {
        self.mode = Mode::from(data["mode"].as_str().unwrap());
        self.endpoint = data["endpoint"].to_string();
        self.access_key_id = data["access_key_id"].to_string();
        self.access_key_secret = data["access_key_secret"].to_string();
        self.bucket_name = data["bucket_name"].to_string();
        self
    }
}


#[derive(Clone)]
pub enum Mode {
    Aliyun
}

impl Mode {
    pub fn to_str(&mut self) -> &'static str {
        match self {
            Mode::Aliyun => "aliyun",
        }
    }
    pub fn from(name: &str) -> Self {
        match name {
            "aliyun" => Mode::Aliyun,
            _ => Mode::Aliyun
        }
    }
}