aliyun_oss_rust_sdk/
metadata.rs

1use std::collections::HashMap;
2use chrono::{DateTime, Utc};
3use reqwest::header::HeaderMap;
4use crate::debug;
5
6#[derive(Debug)]
7pub struct ObjectMetadata {
8    pub(crate) metadata: HashMap<String, String>,
9}
10
11unsafe impl Send for ObjectMetadata {}
12
13unsafe impl Sync for ObjectMetadata {}
14
15impl ObjectMetadata {
16    pub fn new(headers: &HeaderMap) -> ObjectMetadata {
17        let mut metadata = HashMap::new();
18        let mut user_metadata = HashMap::new();
19
20        for (key, value) in headers.iter() {
21            let key = key.as_str().to_string().to_lowercase();
22            let value = value.to_str().unwrap().to_string();
23            if key.starts_with("x-oss-meta-") {
24                user_metadata.insert(key[11..].to_string(), value);
25            } else if key == "etag" {
26                let value = value.trim_matches('"').to_string();
27                metadata.insert("etag".to_string(), value);
28            } else {
29                metadata.insert(key, value);
30            }
31        }
32
33        ObjectMetadata {
34            metadata,
35        }
36    }
37
38    pub fn last_modified(&self) -> Option<DateTime<Utc>> {
39        let val = self.metadata.get("last-modified");
40        if val.is_none() {
41            debug!("Can't find <last-modified>.");
42            return None;
43        }
44        let result = chrono::DateTime::parse_from_rfc2822(val.unwrap()).map(|dt| dt.with_timezone(&chrono::Utc));
45        return match result {
46            Ok(date) => {
47                Some(date)
48            }
49            Err(e) => {
50                debug!("Last modified parsed failed.{}", e);
51                None
52            }
53        };
54    }
55
56    pub fn expiration_time(&self) -> Option<DateTime<Utc>> {
57        let val = self.metadata.get("x-oss-expiration");
58        if val.is_none() {
59            debug!("Can't find <x-oss-expiration>.");
60            return None;
61        }
62        let result = chrono::DateTime::parse_from_rfc2822(val.unwrap()).map(|dt| dt.with_timezone(&Utc));
63        return match result {
64            Ok(date) => {
65                Some(date)
66            }
67            Err(e) => {
68                debug!("Expiration time parsed failed.{}", e);
69                None
70            }
71        };
72    }
73    pub fn content_md5(&self) -> Option<String> {
74        self.metadata.get("content-md5").map(|s| s.to_string())
75    }
76    pub fn etag(&self) -> Option<String> {
77        self.metadata.get("etag").map(|s| s.to_string())
78    }
79    pub fn content_length(&self) -> Option<String> {
80        self.metadata.get("content-length").map(|s| s.to_string())
81    }
82    pub fn content_type(&self) -> Option<String> {
83        self.metadata.get("content-type").map(|s| s.to_string())
84    }
85    pub fn content_encoding(&self) -> Option<String> {
86        self.metadata.get("content-encoding").map(|s| s.to_string())
87    }
88    pub fn content_disposition(&self) -> Option<String> {
89        self.metadata.get("content-disposition").map(|s| s.to_string())
90    }
91    pub fn cache_control(&self) -> Option<String> {
92        self.metadata.get("cache-control").map(|s| s.to_string())
93    }
94    pub fn crc64(&self) -> Option<String> {
95        self.metadata.get("x-oss-hash-crc64ecma").map(|s| s.to_string())
96    }
97    pub fn server_side_encryption(&self) -> Option<String> {
98        self.metadata.get("x-oss-server-side-encryption").map(|s| s.to_string())
99    }
100    pub fn object_type(&self) -> Option<String> {
101        self.metadata.get("x-oss-object-type").map(|s| s.to_string())
102    }
103}