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 #include <alibabacloud/oss/model/UploadPartCopyRequest.h>
18 #include <sstream>
19 #include "utils/Utils.h"
20 #include "ModelError.h"
21 #include <alibabacloud/oss/Const.h>
22 
23 using namespace AlibabaCloud::OSS;
24 using std::stringstream;
25 
26 
UploadPartCopyRequest(const std::string & bucket,const std::string & key)27 UploadPartCopyRequest::UploadPartCopyRequest(const std::string &bucket, const std::string &key) :
28     UploadPartCopyRequest(bucket, key, std::string(), std::string(), std::string(), 0)
29 {
30 }
31 
UploadPartCopyRequest(const std::string & bucket,const std::string & key,const std::string & srcBucket,const std::string & srcKey)32 UploadPartCopyRequest::UploadPartCopyRequest(const std::string &bucket, const std::string &key,
33     const std::string &srcBucket, const std::string &srcKey) :
34     UploadPartCopyRequest(bucket, key, srcBucket, srcKey, std::string(), 0)
35 {
36 }
37 
UploadPartCopyRequest(const std::string & bucket,const std::string & key,const std::string & srcBucket,const std::string & srcKey,const std::string & uploadId,int partNumber)38 UploadPartCopyRequest::UploadPartCopyRequest(const std::string &bucket, const std::string &key,
39     const std::string &srcBucket, const std::string &srcKey,
40     const std::string &uploadId, int partNumber):
41     OssObjectRequest(bucket, key),
42     uploadId_(uploadId),
43     sourceBucket_(srcBucket),
44     sourceKey_(srcKey),
45     partNumber_(partNumber),
46     sourceRangeIsSet_(false),
47     sourceIfMatchETagIsSet_(false),
48     sourceIfNotMatchETagIsSet_(false),
49     sourceIfModifiedSinceIsSet_(false),
50     sourceIfUnModifiedSinceIsSet_(false),
51     trafficLimit_(0)
52 {
53 }
54 
UploadPartCopyRequest(const std::string & bucket,const std::string & key,const std::string & srcBucket,const std::string & srcKey,const std::string & uploadId,int partNumber,const std::string & sourceIfMatchETag,const std::string & sourceIfNotMatchETag,const std::string & sourceIfModifiedSince,const std::string & sourceIfUnModifiedSince)55 UploadPartCopyRequest::UploadPartCopyRequest(const std::string& bucket, const std::string& key,
56     const std::string& srcBucket, const std::string& srcKey,
57     const std::string& uploadId, int partNumber,
58     const std::string& sourceIfMatchETag,
59     const std::string& sourceIfNotMatchETag,
60     const std::string& sourceIfModifiedSince,
61     const std::string& sourceIfUnModifiedSince) :
62     OssObjectRequest(bucket, key),
63     uploadId_(uploadId),
64     sourceBucket_(srcBucket),
65     sourceKey_(srcKey),
66     partNumber_(partNumber),
67     sourceRangeIsSet_(false),
68     trafficLimit_(0)
69 {
70     if (!sourceIfMatchETag.empty()) {
71         sourceIfMatchETag_ = sourceIfMatchETag;
72         sourceIfMatchETagIsSet_ = true;
73     }
74     else {
75         sourceIfMatchETagIsSet_ = false;
76     }
77 
78     if (!sourceIfNotMatchETag.empty()) {
79         sourceIfNotMatchETag_ = sourceIfNotMatchETag;
80         sourceIfNotMatchETagIsSet_ = true;
81     }
82     else {
83         sourceIfNotMatchETagIsSet_ = false;
84     }
85 
86     if (!sourceIfModifiedSince.empty()) {
87         sourceIfModifiedSince_ = sourceIfModifiedSince;
88         sourceIfModifiedSinceIsSet_ = true;
89     }
90     else {
91         sourceIfModifiedSinceIsSet_ = false;
92     }
93 
94     if (!sourceIfUnModifiedSince.empty()) {
95         sourceIfUnModifiedSince_ = sourceIfUnModifiedSince;
96         sourceIfUnModifiedSinceIsSet_ = true;
97     }
98     else {
99         sourceIfUnModifiedSinceIsSet_ = false;
100     }
101 }
102 
setPartNumber(uint32_t partNumber)103 void UploadPartCopyRequest::setPartNumber(uint32_t partNumber)
104 {
105     partNumber_ = partNumber;
106 }
107 
setUploadId(const std::string & uploadId)108 void UploadPartCopyRequest::setUploadId(const std::string &uploadId)
109 {
110     uploadId_ = uploadId;
111 }
112 
SetCopySource(const std::string & srcBucket,const std::string & srcKey)113 void UploadPartCopyRequest::SetCopySource(const std::string& srcBucket, const std::string& srcKey)
114 {
115     sourceBucket_ = srcBucket;
116     sourceKey_ = srcKey;
117 }
118 
setCopySourceRange(uint64_t begin,uint64_t end)119 void UploadPartCopyRequest::setCopySourceRange(uint64_t begin, uint64_t end)
120 {
121     sourceRange_[0] = begin;
122     sourceRange_[1] = end;
123     sourceRangeIsSet_ = true;
124 }
125 
SetSourceIfMatchETag(const std::string & value)126 void UploadPartCopyRequest::SetSourceIfMatchETag(const std::string& value)
127 {
128     sourceIfMatchETag_ = value;
129     sourceIfMatchETagIsSet_ = true;
130 }
131 
SetSourceIfNotMatchETag(const std::string & value)132 void UploadPartCopyRequest::SetSourceIfNotMatchETag(const std::string& value)
133 {
134     sourceIfNotMatchETag_ = value;
135     sourceIfNotMatchETagIsSet_ = true;
136 }
137 
SetSourceIfModifiedSince(const std::string & value)138 void UploadPartCopyRequest::SetSourceIfModifiedSince(const std::string& value)
139 {
140     sourceIfModifiedSince_ = value;
141     sourceIfModifiedSinceIsSet_ = true;
142 }
143 
SetSourceIfUnModifiedSince(const std::string & value)144 void UploadPartCopyRequest::SetSourceIfUnModifiedSince(const std::string& value)
145 {
146     sourceIfUnModifiedSince_ = value;
147     sourceIfUnModifiedSinceIsSet_ = true;
148 }
149 
setTrafficLimit(uint64_t value)150 void UploadPartCopyRequest::setTrafficLimit(uint64_t value)
151 {
152     trafficLimit_ = value;
153 }
154 
specialParameters() const155 ParameterCollection UploadPartCopyRequest::specialParameters() const
156 {
157     ParameterCollection parameters;
158     parameters["partNumber"] = std::to_string(partNumber_);
159     parameters["uploadId"] = uploadId_;
160     return parameters;
161 }
162 
specialHeaders() const163 HeaderCollection UploadPartCopyRequest::specialHeaders() const
164 {
165     auto headers = OssObjectRequest::specialHeaders();
166     std::string source;
167     source.append("/").append(sourceBucket_).append("/").append(UrlEncode(sourceKey_));
168     if (!versionId_.empty()) {
169         source.append("?versionId=").append(versionId_);
170     }
171     headers["x-oss-copy-source"] = source;
172 
173     if (sourceRangeIsSet_) {
174         std::string range("bytes=");
175         range.append(std::to_string(sourceRange_[0])).append("-");
176         if (sourceRange_[1] > 0){
177             range.append(std::to_string(sourceRange_[1]));
178         }
179         headers["x-oss-copy-source-range"] = range;
180     }
181 
182     if(sourceIfMatchETagIsSet_) {
183         headers["x-oss-copy-source-if-match"] = sourceIfMatchETag_;
184     }
185 
186     if(sourceIfNotMatchETagIsSet_) {
187         headers["x-oss-copy-source-if-none-match"] = sourceIfNotMatchETag_;
188     }
189 
190     if(sourceIfModifiedSinceIsSet_) {
191         headers["x-oss-copy-source-if-modified-since"] = sourceIfModifiedSince_;
192     }
193 
194     if (sourceIfUnModifiedSinceIsSet_) {
195         headers["x-oss-copy-source-if-unmodified-since"] = sourceIfUnModifiedSince_;
196     }
197 
198     if (trafficLimit_ != 0) {
199         headers["x-oss-traffic-limit"] = std::to_string(trafficLimit_);
200     }
201 
202     return headers;
203 }
204 
validate() const205 int UploadPartCopyRequest::validate() const
206 {
207     int ret = OssObjectRequest::validate();
208     if (ret != 0){
209         return ret;
210     }
211 
212     if (!IsValidBucketName(sourceBucket_)) {
213         return ARG_ERROR_BUCKET_NAME;
214     }
215 
216     if (!IsValidObjectKey(sourceKey_)) {
217         return ARG_ERROR_OBJECT_NAME;
218     }
219 
220     if (sourceRangeIsSet_ &&
221           ((sourceRange_[1] < sourceRange_[0]) ||
222            ((sourceRange_[1] - sourceRange_[0] + 1) > MaxFileSize))) {
223         return ARG_ERROR_MULTIPARTUPLOAD_PARTSIZE_RANGE;
224     }
225 
226     if(!(partNumber_ > 0 && partNumber_ < PartNumberUpperLimit)){
227         return ARG_ERROR_MULTIPARTUPLOAD_PARTNUMBER_RANGE;
228     }
229 
230     return 0;
231 }
232