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
use crate::oss;
use builders::{GetObjectAclBuilder, PutObjectACLBuilder};

pub mod builders {
    use crate::oss::{
        self,
        api::{self, ApiResponseFrom},
        entities::{acl::AccessControlPolicy, ObjectACL},
        http,
    };

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

    #[allow(unused)]
    impl<'a> PutObjectACLBuilder<'a> {
        pub(crate) fn new(client: &'a oss::Client, object: &'a str) -> Self {
            Self {
                client,
                object,
                version_id: None,
                acl: ObjectACL::Default,
            }
        }

        pub fn with_acl(mut self, acl: ObjectACL) -> Self {
            self.acl = acl;
            self
        }

        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, "acl");
            let mut url = { format!("{}?{}", self.client.object_url(self.object), "acl") };
            if let Some(version_id) = self.version_id {
                res = format!("{}&versionId={}", res, version_id);
                url = format!("{}&versionId={}", url, version_id);
            }

            let mut headers = http::HeaderMap::new();
            headers.insert("x-oss-object-acl", self.acl.to_string().parse().unwrap());

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

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

    impl<'a> GetObjectAclBuilder<'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<AccessControlPolicy> {
            let mut res = format!("/{}/{}?{}", self.client.bucket(), self.object, "acl");
            let mut url = { format!("{}?{}", self.client.object_url(self.object), "acl") };
            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_type().await)
        }
    }
}

/// # 基础操作
#[allow(non_snake_case)]
impl<'a> oss::Client<'a> {
    /// 调用PutObjectACL接口修改文件`Object`的访问权限`ACL`。
    /// 此操作只有Bucket Owner有权限执行,且需对Object有读写权限。
    ///
    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/putobjectacl)
    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_object_acl_put.rs)
    pub fn PutObjectACL(&self, object: &'a str) -> PutObjectACLBuilder {
        PutObjectACLBuilder::new(self, object)
    }

    /// 调用GetObjectACL接口获取存储空间`Bucket`下某个文件`Object`的访问权限`ACL`。
    ///
    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/getobjectacl)
    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_object_acl_get.rs)
    pub fn GetObjectACL(&self, object: &'a str) -> GetObjectAclBuilder {
        GetObjectAclBuilder::new(self, object)
    }
}