@@ -38,10 +38,10 @@ discard block |
||
| 38 | 38 | return $this->_errorResponse("FileNotExist", "file not exist"); |
| 39 | 39 | } |
| 40 | 40 | if (empty($uploadOption)) { |
| 41 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
| 41 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
| 42 | 42 | } |
| 43 | 43 | if (empty($uploadOption->name)) { |
| 44 | - $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
| 44 | + $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
| 45 | 45 | } |
| 46 | 46 | // UploadPolicy 和 UploadOption检查 |
| 47 | 47 | list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | { |
| 69 | 69 | $data = file_get_contents($filePath); |
| 70 | 70 | $uploadOption->setContent($data); |
| 71 | - $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
| 71 | + $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
| 72 | 72 | return $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 73 | 73 | } |
| 74 | 74 | |
@@ -81,8 +81,8 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | protected function uploadSuperFile($filePath, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
| 83 | 83 | { |
| 84 | - $fileSize = filesize($filePath); // 文件大小 |
|
| 85 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
| 84 | + $fileSize = filesize($filePath); // 文件大小 |
|
| 85 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
| 86 | 86 | $blockNum = intval(ceil($fileSize / $blockSize)); // 文件分片后的块数 |
| 87 | 87 | for ($i = 0; $i < $blockNum; $i++) { |
| 88 | 88 | $currentSize = $blockSize; // 当前文件块的大小 |
@@ -95,8 +95,8 @@ discard block |
||
| 95 | 95 | $httpRes = null; |
| 96 | 96 | if (0 == $i) { |
| 97 | 97 | // 分片初始化阶段 |
| 98 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
| 99 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
| 98 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
| 99 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
| 100 | 100 | $httpRes = $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 101 | 101 | $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
| 102 | 102 | $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
@@ -104,16 +104,16 @@ discard block |
||
| 104 | 104 | $uploadOption->setUniqueIdId($id); |
| 105 | 105 | } else { |
| 106 | 106 | // 分片上传过程中 |
| 107 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
| 108 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
| 109 | - $uploadOption->setPartNumber($i + 1); // |
|
| 107 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
| 108 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
| 109 | + $uploadOption->setPartNumber($i + 1); // |
|
| 110 | 110 | $httpRes = $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 111 | 111 | } |
| 112 | 112 | // 如果分片上传失败,则取消cancel分片上传任务,然后返回错误信息 |
| 113 | 113 | if (!$httpRes['isSuccess']) { |
| 114 | 114 | if ($uploadOption->checkMutipartParas()) { |
| 115 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
| 116 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
| 115 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
| 116 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
| 117 | 117 | $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); // 不判断取消分片任务返回的结果 |
| 118 | 118 | } |
| 119 | 119 | return $httpRes; |
@@ -126,9 +126,9 @@ discard block |
||
| 126 | 126 | $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
| 127 | 127 | } |
| 128 | 128 | // 分片上传完成 |
| 129 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
| 130 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
| 131 | - $uploadOption->setMd5(md5_file($filePath)); //文件Md5 |
|
| 129 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
| 130 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
| 131 | + $uploadOption->setMd5(md5_file($filePath)); //文件Md5 |
|
| 132 | 132 | return $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 133 | 133 | } |
| 134 | 134 | |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | public function uploadData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
| 145 | 145 | { |
| 146 | 146 | if (empty($uploadOption)) { |
| 147 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
| 147 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
| 148 | 148 | } |
| 149 | 149 | // UploadPolicy 和 UploadOption检查 |
| 150 | 150 | list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | } |
| 159 | 159 | // 文件不大于设定的分片大小(默认2M),则直接上传uploadMiniFile() |
| 160 | 160 | $uploadOption->setContent($data); |
| 161 | - $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
| 161 | + $url = $this->upload_host . Conf::UPLOAD_API_UPLOAD; //普通上传的API |
|
| 162 | 162 | return $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 163 | 163 | } |
| 164 | 164 | |
@@ -171,8 +171,8 @@ discard block |
||
| 171 | 171 | */ |
| 172 | 172 | protected function uploadSuperData($data, UploadPolicy $uploadPolicy, UploadOption $uploadOption = null) |
| 173 | 173 | { |
| 174 | - $dataSize = strlen($data); // 文件大小 |
|
| 175 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
| 174 | + $dataSize = strlen($data); // 文件大小 |
|
| 175 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
| 176 | 176 | $blockNum = intval(ceil($dataSize / $blockSize)); // 文件分片后的块数 |
| 177 | 177 | for ($i = 0; $i < $blockNum; $i++) { |
| 178 | 178 | $currentSize = $blockSize; // 当前文件块的大小 |
@@ -185,8 +185,8 @@ discard block |
||
| 185 | 185 | $httpRes = null; |
| 186 | 186 | if (0 == $i) { |
| 187 | 187 | // 分片初始化阶段 |
| 188 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
| 189 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
| 188 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
| 189 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
| 190 | 190 | $httpRes = $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 191 | 191 | $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
| 192 | 192 | $id = isset($httpRes['id']) ? $httpRes['id'] : null; // 上传唯一ID(多媒体服务用于区分上传的id) |
@@ -194,16 +194,16 @@ discard block |
||
| 194 | 194 | $uploadOption->setUniqueIdId($id); |
| 195 | 195 | } else { |
| 196 | 196 | // 分片上传过程中 |
| 197 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
| 198 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
| 199 | - $uploadOption->setPartNumber($i + 1); // |
|
| 197 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
| 198 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
| 199 | + $uploadOption->setPartNumber($i + 1); // |
|
| 200 | 200 | $httpRes = $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 201 | 201 | } |
| 202 | 202 | // 如果分片上传失败,则取消cancel分片上传任务,然后返回错误信息 |
| 203 | 203 | if (!$httpRes['isSuccess']) { |
| 204 | 204 | if ($uploadOption->checkMutipartParas()) { |
| 205 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
| 206 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
| 205 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
| 206 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
| 207 | 207 | $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); // 不判断取消分片任务返回的结果 |
| 208 | 208 | } |
| 209 | 209 | return $httpRes; |
@@ -212,9 +212,9 @@ discard block |
||
| 212 | 212 | $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
| 213 | 213 | } |
| 214 | 214 | // 分片上传完成 |
| 215 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
| 216 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
| 217 | - $uploadOption->setMd5(md5($data)); //文件Md5 |
|
| 215 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
| 216 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
| 217 | + $uploadOption->setMd5(md5($data)); //文件Md5 |
|
| 218 | 218 | return $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 219 | 219 | } |
| 220 | 220 | |
@@ -232,10 +232,10 @@ discard block |
||
| 232 | 232 | return $this->_errorResponse("FileNotExist", "file not exist"); |
| 233 | 233 | } |
| 234 | 234 | if (empty($uploadOption)) { |
| 235 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
| 235 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
| 236 | 236 | } |
| 237 | 237 | if (empty($uploadOption->name)) { |
| 238 | - $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
| 238 | + $uploadOption->name = basename($filePath); //如果用户没有设置name属性,则使用上传时的文件名 |
|
| 239 | 239 | } |
| 240 | 240 | $blockData = file_get_contents($encodePath, 0, null, 0, $uploadOption->blockSize); |
| 241 | 241 | return $this->multipartInitByData($blockData, $uploadPolicy, $uploadOption); |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | public function multipartInitByData($blockData, UploadPolicy $uploadPolicy, UploadOption $uploadOption) |
| 252 | 252 | { |
| 253 | 253 | if (empty($uploadOption)) { |
| 254 | - $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
| 254 | + $uploadOption = new UploadOption(); //如果用户没有传递UploadOption,则生成一个默认的 |
|
| 255 | 255 | } |
| 256 | 256 | // UploadPolicy 和 UploadOption检查 |
| 257 | 257 | list($isValid, $message) = $this->checkUploadInfo($uploadPolicy, $uploadOption); |
@@ -259,13 +259,13 @@ discard block |
||
| 259 | 259 | return $this->_errorResponse("ErrorUploadInfo", "error upload policy or option:" . $message); |
| 260 | 260 | } |
| 261 | 261 | // 数据大小不等于设定的分片大小(默认2M),则无法完成初始化 |
| 262 | - $dataSize = strlen($blockData); // 数据文件大小 |
|
| 262 | + $dataSize = strlen($blockData); // 数据文件大小 |
|
| 263 | 263 | if ($dataSize != ($uploadOption->blockSize)) { |
| 264 | 264 | return $this->_errorResponse("MultipartInitError", "UploadOption's blockSize is not equal to data's size"); |
| 265 | 265 | } |
| 266 | 266 | $uploadOption->setContent($blockData); // 设置待上传的文件块 |
| 267 | - $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
| 268 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
| 267 | + $uploadOption->optionType = UpOptionType::BLOCK_INIT_UPLOAD; //初始化分片时的Option类型 |
|
| 268 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_INIT; //初始化分片上传的API |
|
| 269 | 269 | $httpRes = $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 270 | 270 | //若成功返回,则保存初始化成功的uploadId、id 以及 partNumber、eTag |
| 271 | 271 | $uploadId = isset($httpRes['uploadId']) ? $httpRes['uploadId'] : null; // 分片上传ID(OSS用于区分上传的id) |
@@ -290,8 +290,8 @@ discard block |
||
| 290 | 290 | if (!file_exists($encodePath)) { |
| 291 | 291 | return $this->_errorResponse("FileNotExist", "file not exist"); |
| 292 | 292 | } |
| 293 | - $fileSize = filesize($encodePath); // 文件大小 |
|
| 294 | - $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
| 293 | + $fileSize = filesize($encodePath); // 文件大小 |
|
| 294 | + $blockSize = $uploadOption->blockSize; // 文件分片大小 |
|
| 295 | 295 | $blockNum = intval(ceil($fileSize / $blockSize)); // 文件分片后的块数 |
| 296 | 296 | $currentSize = $blockSize; // 当前文件块的大小 |
| 297 | 297 | if ($uploadOption->getPartNumber() == $blockNum) { |
@@ -317,8 +317,8 @@ discard block |
||
| 317 | 317 | return $this->_errorResponse("MultipartUploadError", "multipart upload's parameters(id,uploadId,partNumber) error"); |
| 318 | 318 | } |
| 319 | 319 | $uploadOption->setContent($blockData); // 设置待上传的文件块 |
| 320 | - $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
| 321 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
| 320 | + $uploadOption->optionType = UpOptionType::BLOCK_RUN_UPLOAD; //分片上传过程中的Option类型 |
|
| 321 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_UPLOAD; //分片上传过程中的API |
|
| 322 | 322 | $httpRes = $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 323 | 323 | if (isset($httpRes['partNumber']) && isset($httpRes['eTag'])) { |
| 324 | 324 | $uploadOption->addPartNumberAndETag($httpRes['partNumber'], $httpRes['eTag']); |
@@ -339,8 +339,8 @@ discard block |
||
| 339 | 339 | if (!$uploadOption->checkMutipartParas() || empty($fileMd5)) { |
| 340 | 340 | return $this->_errorResponse("MultipartCompleteError", "multipart upload's parameters(id,uploadId,md5) error"); |
| 341 | 341 | } |
| 342 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
| 343 | - $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
| 342 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_COMPLETE; //完成分片上传任务的API |
|
| 343 | + $uploadOption->optionType = UpOptionType::BLOCK_COMPLETE_UPLOAD; //完成分片上传任务时的Option类型 |
|
| 344 | 344 | return $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 345 | 345 | } |
| 346 | 346 | |
@@ -355,8 +355,8 @@ discard block |
||
| 355 | 355 | if (!$uploadOption->checkMutipartParas()) { |
| 356 | 356 | return $this->_errorResponse("MultipartCancelError", "multipart upload's parameters(id,uploadId) error"); |
| 357 | 357 | } |
| 358 | - $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
| 359 | - $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
| 358 | + $url = $this->upload_host . Conf::UPLOAD_API_BLOCK_CANCEL; //取消分片任务的API |
|
| 359 | + $uploadOption->optionType = UpOptionType::BLOCK_CANCEL_UPLOAD; //取消分片任务时的Option类型 |
|
| 360 | 360 | return $this->_send_request('POST', $url, $uploadPolicy, $uploadOption); |
| 361 | 361 | } |
| 362 | 362 | |
@@ -387,17 +387,17 @@ discard block |
||
| 387 | 387 | list($contentType, $httpBody) = $this->BuildMultipartForm($uploadOption); |
| 388 | 388 | $length = @strlen($httpBody); |
| 389 | 389 | array_push($_headers, "Content-Type: {$contentType}"); |
| 390 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $httpBody); //请求体 |
|
| 390 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $httpBody); //请求体 |
|
| 391 | 391 | } |
| 392 | 392 | array_push($_headers, "Content-Length: {$length}"); |
| 393 | - curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
| 394 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
| 395 | - curl_setopt($ch, CURLOPT_TIMEOUT, $uploadOption->timeout); //持续时长 |
|
| 396 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
| 393 | + curl_setopt($ch, CURLOPT_HEADER, 1); //设置头部 |
|
| 394 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); //请求头 |
|
| 395 | + curl_setopt($ch, CURLOPT_TIMEOUT, $uploadOption->timeout); //持续时长 |
|
| 396 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接超时时长 |
|
| 397 | 397 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //成功,只返回结果,不自动输出任何内容。如果失败返回FALSE |
| 398 | 398 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
| 399 | 399 | curl_setopt($ch, CURLOPT_URL, $url); |
| 400 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
| 400 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //自定义请求 |
|
| 401 | 401 | //设置请求方式(GET或POST等) |
| 402 | 402 | if ($method == 'PUT' || $method == 'POST') { |
| 403 | 403 | curl_setopt($ch, CURLOPT_POST, 1); |
@@ -418,10 +418,10 @@ discard block |
||
| 418 | 418 | } else { |
| 419 | 419 | //解析返回结果,并判断是否上传成功 |
| 420 | 420 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 421 | - $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
| 421 | + $success = ($http_code == 200) ? true : false; //判断是否上传成功 |
|
| 422 | 422 | $resStr = explode("\r\n\r\n", $response, 2); |
| 423 | 423 | $resBody = isset($resStr[1]) ? $resStr[1] : ''; |
| 424 | - $resArray = json_decode($resBody, true); //解析得到结果 |
|
| 424 | + $resArray = json_decode($resBody, true); //解析得到结果 |
|
| 425 | 425 | $result = (empty($resArray) ? $result : $resArray); |
| 426 | 426 | } |
| 427 | 427 | } catch (Exception $e) { |