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
use std::{mem,str};
use std::collections::HashMap;
use chrono::prelude::*;
use reqwest::Client;
use reqwest::r#async as async_reqwest;
use reqwest::header::{DATE, HeaderMap};
use futures::{Future, Stream};

use failure::Error;
use super::auth::*;
use super::utils::*;

#[derive(Clone, Debug)]
pub struct OSS {
	key_id: String,
	key_secret: String,
	endpoint: String,
	bucket: String,
	pub client: Client
}

const RESOURCES: [&str; 50] = ["acl", "uploads", "location", "cors", "logging", "website", "referer","lifecycle", "delete", "append",
	"tagging", "objectMeta", "uploadId", "partNumber", "security-token","position", "img", "style", "styleName", "replication",
	 "replicationProgress", "replicationLocation","cname", "bucketInfo", "comp", "qos", "live", "status", "vod", "startTime",
	 "endTime", "symlink","x-oss-process", "response-content-type", "response-content-language", "response-expires", "response-cache-control","response-content-disposition", "response-content-encoding", "udf",
	 "udfName", "udfImage", "udfId", "udfImageDesc","udfApplication", "comp", "udfApplicationLog", "restore", "callback", "callback-var"];

impl OSS {
	pub fn new(key_id: &str, key_secret: &str, endpoint: &str, bucket: &str) -> Self {
		OSS {
			key_id: key_id.to_string(),
			key_secret: key_secret.to_string(),
			endpoint: endpoint.to_string(),
			bucket: bucket.to_string(),
			client: Client::new()
		}
	}

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

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

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

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

	pub fn set_bucket(&mut self, bucket: &str) {
		self.bucket = bucket.to_string()
	}

	pub fn host(&self, bucket: &str, object: &str, resources_str: &str) -> String {
		let host = if self.endpoint.starts_with("https") {
			format!("https://{}.{}/{}?{}", bucket, self.endpoint.replacen("https://", "", 1),object, resources_str)
		} else {
			format!("http://{}.{}/{}?{}", bucket, self.endpoint.replacen("http://", "", 1), object, resources_str)
		};
		host
	}

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

	pub fn get_resources_str(&self, params: HashMap<String, Option<String>>) -> String {
		let mut resources: Vec<(&String, &Option<String>)> = params.iter().filter(|(k, _)| RESOURCES.contains(&k.as_str())).collect();
		resources.sort_by(|a, b| a.0.to_string().cmp(&b.0.to_string()));
		let mut result = String::new();
		for (k, v) in resources {
			if result.len() > 0 {
				result += "&";
			}
			if let Some(vv) = v {
				result += &format!("{}={}", k.to_owned(), vv);
			} else {
				result += &k;
			}
		}
		result
	}

	pub fn async_get_object(&self, object: &str, headers: Option<HashMap<&str, &str>>, resources: Option<HashMap<String, Option<String>>>) -> impl Future<Item=String, Error=Error> {
		let resources_str = if let Some(r) = resources {
			self.get_resources_str(r)
		} else {
			String::new()
		};
		let host = self.host(self.bucket(), object, &resources_str);
		let date = self.date();
		let mut headers = if let Some(h) = headers { to_headers(h).unwrap() } else { HeaderMap::new() };
		headers.insert(DATE, date.parse().unwrap());
		let authorization = self.oss_sign("GET", self.key_id(), self.key_secret(), self.bucket(), object, &resources_str, &headers);
		headers.insert("Authorization", authorization.parse().unwrap());

		async_reqwest::Client::new().get(&host)
			.headers(headers)
			.send()
			.and_then(|mut resp| {
				let body = mem::replace(resp.body_mut(), async_reqwest::Decoder::empty());
				body.concat2()
			})
			.map_err(|err| {
				err.into()
			})
			.map(|body| {
				str::from_utf8(&body).unwrap().to_string()
			})
	}


	pub fn async_put_object_from_buffer(&self, buf: &[u8], object: &str, headers: Option<HashMap<&str, &str>>, resources: Option<HashMap<String, Option<String>>>) -> impl Future<Item=(async_reqwest::Response), Error=Error> {
		let resources_str = if let Some(r) = resources {
			self.get_resources_str(r)
		} else {
			String::new()
		};
		let host = self.host(self.bucket(), object, &resources_str);
		let date = self.date();

		let mut headers = if let Some(h) = headers { to_headers(h).unwrap() } else { HeaderMap::new() };
		headers.insert(DATE, date.parse().unwrap());
		let authorization = self.oss_sign("PUT", self.key_id(), self.key_secret(), self.bucket(), object, &resources_str, &headers);
		headers.insert("Authorization", authorization.parse().unwrap());

		async_reqwest::Client::new().put(&host)
			.headers(headers)
			.body(buf.to_owned())
			.send()
			.map(|resp| {
				resp
			})
			.map_err(|err| {
				err.into()
			})
	}
}