1 /*
2  * Copyright 2009-2017 Alibaba Cloud All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <alibabacloud/oss/model/CopyObjectRequest.h>
19 #include <alibabacloud/oss/http/HttpType.h>
20 #include "utils/Utils.h"
21 #include <sstream>
22 using namespace AlibabaCloud::OSS;
23 
CopyObjectRequest(const std::string & bucket,const std::string & key)24 CopyObjectRequest::CopyObjectRequest(const std::string &bucket, const std::string &key):
25     OssObjectRequest(bucket, key),
26     sourceBucket_(),
27     sourceKey_()
28 {
29 }
30 
CopyObjectRequest(const std::string & bucket,const std::string & key,const ObjectMetaData & metaData)31 CopyObjectRequest::CopyObjectRequest(const std::string &bucket, const std::string &key,
32     const ObjectMetaData &metaData):
33     OssObjectRequest(bucket, key),
34     sourceBucket_(),
35     sourceKey_(),
36     metaData_(metaData)
37 {
38 }
39 
setCopySource(const std::string & srcBucket,const std::string & srcKey)40 void CopyObjectRequest::setCopySource(const std::string& srcBucket,const std::string& srcKey)
41 {
42     sourceBucket_ = srcBucket;
43     sourceKey_ = srcKey;
44 }
45 
setSourceIfMatchETag(const std::string & value)46 void CopyObjectRequest::setSourceIfMatchETag(const std::string& value)
47 {
48     metaData_.addHeader("x-oss-copy-source-if-match", value);
49 }
50 
setSourceIfNotMatchETag(const std::string & value)51 void CopyObjectRequest::setSourceIfNotMatchETag(const std::string& value)
52 {
53     metaData_.addHeader("x-oss-copy-source-if-none-match", value);
54 }
55 
setSourceIfUnModifiedSince(const std::string & value)56 void CopyObjectRequest::setSourceIfUnModifiedSince(const std::string& value)
57 {
58     metaData_.addHeader("x-oss-copy-source-if-unmodified-since", value);
59 }
60 
setSourceIfModifiedSince(const std::string & value)61 void CopyObjectRequest::setSourceIfModifiedSince(const std::string& value)
62 {
63     metaData_.addHeader("x-oss-copy-source-if-modified-since", value);
64 }
65 
setMetadataDirective(const CopyActionList & action)66 void CopyObjectRequest::setMetadataDirective(const CopyActionList& action)
67 {
68     metaData_.addHeader("x-oss-metadata-directive", ToCopyActionName(action));
69 }
70 
setAcl(const CannedAccessControlList & acl)71 void CopyObjectRequest::setAcl(const CannedAccessControlList& acl)
72 {
73     metaData_.addHeader("x-oss-object-acl", ToAclName(acl));
74 }
75 
setTagging(const std::string & value)76 void CopyObjectRequest::setTagging(const std::string& value)
77 {
78     metaData_.addHeader("x-oss-tagging", value);
79 }
80 
setTaggingDirective(const CopyActionList & action)81 void CopyObjectRequest::setTaggingDirective(const CopyActionList& action)
82 {
83     metaData_.addHeader("x-oss-tagging-directive", ToCopyActionName(action));
84 }
85 
setTrafficLimit(uint64_t value)86 void CopyObjectRequest::setTrafficLimit(uint64_t value)
87 {
88     metaData_.addHeader("x-oss-traffic-limit", std::to_string(value));
89 }
90 
specialHeaders() const91 HeaderCollection CopyObjectRequest::specialHeaders() const
92 {
93     auto headers = metaData_.toHeaderCollection();
94 
95     if (headers.find(Http::CONTENT_TYPE) == headers.end()) {
96         headers[Http::CONTENT_TYPE] = LookupMimeType(Key());
97     }
98 
99     std::string source;
100     source.append("/").append(sourceBucket_).append("/").append(UrlEncode(sourceKey_));
101     if (!versionId_.empty()) {
102         source.append("?versionId=").append(versionId_);
103     }
104     headers["x-oss-copy-source"] = source;
105 
106     auto baseHeaders = OssObjectRequest::specialHeaders();
107     headers.insert(baseHeaders.begin(), baseHeaders.end());
108 
109     return headers;
110 }
111 
specialParameters() const112 ParameterCollection CopyObjectRequest::specialParameters() const
113 {
114     return ParameterCollection();
115 }
116