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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use crate::oss;

use self::builders::{GetSymlinkBuilder, PutSymlinkBuilder};

pub mod builders {

    use std::collections::HashMap;

    use reqwest::header::{
        CACHE_CONTROL, CONTENT_DISPOSITION, CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_TYPE,
        EXPIRES,
    };

    use crate::oss::{
        self,
        api::{self, insert_custom_header, insert_header, ApiResponseFrom},
        entities::{ObjectACL, StorageClass},
        http,
    };

    #[derive(Debug, Default, Clone)]
    struct PutSymlinkBuilderHeaders<'a> {
        cache_control: Option<http::CacheControl>,
        content_disposition: Option<http::ContentDisposition>,
        content_language: Option<&'a str>,
        content_encoding: Option<http::ContentEncoding>,
        content_type: Option<&'a str>,
        expires: Option<&'a str>,
    }

    #[derive(Debug)]
    pub struct PutSymlinkBuilder<'a> {
        client: &'a oss::Client<'a>,
        object: &'a str,
        version_id: Option<&'a str>,
        symlink_target: &'a str,
        forbid_overwrite: Option<bool>,
        object_acl: Option<ObjectACL>,
        storage_class: Option<StorageClass>,
        oss_meta: HashMap<&'a str, &'a str>,
        headers: PutSymlinkBuilderHeaders<'a>,
    }

    impl<'a> PutSymlinkBuilder<'a> {
        pub(crate) fn new(client: &'a oss::Client, object: &'a str) -> Self {
            Self {
                client,
                object,
                version_id: None,
                symlink_target: Default::default(),
                forbid_overwrite: None,
                object_acl: None,
                storage_class: None,
                oss_meta: HashMap::new(),
                headers: PutSymlinkBuilderHeaders::default(),
            }
        }

        pub fn with_symlink_target(mut self, value: &'a str) -> Self {
            self.symlink_target = value;
            self
        }

        pub fn with_forbid_overwrite(mut self, value: bool) -> Self {
            self.forbid_overwrite = Some(value);
            self
        }

        pub fn with_object_acl(mut self, value: ObjectACL) -> Self {
            self.object_acl = Some(value);
            self
        }

        pub fn with_storage_class(mut self, value: StorageClass) -> Self {
            self.storage_class = Some(value);
            self
        }

        pub fn with_oss_meta(mut self, key: &'a str, value: &'a str) -> Self {
            self.oss_meta.insert(key, value);
            self
        }
        pub fn with_content_type(mut self, value: &'a str) -> Self {
            self.headers.content_type = Some(value);
            self
        }

        pub fn with_content_language(mut self, value: &'a str) -> Self {
            self.headers.content_language = Some(value);
            self
        }

        pub fn with_cache_control(mut self, value: http::CacheControl) -> Self {
            self.headers.cache_control = Some(value);
            self
        }

        pub fn with_content_disposition(mut self, value: http::ContentDisposition) -> Self {
            self.headers.content_disposition = Some(value);
            self
        }

        pub fn with_content_encoding(mut self, value: http::ContentEncoding) -> Self {
            self.headers.content_encoding = Some(value);
            self
        }

        pub fn with_expires(mut self, value: &'a str) -> Self {
            self.headers.expires = Some(value);
            self
        }

        fn headers(&self) -> http::HeaderMap {
            let mut headers = http::HeaderMap::new();
            insert_custom_header(&mut headers, "x-oss-symlink-target", self.symlink_target);
            if let Some(forbid_overwrite) = self.forbid_overwrite {
                insert_custom_header(&mut headers, "x-oss-forbid-overwrite", forbid_overwrite);
            }
            if let Some(object_acl) = &self.object_acl {
                insert_custom_header(&mut headers, "x-oss-object-acl", object_acl.to_string());
            }
            if let Some(storage_class) = &self.storage_class {
                insert_custom_header(
                    &mut headers,
                    "x-oss-storage-class",
                    storage_class.to_string(),
                );
            }
            if let Some(content_type) = &self.headers.content_type {
                insert_header(&mut headers, CONTENT_TYPE, content_type);
            }

            if let Some(content_language) = &self.headers.content_language {
                insert_header(&mut headers, CONTENT_LANGUAGE, content_language);
            }

            if let Some(cache_control) = &self.headers.cache_control {
                insert_header(&mut headers, CACHE_CONTROL, cache_control);
            }

            if let Some(content_disposition) = &self.headers.content_disposition {
                insert_header(&mut headers, CONTENT_DISPOSITION, content_disposition);
            }

            if let Some(content_encoding) = &self.headers.content_encoding {
                insert_header(&mut headers, CONTENT_ENCODING, content_encoding);
            }

            if let Some(expires) = &self.headers.expires {
                insert_header(&mut headers, EXPIRES, expires);
            }

            if !self.oss_meta.is_empty() {
                for (key, value) in &self.oss_meta {
                    insert_custom_header(&mut headers, &format!("x-oss-meta-{}", key), value);
                }
            }
            headers
        }

        pub async fn execute(&self) -> api::ApiResult {
            let mut res = format!("/{}/{}?{}", self.client.bucket(), self.object, "symlink");
            let mut url = format!("{}?{}", self.client.object_url(self.object), "symlink");
            if let Some(version_id) = self.version_id {
                res = format!("{}&versionId={}", res, version_id);
                url = format!("{}&versionId={}", url, version_id);
            }

            let headers = self.headers();

            let resp = self
                .client
                .request
                .task()
                .with_url(&url)
                .with_headers(headers)
                .with_method(http::Method::PUT)
                .with_resource(&res)
                .execute()
                .await?;

            Ok(ApiResponseFrom(resp).to_empty().await)
        }
    }

    pub struct GetSymlinkBuilder<'a> {
        client: &'a oss::Client<'a>,
        object: &'a str,
        version_id: Option<&'a str>,
    }

    impl<'a> GetSymlinkBuilder<'a> {
        pub(crate) fn new(client: &'a oss::Client, object: &'a str) -> Self {
            Self {
                client,
                object,
                version_id: None,
            }
        }

        pub fn with_version_id(mut self, value: &'a str) -> Self {
            self.version_id = Some(value);
            self
        }

        pub async fn execute(&self) -> api::ApiResult {
            let mut res = format!("/{}/{}?{}", self.client.bucket(), self.object, "symlink");
            let mut url = { format!("{}?{}", self.client.object_url(self.object), "symlink") };
            if let Some(version_id) = self.version_id {
                res = format!("{}&versionId={}", res, version_id);
                url = format!("{}&versionId={}", url, version_id);
            }

            let resp = self
                .client
                .request
                .task()
                .with_url(&url)
                .with_resource(&res)
                .execute()
                .await?;

            Ok(ApiResponseFrom(resp).to_empty().await)
        }
    }
}

/// # 软链接`Symlink``
#[allow(non_snake_case)]
impl<'a> oss::Client<'a> {
    /// 调用PutSymlink接口用于为OSS的目标文件`TargetObject`创建软链接
    /// `Symlink`,您可以通过该软链接访问TargetObject。
    ///
    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/putsymlink)
    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_object_symlink_put.rs)
    pub fn PutSymlink(&self, object: &'a str) -> PutSymlinkBuilder<'_> {
        PutSymlinkBuilder::new(self, object)
    }

    /// 调用GetSymlink接口获取软链接。此操作需要您对该软链接有读权限。
    ///
    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/getsymlink)
    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_object_symlink_get.rs)
    pub fn GetSymlink(&self, object: &'a str) -> GetSymlinkBuilder<'_> {
        GetSymlinkBuilder::new(self, object)
    }
}