br_oss/
lib.rs

1use std::collections::HashMap;
2use std::fs;
3use json::{JsonValue, object};
4use crate::mode::OssMode;
5
6mod mode;
7
8#[derive(Clone)]
9pub struct Oss {
10    default: String,
11    connections: HashMap<String, Connection>,
12}
13impl Default for Oss {
14    fn default() -> Self {
15        let mut connections = HashMap::new();
16        connections.insert("my_name".to_string(), Connection::new());
17        Self {
18            default: "my_name".to_string(),
19            connections,
20        }
21    }
22}
23impl Oss {
24    pub fn new(path: &str) -> Self {
25        let text = fs::read_to_string(path).unwrap_or("".to_string());
26        let json = {
27            if text.is_empty() {
28                let mut def = Oss::default();
29                let json = def.json();
30                fs::write(path, json.to_string()).unwrap();
31                json
32            } else {
33                json::parse(&text.clone()).unwrap()
34            }
35        };
36        Oss::from(json)
37    }
38    pub fn from(data: JsonValue) -> Self {
39        let default = data["default"].to_string();
40        let mut connections = HashMap::new();
41        for (key, value) in data["connections"].entries() {
42            let connection = Connection::new().from(value.clone()).clone();
43            connections.insert(key.to_string(), connection.clone());
44        }
45        Self {
46            default,
47            connections,
48        }
49    }
50    fn json(&mut self) -> JsonValue {
51        let mut data = object! {};
52        data["default"] = self.default.clone().into();
53        let mut connections = object! {};
54        for (name, connection) in self.connections.iter_mut() {
55            connections[name.clone()] = connection.to_json().clone();
56        }
57        data["connections"] = connections;
58        data
59    }
60    pub fn connection(&mut self, name: &str) -> &mut Self {
61        self.default = name.to_string();
62        self
63    }
64
65    /// 推送
66    /// path 地址路径
67    pub fn put(&mut self, filename: &str, path: &str) -> Result<String, String> {
68        match self.connections.get(&*self.default).unwrap().mode {
69            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).put(filename, path)
70        }
71    }
72    /// 获取
73    pub fn get(&mut self, name: &str) -> Result<Vec<u8>, String> {
74        match self.connections.get(&*self.default).unwrap().mode {
75            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).get(name)
76        }
77    }
78    /// 删除
79    pub fn del(&mut self, name: &str) -> Result<bool, String> {
80        match self.connections.get(&*self.default).unwrap().mode {
81            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).del(name)
82        }
83    }
84    /// 判断文件是否存在
85    pub fn get_head(&mut self, name: &str) -> Result<String, String> {
86        match self.connections.get(&*self.default).unwrap().mode {
87            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).get_head(name)
88        }
89    }
90    pub fn get_head_meta(&mut self, name: &str) -> Result<JsonValue, String> {
91        match self.connections.get(&*self.default).unwrap().mode {
92            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).get_head_meta(name)
93        }
94    }
95}
96
97
98#[derive(Clone)]
99pub struct Connection {
100    // Endpoint以杭州为例,其它Region请按实际情况填写。 https://oss-cn-hangzhou.aliyuncs.com
101    pub endpoint: String,
102    pub access_key_id: String,
103    pub access_key_secret: String,
104    pub mode: Mode,
105    pub bucket_name: String,
106}
107
108impl Default for Connection {
109    fn default() -> Self {
110        Self::new()
111    }
112}
113
114impl Connection {
115    pub fn new() -> Connection {
116        Self {
117            mode: Mode::Aliyun,
118            endpoint: "".to_string(),
119            access_key_id: "".to_string(),
120            access_key_secret: "".to_string(),
121            bucket_name: "".to_string(),
122        }
123    }
124    pub fn to_json(&mut self) -> JsonValue {
125        let mut data = object! {};
126        data["mode"] = self.mode.to_str().into();
127        data["endpoint"] = self.endpoint.clone().into();
128        data["access_key_id"] = self.access_key_id.clone().into();
129        data["access_key_secret"] = self.access_key_secret.clone().into();
130        data["bucket_name"] = self.bucket_name.clone().into();
131        data
132    }
133    pub fn from(&mut self, data: JsonValue) -> &mut Connection {
134        self.mode = Mode::from(data["mode"].as_str().unwrap());
135        self.endpoint = data["endpoint"].to_string();
136        self.access_key_id = data["access_key_id"].to_string();
137        self.access_key_secret = data["access_key_secret"].to_string();
138        self.bucket_name = data["bucket_name"].to_string();
139        self
140    }
141}
142
143
144#[derive(Clone)]
145pub enum Mode {
146    Aliyun
147}
148
149impl Mode {
150    pub fn to_str(&mut self) -> &'static str {
151        match self {
152            Mode::Aliyun => "aliyun",
153        }
154    }
155    pub fn from(name: &str) -> Self {
156        match name {
157            "aliyun" => Mode::Aliyun,
158            _ => Mode::Aliyun
159        }
160    }
161}