Commit ba62decf6e6c0b01bd21333189e9e6c71575bdc8

Authored by u123231312
1 parent 7da3a7eb

no message

  1 +<?php
  2 +namespace Qiniu\Storage;
  3 +
  4 +use Qiniu\Auth;
  5 +use Qiniu\Config;
  6 +use Qiniu\Zone;
  7 +use Qiniu\Http\Client;
  8 +use Qiniu\Http\Error;
  9 +
  10 +/**
  11 + * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
  12 + *
  13 + * @link https://developer.qiniu.com/kodo/api/1274/rs
  14 + */
  15 +final class BucketManager
  16 +{
  17 + private $auth;
  18 + private $config;
  19 +
  20 + public function __construct(Auth $auth, Config $config = null)
  21 + {
  22 + $this->auth = $auth;
  23 + if ($config == null) {
  24 + $this->config = new Config();
  25 + } else {
  26 + $this->config = $config;
  27 + }
  28 + }
  29 +
  30 + /**
  31 + * 获取指定账号下所有的空间名。
  32 + *
  33 + * @return string[] 包含所有空间名
  34 + */
  35 + public function buckets($shared = true)
  36 + {
  37 + $includeShared = "false";
  38 + if ($shared === true) {
  39 + $includeShared = "true";
  40 + }
  41 + return $this->rsGet('/buckets?shared=' . $includeShared);
  42 + }
  43 +
  44 + /**
  45 + * 获取指定空间绑定的所有的域名
  46 + *
  47 + * @return string[] 包含所有空间域名
  48 + */
  49 + public function domains($bucket)
  50 + {
  51 + return $this->apiGet('/v6/domain/list?tbl=' . $bucket);
  52 + }
  53 +
  54 + /**
  55 + * 获取空间绑定的域名列表
  56 + * @return string[] 包含空间绑定的所有域名
  57 + */
  58 +
  59 + /**
  60 + * 列取空间的文件列表
  61 + *
  62 + * @param $bucket 空间名
  63 + * @param $prefix 列举前缀
  64 + * @param $marker 列举标识符
  65 + * @param $limit 单次列举个数限制
  66 + * @param $delimiter 指定目录分隔符
  67 + *
  68 + * @return array 包含文件信息的数组,类似:[
  69 + * {
  70 + * "hash" => "<Hash string>",
  71 + * "key" => "<Key string>",
  72 + * "fsize" => "<file size>",
  73 + * "putTime" => "<file modify time>"
  74 + * },
  75 + * ...
  76 + * ]
  77 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
  78 + */
  79 + public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null)
  80 + {
  81 + $query = array('bucket' => $bucket);
  82 + \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
  83 + \Qiniu\setWithoutEmpty($query, 'marker', $marker);
  84 + \Qiniu\setWithoutEmpty($query, 'limit', $limit);
  85 + \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
  86 + $url = $this->getRsfHost() . '/list?' . http_build_query($query);
  87 + return $this->get($url);
  88 + }
  89 +
  90 + /**
  91 + * 获取资源的元信息,但不返回文件内容
  92 + *
  93 + * @param $bucket 待获取信息资源所在的空间
  94 + * @param $key 待获取资源的文件名
  95 + *
  96 + * @return array 包含文件信息的数组,类似:
  97 + * [
  98 + * "hash" => "<Hash string>",
  99 + * "key" => "<Key string>",
  100 + * "fsize" => <file size>,
  101 + * "putTime" => "<file modify time>"
  102 + * "fileType" => <file type>
  103 + * ]
  104 + *
  105 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html
  106 + */
  107 + public function stat($bucket, $key)
  108 + {
  109 + $path = '/stat/' . \Qiniu\entry($bucket, $key);
  110 + return $this->rsGet($path);
  111 + }
  112 +
  113 + /**
  114 + * 删除指定资源
  115 + *
  116 + * @param $bucket 待删除资源所在的空间
  117 + * @param $key 待删除资源的文件名
  118 + *
  119 + * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  120 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html
  121 + */
  122 + public function delete($bucket, $key)
  123 + {
  124 + $path = '/delete/' . \Qiniu\entry($bucket, $key);
  125 + list(, $error) = $this->rsPost($path);
  126 + return $error;
  127 + }
  128 +
  129 +
  130 + /**
  131 + * 给资源进行重命名,本质为move操作。
  132 + *
  133 + * @param $bucket 待操作资源所在空间
  134 + * @param $oldname 待操作资源文件名
  135 + * @param $newname 目标资源文件名
  136 + *
  137 + * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  138 + */
  139 + public function rename($bucket, $oldname, $newname)
  140 + {
  141 + return $this->move($bucket, $oldname, $bucket, $newname);
  142 + }
  143 +
  144 + /**
  145 + * 给资源进行重命名,本质为move操作。
  146 + *
  147 + * @param $from_bucket 待操作资源所在空间
  148 + * @param $from_key 待操作资源文件名
  149 + * @param $to_bucket 目标资源空间名
  150 + * @param $to_key 目标资源文件名
  151 + *
  152 + * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  153 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html
  154 + */
  155 + public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
  156 + {
  157 + $from = \Qiniu\entry($from_bucket, $from_key);
  158 + $to = \Qiniu\entry($to_bucket, $to_key);
  159 + $path = '/copy/' . $from . '/' . $to;
  160 + if ($force === true) {
  161 + $path .= '/force/true';
  162 + }
  163 + list(, $error) = $this->rsPost($path);
  164 + return $error;
  165 + }
  166 +
  167 + /**
  168 + * 将资源从一个空间到另一个空间
  169 + *
  170 + * @param $from_bucket 待操作资源所在空间
  171 + * @param $from_key 待操作资源文件名
  172 + * @param $to_bucket 目标资源空间名
  173 + * @param $to_key 目标资源文件名
  174 + *
  175 + * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  176 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html
  177 + */
  178 + public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
  179 + {
  180 + $from = \Qiniu\entry($from_bucket, $from_key);
  181 + $to = \Qiniu\entry($to_bucket, $to_key);
  182 + $path = '/move/' . $from . '/' . $to;
  183 + if ($force) {
  184 + $path .= '/force/true';
  185 + }
  186 + list(, $error) = $this->rsPost($path);
  187 + return $error;
  188 + }
  189 +
  190 + /**
  191 + * 主动修改指定资源的文件类型
  192 + *
  193 + * @param $bucket 待操作资源所在空间
  194 + * @param $key 待操作资源文件名
  195 + * @param $mime 待操作文件目标mimeType
  196 + *
  197 + * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  198 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html
  199 + */
  200 + public function changeMime($bucket, $key, $mime)
  201 + {
  202 + $resource = \Qiniu\entry($bucket, $key);
  203 + $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
  204 + $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
  205 + list(, $error) = $this->rsPost($path);
  206 + return $error;
  207 + }
  208 +
  209 +
  210 + /**
  211 + * 修改指定资源的存储类型
  212 + *
  213 + * @param $bucket 待操作资源所在空间
  214 + * @param $key 待操作资源文件名
  215 + * @param $fileType 待操作文件目标文件类型
  216 + *
  217 + * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  218 + * @link https://developer.qiniu.com/kodo/api/3710/modify-the-file-type
  219 + */
  220 + public function changeType($bucket, $key, $fileType)
  221 + {
  222 + $resource = \Qiniu\entry($bucket, $key);
  223 + $path = '/chtype/' . $resource . '/type/' . $fileType;
  224 + list(, $error) = $this->rsPost($path);
  225 + return $error;
  226 + }
  227 +
  228 + /**
  229 + * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
  230 + *
  231 + * @param $bucket 待操作资源所在空间
  232 + * @param $key 待操作资源文件名
  233 + * @param $status 待操作文件目标文件类型
  234 + *
  235 + * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  236 + * @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
  237 + */
  238 + public function changeStatus($bucket, $key, $status)
  239 + {
  240 + $resource = \Qiniu\entry($bucket, $key);
  241 + $path = '/chstatus/' . $resource . '/status/' . $status;
  242 + list(, $error) = $this->rsPost($path);
  243 + return $error;
  244 + }
  245 +
  246 + /**
  247 + * 从指定URL抓取资源,并将该资源存储到指定空间中
  248 + *
  249 + * @param $url 指定的URL
  250 + * @param $bucket 目标资源空间
  251 + * @param $key 目标资源文件名
  252 + *
  253 + * @return array 包含已拉取的文件信息。
  254 + * 成功时: [
  255 + * [
  256 + * "hash" => "<Hash string>",
  257 + * "key" => "<Key string>"
  258 + * ],
  259 + * null
  260 + * ]
  261 + *
  262 + * 失败时: [
  263 + * null,
  264 + * Qiniu/Http/Error
  265 + * ]
  266 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html
  267 + */
  268 + public function fetch($url, $bucket, $key = null)
  269 + {
  270 +
  271 + $resource = \Qiniu\base64_urlSafeEncode($url);
  272 + $to = \Qiniu\entry($bucket, $key);
  273 + $path = '/fetch/' . $resource . '/to/' . $to;
  274 +
  275 + $ak = $this->auth->getAccessKey();
  276 + $ioHost = $this->config->getIovipHost($ak, $bucket);
  277 +
  278 + $url = $ioHost . $path;
  279 + return $this->post($url, null);
  280 + }
  281 +
  282 + /**
  283 + * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
  284 + *
  285 + * @param $bucket 待获取资源所在的空间
  286 + * @param $key 代获取资源文件名
  287 + *
  288 + * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  289 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html
  290 + */
  291 + public function prefetch($bucket, $key)
  292 + {
  293 + $resource = \Qiniu\entry($bucket, $key);
  294 + $path = '/prefetch/' . $resource;
  295 +
  296 + $ak = $this->auth->getAccessKey();
  297 + $ioHost = $this->config->getIovipHost($ak, $bucket);
  298 +
  299 + $url = $ioHost . $path;
  300 + list(, $error) = $this->post($url, null);
  301 + return $error;
  302 + }
  303 +
  304 + /**
  305 + * 在单次请求中进行多个资源管理操作
  306 + *
  307 + * @param $operations 资源管理操作数组
  308 + *
  309 + * @return array 每个资源的处理情况,结果类似:
  310 + * [
  311 + * { "code" => <HttpCode int>, "data" => <Data> },
  312 + * { "code" => <HttpCode int> },
  313 + * { "code" => <HttpCode int> },
  314 + * { "code" => <HttpCode int> },
  315 + * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
  316 + * ...
  317 + * ]
  318 + * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
  319 + */
  320 + public function batch($operations)
  321 + {
  322 + $params = 'op=' . implode('&op=', $operations);
  323 + return $this->rsPost('/batch', $params);
  324 + }
  325 +
  326 + /**
  327 + * 设置文件的生命周期
  328 + *
  329 + * @param $bucket 设置文件生命周期文件所在的空间
  330 + * @param $key 设置文件生命周期文件的文件名
  331 + * @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
  332 + *
  333 + * @return Mixed
  334 + * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
  335 + */
  336 + public function deleteAfterDays($bucket, $key, $days)
  337 + {
  338 + $entry = \Qiniu\entry($bucket, $key);
  339 + $path = "/deleteAfterDays/$entry/$days";
  340 + list(, $error) = $this->rsPost($path);
  341 + return $error;
  342 + }
  343 +
  344 + private function getRsfHost()
  345 + {
  346 + $scheme = "http://";
  347 + if ($this->config->useHTTPS == true) {
  348 + $scheme = "https://";
  349 + }
  350 + return $scheme . Config::RSF_HOST;
  351 + }
  352 +
  353 + private function getRsHost()
  354 + {
  355 + $scheme = "http://";
  356 + if ($this->config->useHTTPS == true) {
  357 + $scheme = "https://";
  358 + }
  359 + return $scheme . Config::RS_HOST;
  360 + }
  361 +
  362 + private function getApiHost()
  363 + {
  364 + $scheme = "http://";
  365 + if ($this->config->useHTTPS == true) {
  366 + $scheme = "https://";
  367 + }
  368 + return $scheme . Config::API_HOST;
  369 + }
  370 +
  371 + private function rsPost($path, $body = null)
  372 + {
  373 + $url = $this->getRsHost() . $path;
  374 + return $this->post($url, $body);
  375 + }
  376 +
  377 + private function apiGet($path)
  378 + {
  379 + $url = $this->getApiHost() . $path;
  380 + return $this->get($url);
  381 + }
  382 +
  383 + private function rsGet($path)
  384 + {
  385 + $url = $this->getRsHost() . $path;
  386 + return $this->get($url);
  387 + }
  388 +
  389 + private function get($url)
  390 + {
  391 + $headers = $this->auth->authorization($url);
  392 + $ret = Client::get($url, $headers);
  393 + if (!$ret->ok()) {
  394 + return array(null, new Error($url, $ret));
  395 + }
  396 + return array($ret->json(), null);
  397 + }
  398 +
  399 + private function post($url, $body)
  400 + {
  401 + $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
  402 + $ret = Client::post($url, $body, $headers);
  403 + if (!$ret->ok()) {
  404 + return array(null, new Error($url, $ret));
  405 + }
  406 + $r = ($ret->body === null) ? array() : $ret->json();
  407 + return array($r, null);
  408 + }
  409 +
  410 + public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
  411 + {
  412 + return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
  413 + }
  414 +
  415 +
  416 + public static function buildBatchRename($bucket, $key_pairs, $force)
  417 + {
  418 + return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
  419 + }
  420 +
  421 +
  422 + public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
  423 + {
  424 + return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
  425 + }
  426 +
  427 +
  428 + public static function buildBatchDelete($bucket, $keys)
  429 + {
  430 + return self::oneKeyBatch('/delete', $bucket, $keys);
  431 + }
  432 +
  433 +
  434 + public static function buildBatchStat($bucket, $keys)
  435 + {
  436 + return self::oneKeyBatch('/stat', $bucket, $keys);
  437 + }
  438 +
  439 + public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
  440 + {
  441 + $data = array();
  442 + foreach ($key_day_pairs as $key => $day) {
  443 + array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
  444 + }
  445 + return $data;
  446 + }
  447 +
  448 + public static function buildBatchChangeMime($bucket, $key_mime_pairs)
  449 + {
  450 + $data = array();
  451 + foreach ($key_mime_pairs as $key => $mime) {
  452 + array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
  453 + }
  454 + return $data;
  455 + }
  456 +
  457 + public static function buildBatchChangeType($bucket, $key_type_pairs)
  458 + {
  459 + $data = array();
  460 + foreach ($key_type_pairs as $key => $type) {
  461 + array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
  462 + }
  463 + return $data;
  464 + }
  465 +
  466 + private static function oneKeyBatch($operation, $bucket, $keys)
  467 + {
  468 + $data = array();
  469 + foreach ($keys as $key) {
  470 + array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
  471 + }
  472 + return $data;
  473 + }
  474 +
  475 + private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
  476 + {
  477 + if ($target_bucket === null) {
  478 + $target_bucket = $source_bucket;
  479 + }
  480 + $data = array();
  481 + $forceOp = "false";
  482 + if ($force) {
  483 + $forceOp = "true";
  484 + }
  485 + foreach ($key_pairs as $from_key => $to_key) {
  486 + $from = \Qiniu\entry($source_bucket, $from_key);
  487 + $to = \Qiniu\entry($target_bucket, $to_key);
  488 + array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
  489 + }
  490 + return $data;
  491 + }
  492 +}
  1 +<?php
  2 +namespace Qiniu\Storage;
  3 +
  4 +use Qiniu\Http\Client;
  5 +use Qiniu\Http\Error;
  6 +
  7 +final class FormUploader
  8 +{
  9 +
  10 + /**
  11 + * 上传二进制流到七牛, 内部使用
  12 + *
  13 + * @param $upToken 上传凭证
  14 + * @param $key 上传文件名
  15 + * @param $data 上传二进制流
  16 + * @param $config 上传配置
  17 + * @param $params 自定义变量,规格参考
  18 + * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  19 + * @param $mime 上传数据的mimeType
  20 + *
  21 + * @return array 包含已上传文件的信息,类似:
  22 + * [
  23 + * "hash" => "<Hash string>",
  24 + * "key" => "<Key string>"
  25 + * ]
  26 + */
  27 + public static function put(
  28 + $upToken,
  29 + $key,
  30 + $data,
  31 + $config,
  32 + $params,
  33 + $mime,
  34 + $fname
  35 + ) {
  36 +
  37 + $fields = array('token' => $upToken);
  38 + if ($key === null) {
  39 + $fname='nullkey';
  40 + } else {
  41 + $fields['key'] = $key;
  42 + }
  43 +
  44 + //enable crc32 check by default
  45 + $fields['crc32'] = \Qiniu\crc32_data($data);
  46 +
  47 + if ($params) {
  48 + foreach ($params as $k => $v) {
  49 + $fields[$k] = $v;
  50 + }
  51 + }
  52 +
  53 + list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
  54 + if ($err != null) {
  55 + return array(null, $err);
  56 + }
  57 +
  58 + $upHost = $config->getUpHost($accessKey, $bucket);
  59 +
  60 + $response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
  61 + if (!$response->ok()) {
  62 + return array(null, new Error($upHost, $response));
  63 + }
  64 + return array($response->json(), null);
  65 + }
  66 +
  67 + /**
  68 + * 上传文件到七牛,内部使用
  69 + *
  70 + * @param $upToken 上传凭证
  71 + * @param $key 上传文件名
  72 + * @param $filePath 上传文件的路径
  73 + * @param $config 上传配置
  74 + * @param $params 自定义变量,规格参考
  75 + * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  76 + * @param $mime 上传数据的mimeType
  77 + *
  78 + * @return array 包含已上传文件的信息,类似:
  79 + * [
  80 + * "hash" => "<Hash string>",
  81 + * "key" => "<Key string>"
  82 + * ]
  83 + */
  84 + public static function putFile(
  85 + $upToken,
  86 + $key,
  87 + $filePath,
  88 + $config,
  89 + $params,
  90 + $mime
  91 + ) {
  92 +
  93 +
  94 + $fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime));
  95 + if ($key !== null) {
  96 + $fields['key'] = $key;
  97 + }
  98 +
  99 + $fields['crc32'] = \Qiniu\crc32_file($filePath);
  100 +
  101 + if ($params) {
  102 + foreach ($params as $k => $v) {
  103 + $fields[$k] = $v;
  104 + }
  105 + }
  106 + $fields['key'] = $key;
  107 + $headers = array('Content-Type' => 'multipart/form-data');
  108 +
  109 + list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
  110 + if ($err != null) {
  111 + return array(null, $err);
  112 + }
  113 +
  114 + $upHost = $config->getUpHost($accessKey, $bucket);
  115 +
  116 + $response = Client::post($upHost, $fields, $headers);
  117 + if (!$response->ok()) {
  118 + return array(null, new Error($upHost, $response));
  119 + }
  120 + return array($response->json(), null);
  121 + }
  122 +
  123 + private static function createFile($filename, $mime)
  124 + {
  125 + // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
  126 + // See: https://wiki.php.net/rfc/curl-file-upload
  127 + if (function_exists('curl_file_create')) {
  128 + return curl_file_create($filename, $mime);
  129 + }
  130 +
  131 + // Use the old style if using an older version of PHP
  132 + $value = "@{$filename}";
  133 + if (!empty($mime)) {
  134 + $value .= ';type=' . $mime;
  135 + }
  136 +
  137 + return $value;
  138 + }
  139 +}
  1 +<?php
  2 +namespace Qiniu\Storage;
  3 +
  4 +use Qiniu\Config;
  5 +use Qiniu\Http\Client;
  6 +use Qiniu\Http\Error;
  7 +
  8 +/**
  9 + * 断点续上传类, 该类主要实现了断点续上传中的分块上传,
  10 + * 以及相应地创建块和创建文件过程.
  11 + *
  12 + * @link http://developer.qiniu.com/docs/v6/api/reference/up/mkblk.html
  13 + * @link http://developer.qiniu.com/docs/v6/api/reference/up/mkfile.html
  14 + */
  15 +final class ResumeUploader
  16 +{
  17 + private $upToken;
  18 + private $key;
  19 + private $inputStream;
  20 + private $size;
  21 + private $params;
  22 + private $mime;
  23 + private $contexts;
  24 + private $host;
  25 + private $currentUrl;
  26 + private $config;
  27 +
  28 + /**
  29 + * 上传二进制流到七牛
  30 + *
  31 + * @param $upToken 上传凭证
  32 + * @param $key 上传文件名
  33 + * @param $inputStream 上传二进制流
  34 + * @param $size 上传流的大小
  35 + * @param $params 自定义变量
  36 + * @param $mime 上传数据的mimeType
  37 + *
  38 + * @link http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  39 + */
  40 + public function __construct(
  41 + $upToken,
  42 + $key,
  43 + $inputStream,
  44 + $size,
  45 + $params,
  46 + $mime,
  47 + $config
  48 + ) {
  49 +
  50 + $this->upToken = $upToken;
  51 + $this->key = $key;
  52 + $this->inputStream = $inputStream;
  53 + $this->size = $size;
  54 + $this->params = $params;
  55 + $this->mime = $mime;
  56 + $this->contexts = array();
  57 + $this->config = $config;
  58 +
  59 + list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
  60 + if ($err != null) {
  61 + return array(null, $err);
  62 + }
  63 +
  64 + $upHost = $config->getUpHost($accessKey, $bucket);
  65 + if ($err != null) {
  66 + throw new \Exception($err->message(), 1);
  67 + }
  68 + $this->host = $upHost;
  69 + }
  70 +
  71 + /**
  72 + * 上传操作
  73 + */
  74 + public function upload($fname)
  75 + {
  76 + $uploaded = 0;
  77 + while ($uploaded < $this->size) {
  78 + $blockSize = $this->blockSize($uploaded);
  79 + $data = fread($this->inputStream, $blockSize);
  80 + if ($data === false) {
  81 + throw new \Exception("file read failed", 1);
  82 + }
  83 + $crc = \Qiniu\crc32_data($data);
  84 + $response = $this->makeBlock($data, $blockSize);
  85 + $ret = null;
  86 + if ($response->ok() && $response->json() != null) {
  87 + $ret = $response->json();
  88 + }
  89 + if ($response->statusCode < 0) {
  90 + list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($this->upToken);
  91 + if ($err != null) {
  92 + return array(null, $err);
  93 + }
  94 +
  95 + $upHostBackup = $this->config->getUpBackupHost($accessKey, $bucket);
  96 + $this->host = $upHostBackup;
  97 + }
  98 + if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
  99 + $response = $this->makeBlock($data, $blockSize);
  100 + $ret = $response->json();
  101 + }
  102 +
  103 + if (!$response->ok() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
  104 + return array(null, new Error($this->currentUrl, $response));
  105 + }
  106 + array_push($this->contexts, $ret['ctx']);
  107 + $uploaded += $blockSize;
  108 + }
  109 + return $this->makeFile($fname);
  110 + }
  111 +
  112 + /**
  113 + * 创建块
  114 + */
  115 + private function makeBlock($block, $blockSize)
  116 + {
  117 + $url = $this->host . '/mkblk/' . $blockSize;
  118 + return $this->post($url, $block);
  119 + }
  120 +
  121 + private function fileUrl($fname)
  122 + {
  123 + $url = $this->host . '/mkfile/' . $this->size;
  124 + $url .= '/mimeType/' . \Qiniu\base64_urlSafeEncode($this->mime);
  125 + if ($this->key != null) {
  126 + $url .= '/key/' . \Qiniu\base64_urlSafeEncode($this->key);
  127 + }
  128 + $url .= '/fname/' . \Qiniu\base64_urlSafeEncode($fname);
  129 + if (!empty($this->params)) {
  130 + foreach ($this->params as $key => $value) {
  131 + $val = \Qiniu\base64_urlSafeEncode($value);
  132 + $url .= "/$key/$val";
  133 + }
  134 + }
  135 + return $url;
  136 + }
  137 +
  138 + /**
  139 + * 创建文件
  140 + */
  141 + private function makeFile($fname)
  142 + {
  143 + $url = $this->fileUrl($fname);
  144 + $body = implode(',', $this->contexts);
  145 + $response = $this->post($url, $body);
  146 + if ($response->needRetry()) {
  147 + $response = $this->post($url, $body);
  148 + }
  149 + if (!$response->ok()) {
  150 + return array(null, new Error($this->currentUrl, $response));
  151 + }
  152 + return array($response->json(), null);
  153 + }
  154 +
  155 + private function post($url, $data)
  156 + {
  157 + $this->currentUrl = $url;
  158 + $headers = array('Authorization' => 'UpToken ' . $this->upToken);
  159 + return Client::post($url, $data, $headers);
  160 + }
  161 +
  162 + private function blockSize($uploaded)
  163 + {
  164 + if ($this->size < $uploaded + Config::BLOCK_SIZE) {
  165 + return $this->size - $uploaded;
  166 + }
  167 + return Config::BLOCK_SIZE;
  168 + }
  169 +}
  1 +<?php
  2 +namespace Qiniu\Storage;
  3 +
  4 +use Qiniu\Config;
  5 +use Qiniu\Http\HttpClient;
  6 +use Qiniu\Storage\ResumeUploader;
  7 +use Qiniu\Storage\FormUploader;
  8 +
  9 +/**
  10 + * 主要涉及了资源上传接口的实现
  11 + *
  12 + * @link http://developer.qiniu.com/docs/v6/api/reference/up/
  13 + */
  14 +final class UploadManager
  15 +{
  16 + private $config;
  17 +
  18 + public function __construct(Config $config = null)
  19 + {
  20 + if ($config === null) {
  21 + $config = new Config();
  22 + }
  23 + $this->config = $config;
  24 + }
  25 +
  26 + /**
  27 + * 上传二进制流到七牛
  28 + *
  29 + * @param $upToken 上传凭证
  30 + * @param $key 上传文件名
  31 + * @param $data 上传二进制流
  32 + * @param $params 自定义变量,规格参考
  33 + * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  34 + * @param $mime 上传数据的mimeType
  35 + * @param $checkCrc 是否校验crc32
  36 + *
  37 + * @return array 包含已上传文件的信息,类似:
  38 + * [
  39 + * "hash" => "<Hash string>",
  40 + * "key" => "<Key string>"
  41 + * ]
  42 + */
  43 + public function put(
  44 + $upToken,
  45 + $key,
  46 + $data,
  47 + $params = null,
  48 + $mime = 'application/octet-stream',
  49 + $fname = null
  50 + ) {
  51 +
  52 + $params = self::trimParams($params);
  53 + return FormUploader::put(
  54 + $upToken,
  55 + $key,
  56 + $data,
  57 + $this->config,
  58 + $params,
  59 + $mime,
  60 + $fname
  61 + );
  62 + }
  63 +
  64 +
  65 + /**
  66 + * 上传文件到七牛
  67 + *
  68 + * @param $upToken 上传凭证
  69 + * @param $key 上传文件名
  70 + * @param $filePath 上传文件的路径
  71 + * @param $params 自定义变量,规格参考
  72 + * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  73 + * @param $mime 上传数据的mimeType
  74 + * @param $checkCrc 是否校验crc32
  75 + *
  76 + * @return array 包含已上传文件的信息,类似:
  77 + * [
  78 + * "hash" => "<Hash string>",
  79 + * "key" => "<Key string>"
  80 + * ]
  81 + */
  82 + public function putFile(
  83 + $upToken,
  84 + $key,
  85 + $filePath,
  86 + $params = null,
  87 + $mime = 'application/octet-stream',
  88 + $checkCrc = false
  89 + ) {
  90 + $file = fopen($filePath, 'rb');
  91 + if ($file === false) {
  92 + throw new \Exception("file can not open", 1);
  93 + }
  94 + $params = self::trimParams($params);
  95 + $stat = fstat($file);
  96 + $size = $stat['size'];
  97 + if ($size <= Config::BLOCK_SIZE) {
  98 + $data = fread($file, $size);
  99 + fclose($file);
  100 + if ($data === false) {
  101 + throw new \Exception("file can not read", 1);
  102 + }
  103 + return FormUploader::put(
  104 + $upToken,
  105 + $key,
  106 + $data,
  107 + $this->config,
  108 + $params,
  109 + $mime,
  110 + basename($filePath)
  111 + );
  112 + }
  113 +
  114 + $up = new ResumeUploader(
  115 + $upToken,
  116 + $key,
  117 + $file,
  118 + $size,
  119 + $params,
  120 + $mime,
  121 + $this->config
  122 + );
  123 + $ret = $up->upload(basename($filePath));
  124 + fclose($file);
  125 + return $ret;
  126 + }
  127 +
  128 + public static function trimParams($params)
  129 + {
  130 + if ($params === null) {
  131 + return null;
  132 + }
  133 + $ret = array();
  134 + foreach ($params as $k => $v) {
  135 + $pos = strpos($k, 'x:');
  136 + if ($pos === 0 && !empty($v)) {
  137 + $ret[$k] = $v;
  138 + }
  139 + }
  140 + return $ret;
  141 + }
  142 +}
  1 +<?php
  2 +// Hack to override the time returned from the S3SignatureV4
  3 +// @codingStandardsIgnoreStart
  4 +namespace Qiniu {
  5 + function time()
  6 + {
  7 + return isset($_SERVER['override_qiniu_auth_time'])
  8 + ? 1234567890
  9 + : \time();
  10 + }
  11 +}
  12 +
  13 +namespace Qiniu\Tests {
  14 + use Qiniu\Auth;
  15 +
  16 + // @codingStandardsIgnoreEnd
  17 +
  18 + class AuthTest extends \PHPUnit_Framework_TestCase
  19 + {
  20 +
  21 + public function testSign()
  22 + {
  23 + global $dummyAuth;
  24 + $token = $dummyAuth->sign('test');
  25 + $this->assertEquals('abcdefghklmnopq:mSNBTR7uS2crJsyFr2Amwv1LaYg=', $token);
  26 + }
  27 +
  28 + public function testSignWithData()
  29 + {
  30 + global $dummyAuth;
  31 + $token = $dummyAuth->signWithData('test');
  32 + $this->assertEquals('abcdefghklmnopq:-jP8eEV9v48MkYiBGs81aDxl60E=:dGVzdA==', $token);
  33 + }
  34 +
  35 + public function testSignRequest()
  36 + {
  37 + global $dummyAuth;
  38 + $token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', '');
  39 + $this->assertEquals('abcdefghklmnopq:cFyRVoWrE3IugPIMP5YJFTO-O-Y=', $token);
  40 + $ctype = 'application/x-www-form-urlencoded';
  41 + $token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', $ctype);
  42 + $this->assertEquals($token, 'abcdefghklmnopq:svWRNcacOE-YMsc70nuIYdaa1e4=');
  43 + }
  44 +
  45 + public function testPrivateDownloadUrl()
  46 + {
  47 + global $dummyAuth;
  48 + $_SERVER['override_qiniu_auth_time'] = true;
  49 + $url = $dummyAuth->privateDownloadUrl('http://www.qiniu.com?go=1');
  50 + $expect = 'http://www.qiniu.com?go=1&e=1234571490&token=abcdefghklmnopq:8vzBeLZ9W3E4kbBLFLW0Xe0u7v4=';
  51 + $this->assertEquals($expect, $url);
  52 + unset($_SERVER['override_qiniu_auth_time']);
  53 + }
  54 +
  55 + public function testUploadToken()
  56 + {
  57 + global $dummyAuth;
  58 + $_SERVER['override_qiniu_auth_time'] = true;
  59 + $token = $dummyAuth->uploadToken('1', '2', 3600, array('endUser' => 'y'));
  60 + // @codingStandardsIgnoreStart
  61 + $exp = 'abcdefghklmnopq:yyeexeUkPOROoTGvwBjJ0F0VLEo=:eyJlbmRVc2VyIjoieSIsInNjb3BlIjoiMToyIiwiZGVhZGxpbmUiOjEyMzQ1NzE0OTB9';
  62 + // @codingStandardsIgnoreEnd
  63 + $this->assertEquals($exp, $token);
  64 + unset($_SERVER['override_qiniu_auth_time']);
  65 + }
  66 +
  67 + public function testVerifyCallback()
  68 + {
  69 + }
  70 + }
  71 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu;
  5 +
  6 +class Base64Test extends \PHPUnit_Framework_TestCase
  7 +{
  8 + public function testUrlSafe()
  9 + {
  10 + $a = '你好';
  11 + $b = \Qiniu\base64_urlSafeEncode($a);
  12 + $this->assertEquals($a, \Qiniu\base64_urlSafeDecode($b));
  13 + }
  14 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu\Storage\BucketManager;
  5 +
  6 +class BucketTest extends \PHPUnit_Framework_TestCase
  7 +{
  8 + protected $bucketManager;
  9 + protected $dummyBucketManager;
  10 + protected $bucketName;
  11 + protected $key;
  12 + protected $key2;
  13 +
  14 + protected function setUp()
  15 + {
  16 + global $bucketName;
  17 + global $key;
  18 + global $key2;
  19 + $this->bucketName = $bucketName;
  20 + $this->key = $key;
  21 + $this->key2 = $key2;
  22 +
  23 + global $testAuth;
  24 + $this->bucketManager = new BucketManager($testAuth);
  25 +
  26 + global $dummyAuth;
  27 + $this->dummyBucketManager = new BucketManager($dummyAuth);
  28 + }
  29 +
  30 + public function testBuckets()
  31 + {
  32 +
  33 + list($list, $error) = $this->bucketManager->buckets();
  34 + $this->assertTrue(in_array($this->bucketName, $list));
  35 + $this->assertNull($error);
  36 +
  37 + list($list2, $error) = $this->dummyBucketManager->buckets();
  38 + $this->assertEquals(401, $error->code());
  39 + $this->assertNull($list2);
  40 + $this->assertNotNull($error->message());
  41 + }
  42 +
  43 + public function testList()
  44 + {
  45 + list($ret, $error) = $this->bucketManager->listFiles($this->bucketName, null, null, 10);
  46 + $this->assertNotNull($ret['items'][0]);
  47 + $this->assertNotNull($ret['marker']);
  48 + }
  49 +
  50 + public function testStat()
  51 + {
  52 + list($stat, $error) = $this->bucketManager->stat($this->bucketName, $this->key);
  53 + $this->assertNotNull($stat);
  54 + $this->assertNull($error);
  55 + $this->assertNotNull($stat['hash']);
  56 +
  57 + list($stat, $error) = $this->bucketManager->stat($this->bucketName, 'nofile');
  58 + $this->assertNull($stat);
  59 + $this->assertEquals(612, $error->code());
  60 + $this->assertNotNull($error->message());
  61 +
  62 + list($stat, $error) = $this->bucketManager->stat('nobucket', 'nofile');
  63 + $this->assertNull($stat);
  64 + $this->assertEquals(631, $error->code());
  65 + $this->assertNotNull($error->message());
  66 + }
  67 +
  68 + public function testDelete()
  69 + {
  70 + $error = $this->bucketManager->delete($this->bucketName, 'del');
  71 + $this->assertEquals(612, $error->code());
  72 + }
  73 +
  74 +
  75 + public function testRename()
  76 + {
  77 + $key = 'renamefrom' . rand();
  78 + $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  79 + $key2 = 'renameto' . $key;
  80 + $error = $this->bucketManager->rename($this->bucketName, $key, $key2);
  81 + $this->assertNull($error);
  82 + $error = $this->bucketManager->delete($this->bucketName, $key2);
  83 + $this->assertNull($error);
  84 + }
  85 +
  86 +
  87 + public function testCopy()
  88 + {
  89 + $key = 'copyto' . rand();
  90 + $this->bucketManager->delete($this->bucketName, $key);
  91 +
  92 + $error = $this->bucketManager->copy(
  93 + $this->bucketName,
  94 + $this->key,
  95 + $this->bucketName,
  96 + $key
  97 + );
  98 + $this->assertNull($error);
  99 +
  100 + //test force copy
  101 + $error = $this->bucketManager->copy(
  102 + $this->bucketName,
  103 + $this->key2,
  104 + $this->bucketName,
  105 + $key,
  106 + true
  107 + );
  108 + $this->assertNull($error);
  109 +
  110 + list($key2Stat,) = $this->bucketManager->stat($this->bucketName, $this->key2);
  111 + list($key2CopiedStat,) = $this->bucketManager->stat($this->bucketName, $key);
  112 +
  113 + $this->assertEquals($key2Stat['hash'], $key2CopiedStat['hash']);
  114 +
  115 + $error = $this->bucketManager->delete($this->bucketName, $key);
  116 + $this->assertNull($error);
  117 + }
  118 +
  119 +
  120 + public function testChangeMime()
  121 + {
  122 + $error = $this->bucketManager->changeMime(
  123 + $this->bucketName,
  124 + 'php-sdk.html',
  125 + 'text/html'
  126 + );
  127 + $this->assertNull($error);
  128 + }
  129 +
  130 + public function testPrefetch()
  131 + {
  132 + $error = $this->bucketManager->prefetch(
  133 + $this->bucketName,
  134 + 'php-sdk.html'
  135 + );
  136 + $this->assertNull($error);
  137 + }
  138 +
  139 + public function testFetch()
  140 + {
  141 + list($ret, $error) = $this->bucketManager->fetch(
  142 + 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  143 + $this->bucketName,
  144 + 'fetch.html'
  145 + );
  146 + $this->assertArrayHasKey('hash', $ret);
  147 + $this->assertNull($error);
  148 +
  149 + list($ret, $error) = $this->bucketManager->fetch(
  150 + 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  151 + $this->bucketName,
  152 + ''
  153 + );
  154 + $this->assertArrayHasKey('key', $ret);
  155 + $this->assertNull($error);
  156 +
  157 + list($ret, $error) = $this->bucketManager->fetch(
  158 + 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  159 + $this->bucketName
  160 + );
  161 + $this->assertArrayHasKey('key', $ret);
  162 + $this->assertNull($error);
  163 + }
  164 +
  165 + public function testBatchCopy()
  166 + {
  167 + $key = 'copyto' . rand();
  168 + $ops = BucketManager::buildBatchCopy(
  169 + $this->bucketName,
  170 + array($this->key => $key),
  171 + $this->bucketName,
  172 + true
  173 + );
  174 + list($ret, $error) = $this->bucketManager->batch($ops);
  175 + $this->assertEquals(200, $ret[0]['code']);
  176 + $ops = BucketManager::buildBatchDelete($this->bucketName, array($key));
  177 + list($ret, $error) = $this->bucketManager->batch($ops);
  178 + $this->assertEquals(200, $ret[0]['code']);
  179 + }
  180 +
  181 + public function testBatchMove()
  182 + {
  183 + $key = 'movefrom' . rand();
  184 + $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  185 + $key2 = $key . 'to';
  186 + $ops = BucketManager::buildBatchMove(
  187 + $this->bucketName,
  188 + array($key => $key2),
  189 + $this->bucketName,
  190 + true
  191 + );
  192 + list($ret, $error) = $this->bucketManager->batch($ops);
  193 + $this->assertEquals(200, $ret[0]['code']);
  194 + $error = $this->bucketManager->delete($this->bucketName, $key2);
  195 + $this->assertNull($error);
  196 + }
  197 +
  198 + public function testBatchRename()
  199 + {
  200 + $key = 'rename' . rand();
  201 + $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  202 + $key2 = $key . 'to';
  203 + $ops = BucketManager::buildBatchRename($this->bucketName, array($key => $key2), true);
  204 + list($ret, $error) = $this->bucketManager->batch($ops);
  205 + $this->assertEquals(200, $ret[0]['code']);
  206 + $error = $this->bucketManager->delete($this->bucketName, $key2);
  207 + $this->assertNull($error);
  208 + }
  209 +
  210 + public function testBatchStat()
  211 + {
  212 + $ops = BucketManager::buildBatchStat($this->bucketName, array('php-sdk.html'));
  213 + list($ret, $error) = $this->bucketManager->batch($ops);
  214 + $this->assertEquals(200, $ret[0]['code']);
  215 + }
  216 +
  217 + public function testDeleteAfterDays()
  218 + {
  219 + $key = rand();
  220 + $err = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  221 + $this->assertEquals(612, $err->code());
  222 +
  223 + $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  224 + $err = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  225 + $this->assertEquals(null, $err);
  226 + }
  227 +}
  1 +<?php
  2 +/**
  3 + * Created by IntelliJ IDEA.
  4 + * User: wf
  5 + * Date: 2017/6/21
  6 + * Time: AM8:46
  7 + */
  8 +
  9 +namespace Qiniu\Tests;
  10 +
  11 +use Qiniu\Cdn\CdnManager;
  12 +use Qiniu\Http\Client;
  13 +
  14 +class CdnManagerTest extends \PHPUnit_Framework_TestCase
  15 +{
  16 + protected $cdnManager;
  17 + protected $encryptKey;
  18 + protected $imgUrl;
  19 +
  20 + protected function setUp()
  21 + {
  22 + global $timestampAntiLeechEncryptKey;
  23 + global $customDomain;
  24 + global $testAuth;
  25 +
  26 + $this->cdnManager = new CdnManager($testAuth);
  27 + $this->encryptKey = $timestampAntiLeechEncryptKey;
  28 + $this->imgUrl = $customDomain . '/24.jpg';
  29 + }
  30 +
  31 + public function testCreateTimestampAntiLeechUrl()
  32 + {
  33 +
  34 + $signUrl = $this->cdnManager->createTimestampAntiLeechUrl($this->imgUrl, $this->encryptKey, 3600);
  35 +
  36 + $response = Client::get($signUrl);
  37 + $this->assertEquals($response->statusCode, 200);
  38 + $this->assertNull($response->error);
  39 +
  40 + $url2 = $this->imgUrl . '?imageInfo';
  41 + $signUrl2 = $this->cdnManager->createTimestampAntiLeechUrl($url2, $this->encryptKey, 3600);
  42 +
  43 + $response = Client::get($signUrl2);
  44 + $imgInfo = $response->json();
  45 +
  46 + $this->assertEquals($response->statusCode, 200);
  47 + $this->assertEquals($imgInfo['size'], 2196145);
  48 + $this->assertNull($response->error);
  49 + }
  50 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu;
  5 +
  6 +class Crc32Test extends \PHPUnit_Framework_TestCase
  7 +{
  8 + public function testData()
  9 + {
  10 + $a = '你好';
  11 + $b = \Qiniu\crc32_data($a);
  12 + $this->assertEquals('1352841281', $b);
  13 + }
  14 +
  15 + public function testFile()
  16 + {
  17 + $b = \Qiniu\crc32_file(__file__);
  18 + $c = \Qiniu\crc32_file(__file__);
  19 + $this->assertEquals($c, $b);
  20 + }
  21 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu\Http\Client;
  5 +
  6 +class DownloadTest extends \PHPUnit_Framework_TestCase
  7 +{
  8 + public function test()
  9 + {
  10 + global $testAuth;
  11 + $base_url = 'http://private-res.qiniudn.com/gogopher.jpg';
  12 + $private_url = $testAuth->privateDownloadUrl($base_url);
  13 + $response = Client::get($private_url);
  14 + $this->assertEquals(200, $response->statusCode);
  15 + }
  16 +
  17 + public function testFop()
  18 + {
  19 + global $testAuth;
  20 + $base_url = 'http://private-res.qiniudn.com/gogopher.jpg?exif';
  21 + $private_url = $testAuth->privateDownloadUrl($base_url);
  22 + $response = Client::get($private_url);
  23 + $this->assertEquals(200, $response->statusCode);
  24 + }
  25 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu\Etag;
  5 +
  6 +class EtagTest extends \PHPUnit_Framework_TestCase
  7 +{
  8 + public function test0M()
  9 + {
  10 + $file = qiniuTempFile(0);
  11 + list($r, $error) = Etag::sum($file);
  12 + unlink($file);
  13 + $this->assertEquals('Fto5o-5ea0sNMlW_75VgGJCv2AcJ', $r);
  14 + $this->assertNull($error);
  15 + }
  16 +
  17 + public function testLess4M()
  18 + {
  19 + $file = qiniuTempFile(3 * 1024 * 1024);
  20 + list($r, $error) = Etag::sum($file);
  21 + unlink($file);
  22 + $this->assertEquals('Fs5BpnAjRykYTg6o5E09cjuXrDkG', $r);
  23 + $this->assertNull($error);
  24 + }
  25 +
  26 + public function test4M()
  27 + {
  28 + $file = qiniuTempFile(4 * 1024 * 1024);
  29 + list($r, $error) = Etag::sum($file);
  30 + unlink($file);
  31 + $this->assertEquals('FiuKULnybewpEnrfTmxjsxc-3dWp', $r);
  32 + $this->assertNull($error);
  33 + }
  34 +
  35 + public function testMore4M()
  36 + {
  37 + $file = qiniuTempFile(5 * 1024 * 1024);
  38 + list($r, $error) = Etag::sum($file);
  39 + unlink($file);
  40 + $this->assertEquals('lhvyfIWMYFTq4s4alzlhXoAkqfVL', $r);
  41 + $this->assertNull($error);
  42 + }
  43 +
  44 + public function test8M()
  45 + {
  46 + $file = qiniuTempFile(8 * 1024 * 1024);
  47 + list($r, $error) = Etag::sum($file);
  48 + unlink($file);
  49 + $this->assertEquals('lmRm9ZfGZ86bnMys4wRTWtJj9ClG', $r);
  50 + $this->assertNull($error);
  51 + }
  52 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu\Processing\Operation;
  5 +use Qiniu\Processing\PersistentFop;
  6 +
  7 +class FopTest extends \PHPUnit_Framework_TestCase
  8 +{
  9 + public function testExifPub()
  10 + {
  11 + $fop = new Operation('testres.qiniudn.com');
  12 + list($exif, $error) = $fop->execute('gogopher.jpg', 'exif');
  13 + $this->assertNull($error);
  14 + $this->assertNotNull($exif);
  15 + }
  16 +
  17 + public function testExifPrivate()
  18 + {
  19 + global $testAuth;
  20 + $fop = new Operation('private-res.qiniudn.com', $testAuth);
  21 + list($exif, $error) = $fop->execute('noexif.jpg', 'exif');
  22 + $this->assertNotNull($error);
  23 + $this->assertNull($exif);
  24 + }
  25 +
  26 + public function testbuildUrl()
  27 + {
  28 + $fops = 'imageView2/2/h/200';
  29 + $fop = new Operation('testres.qiniudn.com');
  30 + $url = $fop->buildUrl('gogopher.jpg', $fops);
  31 + $this->assertEquals($url, 'http://testres.qiniudn.com/gogopher.jpg?imageView2/2/h/200');
  32 +
  33 + $fops = array('imageView2/2/h/200', 'imageInfo');
  34 + $url = $fop->buildUrl('gogopher.jpg', $fops);
  35 + $this->assertEquals($url, 'http://testres.qiniudn.com/gogopher.jpg?imageView2/2/h/200|imageInfo');
  36 + }
  37 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu\Storage\FormUploader;
  5 +use Qiniu\Storage\UploadManager;
  6 +use Qiniu\Config;
  7 +
  8 +class FormUpTest extends \PHPUnit_Framework_TestCase
  9 +{
  10 + protected $bucketName;
  11 + protected $auth;
  12 + protected $cfg;
  13 +
  14 + protected function setUp()
  15 + {
  16 + global $bucketName;
  17 + $this->bucketName = $bucketName;
  18 +
  19 + global $testAuth;
  20 + $this->auth = $testAuth;
  21 + $this->cfg = new Config();
  22 + }
  23 +
  24 + public function testData()
  25 + {
  26 + $token = $this->auth->uploadToken($this->bucketName);
  27 + list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->cfg, null, 'text/plain', null);
  28 + $this->assertNull($error);
  29 + $this->assertNotNull($ret['hash']);
  30 + }
  31 +
  32 + public function testData2()
  33 + {
  34 + $upManager = new UploadManager();
  35 + $token = $this->auth->uploadToken($this->bucketName);
  36 + list($ret, $error) = $upManager->put($token, 'formput', 'hello world', null, 'text/plain', null);
  37 + $this->assertNull($error);
  38 + $this->assertNotNull($ret['hash']);
  39 + }
  40 +
  41 + public function testFile()
  42 + {
  43 + $key = 'formPutFile';
  44 + $token = $this->auth->uploadToken($this->bucketName, $key);
  45 + list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->cfg, null, 'text/plain', null);
  46 + $this->assertNull($error);
  47 + $this->assertNotNull($ret['hash']);
  48 + }
  49 +
  50 + public function testFile2()
  51 + {
  52 + $key = 'formPutFile';
  53 + $token = $this->auth->uploadToken($this->bucketName, $key);
  54 + $upManager = new UploadManager();
  55 + list($ret, $error) = $upManager->putFile($token, $key, __file__, null, 'text/plain', null);
  56 + $this->assertNull($error);
  57 + $this->assertNotNull($ret['hash']);
  58 + }
  59 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu\Http\Client;
  5 +
  6 +class HttpTest extends \PHPUnit_Framework_TestCase
  7 +{
  8 + public function testGet()
  9 + {
  10 + $response = Client::get('baidu.com');
  11 + $this->assertEquals($response->statusCode, 200);
  12 + $this->assertNotNull($response->body);
  13 + $this->assertNull($response->error);
  14 + }
  15 +
  16 + public function testGetQiniu()
  17 + {
  18 + $response = Client::get('up.qiniu.com');
  19 + $this->assertEquals(405, $response->statusCode);
  20 + $this->assertNotNull($response->body);
  21 + $this->assertNotNull($response->xReqId());
  22 + $this->assertNotNull($response->xLog());
  23 + $this->assertNotNull($response->error);
  24 + }
  25 +
  26 + public function testPost()
  27 + {
  28 + $response = Client::post('baidu.com', null);
  29 + $this->assertEquals($response->statusCode, 200);
  30 + $this->assertNotNull($response->body);
  31 + $this->assertNull($response->error);
  32 + }
  33 +
  34 + public function testPostQiniu()
  35 + {
  36 + $response = Client::post('up.qiniu.com', null);
  37 + $this->assertEquals($response->statusCode, 400);
  38 + $this->assertNotNull($response->body);
  39 + $this->assertNotNull($response->xReqId());
  40 + $this->assertNotNull($response->xLog());
  41 + $this->assertNotNull($response->error);
  42 + }
  43 +}
  1 +<?php
  2 +
  3 +namespace Qiniu\Tests;
  4 +
  5 +/**
  6 + * imageprocess test
  7 + *
  8 + * @package Qiniu
  9 + * @subpackage test
  10 + * @author Sherlock Ren <sherlock_ren@icloud.com>
  11 + */
  12 +class ImageUrlBuilderTest extends \PHPUnit_Framework_TestCase
  13 +{
  14 + /**
  15 + * 缩略图测试
  16 + *
  17 + * @test
  18 + * @return void
  19 + * @author Sherlock Ren <sherlock_ren@icloud.com>
  20 + */
  21 + public function testThumbutl()
  22 + {
  23 + $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
  24 + $url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
  25 + $url2 = $url . '?watermark/1/gravity/SouthEast/dx/0/dy/0/image/'
  26 + . 'aHR0cDovL2Fkcy1jZG4uY2h1Y2h1amllLmNvbS9Ga1R6bnpIY2RLdmRBUFc5cHZZZ3pTc21UY0tB';
  27 + // 异常测试
  28 + $this->assertEquals($url, $imageUrlBuilder->thumbnail($url, 1, 0, 0));
  29 + $this->assertEquals($url, \Qiniu\thumbnail($url, 1, 0, 0));
  30 +
  31 + // 简单缩略测试
  32 + $this->assertEquals(
  33 + $url . '?imageView2/1/w/200/h/200/ignore-error/1/',
  34 + $imageUrlBuilder->thumbnail($url, 1, 200, 200)
  35 + );
  36 + $this->assertEquals(
  37 + $url . '?imageView2/1/w/200/h/200/ignore-error/1/',
  38 + \Qiniu\thumbnail($url, 1, 200, 200)
  39 + );
  40 +
  41 + // 输出格式测试
  42 + $this->assertEquals(
  43 + $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
  44 + $imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png')
  45 + );
  46 + $this->assertEquals(
  47 + $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
  48 + \Qiniu\thumbnail($url, 1, 200, 200, 'png')
  49 + );
  50 +
  51 + // 渐进显示测试
  52 + $this->assertEquals(
  53 + $url . '?imageView2/1/w/200/h/200/format/png/interlace/1/ignore-error/1/',
  54 + $imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png', 1)
  55 + );
  56 + $this->assertEquals(
  57 + $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
  58 + \Qiniu\thumbnail($url, 1, 200, 200, 'png', 2)
  59 + );
  60 +
  61 + // 图片质量测试
  62 + $this->assertEquals(
  63 + $url . '?imageView2/1/w/200/h/200/format/png/interlace/1/q/80/ignore-error/1/',
  64 + $imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png', 1, 80)
  65 + );
  66 + $this->assertEquals(
  67 + $url . '?imageView2/1/w/200/h/200/format/png/interlace/1/ignore-error/1/',
  68 + \Qiniu\thumbnail($url, 1, 200, 200, 'png', 1, 101)
  69 + );
  70 +
  71 + // 多参数测试
  72 + $this->assertEquals(
  73 + $url2 . '|imageView2/1/w/200/h/200/ignore-error/1/',
  74 + $imageUrlBuilder->thumbnail($url2, 1, 200, 200)
  75 + );
  76 + $this->assertEquals(
  77 + $url2 . '|imageView2/1/w/200/h/200/ignore-error/1/',
  78 + \Qiniu\thumbnail($url2, 1, 200, 200)
  79 + );
  80 + }
  81 +
  82 + /**
  83 + * 图片水印测试
  84 + *
  85 + * @test
  86 + * @param void
  87 + * @return void
  88 + * @author Sherlock Ren <sherlock_ren@icloud.com>
  89 + */
  90 + public function waterImgTest()
  91 + {
  92 + $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
  93 + $url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
  94 + $url2 = $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/';
  95 + $image = 'http://developer.qiniu.com/resource/logo-2.jpg';
  96 +
  97 + // 水印简单测试
  98 + $this->assertEquals(
  99 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  100 + . '/dissolve/100/gravity/SouthEast/',
  101 + $imageUrlBuilder->waterImg($url, $image)
  102 + );
  103 + $this->assertEquals(
  104 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  105 + . '/gravity/SouthEast/',
  106 + $imageUrlBuilder->waterImg($url, $image, 101)
  107 + );
  108 + $this->assertEquals(
  109 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw==/',
  110 + $imageUrlBuilder->waterImg($url, $image, 101, 'sdfsd')
  111 + );
  112 + $this->assertEquals(
  113 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  114 + . '/dissolve/100/gravity/SouthEast/',
  115 + \Qiniu\waterImg($url, $image)
  116 + );
  117 +
  118 + // 横轴边距测试
  119 + $this->assertEquals(
  120 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  121 + . '/dissolve/100/gravity/SouthEast/dx/10/',
  122 + $imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10)
  123 + );
  124 + $this->assertEquals(
  125 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  126 + . '/dissolve/100/gravity/SouthEast/',
  127 + \Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad')
  128 + );
  129 +
  130 + // 纵轴边距测试
  131 + $this->assertEquals(
  132 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  133 + . '/dissolve/100/gravity/SouthEast/dx/10/dy/10/',
  134 + $imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10, 10)
  135 + );
  136 + $this->assertEquals(
  137 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  138 + . '/dissolve/100/gravity/SouthEast/',
  139 + \Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad', 'asdf')
  140 + );
  141 +
  142 + // 自适应原图的短边比例测试
  143 + $this->assertEquals(
  144 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  145 + . '/dissolve/100/gravity/SouthEast/dx/10/dy/10/ws/0.5/',
  146 + $imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10, 10, 0.5)
  147 + );
  148 + $this->assertEquals(
  149 + $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  150 + . '/dissolve/100/gravity/SouthEast/',
  151 + \Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad', 'asdf', 2)
  152 + );
  153 +
  154 + // 多参数测试
  155 + $this->assertEquals(
  156 + $url2 . '|watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  157 + . '/dissolve/100/gravity/SouthEast/',
  158 + $imageUrlBuilder->waterImg($url2, $image)
  159 + );
  160 + $this->assertEquals(
  161 + $url2 . '|watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  162 + . '/dissolve/100/gravity/SouthEast/',
  163 + \Qiniu\waterImg($url2, $image)
  164 + );
  165 + }
  166 +
  167 + /**
  168 + * 文字水印测试
  169 + *
  170 + * @test
  171 + * @param void
  172 + * @return void
  173 + * @author Sherlock Ren <sherlock_ren@icloud.com>
  174 + */
  175 + public function waterTextTest()
  176 + {
  177 + $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
  178 + $url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
  179 + $url2 = $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/';
  180 + $text = '测试一下';
  181 + $font = '微软雅黑';
  182 + $fontColor = '#FF0000';
  183 +
  184 + // 水印简单测试
  185 + $this->assertEquals($url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
  186 + . 'fontsize/500/dissolve/100/gravity/SouthEast/', $imageUrlBuilder->waterText($url, $text, $font, 500));
  187 + $this->assertEquals(
  188 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
  189 + . 'dissolve/100/gravity/SouthEast/',
  190 + \Qiniu\waterText($url, $text, $font, 'sdf')
  191 + );
  192 +
  193 + // 字体颜色测试
  194 + $this->assertEquals(
  195 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/'
  196 + . 'I0ZGMDAwMA==/dissolve/100/gravity/SouthEast/',
  197 + $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor)
  198 + );
  199 + $this->assertEquals(
  200 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA=='
  201 + . '/dissolve/100/gravity/SouthEast/',
  202 + \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor)
  203 + );
  204 +
  205 + // 透明度测试
  206 + $this->assertEquals(
  207 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
  208 + . '/dissolve/80/gravity/SouthEast/',
  209 + $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80)
  210 + );
  211 + $this->assertEquals(
  212 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA=='
  213 + . '/gravity/SouthEast/',
  214 + \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101)
  215 + );
  216 +
  217 + // 水印位置测试
  218 + $this->assertEquals(
  219 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
  220 + . '/dissolve/80/gravity/East/',
  221 + $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East')
  222 + );
  223 + $this->assertEquals(
  224 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
  225 + \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf')
  226 + );
  227 +
  228 + // 横轴距离测试
  229 + $this->assertEquals(
  230 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
  231 + . '/dissolve/80/gravity/East/dx/10/',
  232 + $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East', 10)
  233 + );
  234 + $this->assertEquals(
  235 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
  236 + \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf', 'sdfs')
  237 + );
  238 +
  239 + // 纵轴距离测试
  240 + $this->assertEquals(
  241 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
  242 + . '/dissolve/80/gravity/East/dx/10/dy/10/',
  243 + $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East', 10, 10)
  244 + );
  245 + $this->assertEquals(
  246 + $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
  247 + \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf', 'sdfs', 'ssdf')
  248 + );
  249 + // 多参数测试
  250 + $this->assertEquals(
  251 + $url2 . '|watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
  252 + . 'fontsize/500/dissolve/100/gravity/SouthEast/',
  253 + $imageUrlBuilder->waterText($url2, $text, $font, 500)
  254 + );
  255 + $this->assertEquals(
  256 + $url2 . '|watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
  257 + . 'fontsize/500/dissolve/100/gravity/SouthEast/',
  258 + \Qiniu\waterText($url2, $text, $font, 500)
  259 + );
  260 + }
  261 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu\Processing\Operation;
  5 +use Qiniu\Processing\PersistentFop;
  6 +
  7 +class PfopTest extends \PHPUnit_Framework_TestCase
  8 +{
  9 + public function testPfop()
  10 + {
  11 + global $testAuth;
  12 + $bucket = 'testres';
  13 + $key = 'sintel_trailer.mp4';
  14 + $pfop = new PersistentFop($testAuth, null);
  15 +
  16 + $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240';
  17 + list($id, $error) = $pfop->execute($bucket, $key, $fops);
  18 + $this->assertNull($error);
  19 + list($status, $error) = $pfop->status($id);
  20 + $this->assertNotNull($status);
  21 + $this->assertNull($error);
  22 + }
  23 +
  24 +
  25 + public function testPfops()
  26 + {
  27 + global $testAuth;
  28 + $bucket = 'testres';
  29 + $key = 'sintel_trailer.mp4';
  30 + $fops = array(
  31 + 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240',
  32 + 'vframe/jpg/offset/7/w/480/h/360',
  33 + );
  34 + $pfop = new PersistentFop($testAuth, null);
  35 +
  36 + list($id, $error) = $pfop->execute($bucket, $key, $fops);
  37 + $this->assertNull($error);
  38 +
  39 + list($status, $error) = $pfop->status($id);
  40 + $this->assertNotNull($status);
  41 + $this->assertNull($error);
  42 + }
  43 +
  44 + public function testMkzip()
  45 + {
  46 + global $testAuth;
  47 + $bucket = 'phpsdk';
  48 + $key = 'php-logo.png';
  49 + $pfop = new PersistentFop($testAuth, null);
  50 +
  51 + $url1 = 'http://phpsdk.qiniudn.com/php-logo.png';
  52 + $url2 = 'http://phpsdk.qiniudn.com/php-sdk.html';
  53 + $zipKey = 'test.zip';
  54 +
  55 + $fops = 'mkzip/2/url/' . \Qiniu\base64_urlSafeEncode($url1);
  56 + $fops .= '/url/' . \Qiniu\base64_urlSafeEncode($url2);
  57 + $fops .= '|saveas/' . \Qiniu\base64_urlSafeEncode("$bucket:$zipKey");
  58 +
  59 + list($id, $error) = $pfop->execute($bucket, $key, $fops);
  60 + $this->assertNull($error);
  61 +
  62 + list($status, $error) = $pfop->status($id);
  63 + $this->assertNotNull($status);
  64 + $this->assertNull($error);
  65 + }
  66 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu\Storage\ResumeUploader;
  5 +use Qiniu\Storage\UploadManager;
  6 +use Qiniu\Config;
  7 +use Qiniu\Zone;
  8 +
  9 +class ResumeUpTest extends \PHPUnit_Framework_TestCase
  10 +{
  11 + protected $bucketName;
  12 + protected $auth;
  13 +
  14 + protected function setUp()
  15 + {
  16 + global $bucketName;
  17 + $this->bucketName = $bucketName;
  18 +
  19 + global $testAuth;
  20 + $this->auth = $testAuth;
  21 + }
  22 +
  23 + public function test4ML()
  24 + {
  25 + $key = 'resumePutFile4ML';
  26 + $upManager = new UploadManager();
  27 + $token = $this->auth->uploadToken($this->bucketName, $key);
  28 + $tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
  29 + list($ret, $error) = $upManager->putFile($token, $key, $tempFile);
  30 + $this->assertNull($error);
  31 + $this->assertNotNull($ret['hash']);
  32 + unlink($tempFile);
  33 + }
  34 +
  35 + public function test4ML2()
  36 + {
  37 + $key = 'resumePutFile4ML';
  38 + $zone = new Zone(array('up.fake.qiniu.com'), array('up.qiniup.com'));
  39 + $cfg = new Config($zone);
  40 + $upManager = new UploadManager($cfg);
  41 + $token = $this->auth->uploadToken($this->bucketName, $key);
  42 + $tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
  43 + list($ret, $error) = $upManager->putFile($token, $key, $tempFile);
  44 + $this->assertNull($error);
  45 + $this->assertNotNull($ret['hash']);
  46 + unlink($tempFile);
  47 + }
  48 +
  49 + // public function test8M()
  50 + // {
  51 + // $key = 'resumePutFile8M';
  52 + // $upManager = new UploadManager();
  53 + // $token = $this->auth->uploadToken($this->bucketName, $key);
  54 + // $tempFile = qiniuTempFile(8*1024*1024+10);
  55 + // list($ret, $error) = $upManager->putFile($token, $key, $tempFile);
  56 + // $this->assertNull($error);
  57 + // $this->assertNotNull($ret['hash']);
  58 + // unlink($tempFile);
  59 + // }
  60 +}
  1 +<?php
  2 +namespace Qiniu\Tests;
  3 +
  4 +use Qiniu;
  5 +use Qiniu\Zone;
  6 +
  7 +class ZoneTest extends \PHPUnit_Framework_TestCase
  8 +{
  9 + protected $zone;
  10 + protected $zoneHttps;
  11 + protected $ak;
  12 +
  13 + protected $bucketName;
  14 + protected $bucketNameBC;
  15 + protected $bucketNameNA;
  16 +
  17 +
  18 + protected function setUp()
  19 + {
  20 + global $bucketName;
  21 + $this->bucketName = $bucketName;
  22 +
  23 + global $bucketNameBC;
  24 + $this->bucketNameBC = $bucketNameBC;
  25 +
  26 + global $bucketNameNA;
  27 + $this->bucketNameNA = $bucketNameNA;
  28 +
  29 + global $accessKey;
  30 + $this->ak = $accessKey;
  31 +
  32 + $this->zone = new Zone();
  33 + $this->zoneHttps = new Zone('https');
  34 + }
  35 +
  36 + public function testUpHosts()
  37 + {
  38 + $zone = Zone::queryZone($this->ak, $this->bucketName);
  39 + $this->assertContains('upload.qiniup.com', $zone->cdnUpHosts);
  40 +
  41 + $zone = Zone::queryZone($this->ak, $this->bucketNameBC);
  42 + $this->assertContains('upload-z1.qiniup.com', $zone->cdnUpHosts);
  43 +
  44 + $zone = Zone::queryZone($this->ak, $this->bucketNameNA);
  45 + $this->assertContains('upload-na0.qiniup.com', $zone->cdnUpHosts);
  46 + }
  47 +
  48 + public function testIoHosts()
  49 + {
  50 + $zone = Zone::queryZone($this->ak, $this->bucketName);
  51 + $this->assertEquals($zone->iovipHost, 'iovip.qbox.me');
  52 +
  53 + $zone = Zone::queryZone($this->ak, $this->bucketNameBC);
  54 + $this->assertEquals($zone->iovipHost, 'iovip-z1.qbox.me');
  55 +
  56 + $zone = Zone::queryZone($this->ak, $this->bucketNameNA);
  57 + $this->assertEquals($zone->iovipHost, 'iovip-na0.qbox.me');
  58 + }
  59 +}
  1 +<?php
  2 +// @codingStandardsIgnoreFile
  3 +require_once __DIR__ . '/../autoload.php';
  4 +
  5 +use Qiniu\Auth;
  6 +
  7 +$accessKey = getenv('QINIU_ACCESS_KEY');
  8 +$secretKey = getenv('QINIU_SECRET_KEY');
  9 +$testAuth = new Auth($accessKey, $secretKey);
  10 +
  11 +$bucketName = 'phpsdk';
  12 +$key = 'php-logo.png';
  13 +$key2 = 'niu.jpg';
  14 +
  15 +$bucketNameBC = 'phpsdk-bc';
  16 +$bucketNameNA = 'phpsdk-na';
  17 +
  18 +$dummyAccessKey = 'abcdefghklmnopq';
  19 +$dummySecretKey = '1234567890';
  20 +$dummyAuth = new Auth($dummyAccessKey, $dummySecretKey);
  21 +
  22 +//cdn
  23 +$timestampAntiLeechEncryptKey = getenv('QINIU_TIMESTAMP_ENCRPTKEY');
  24 +$customDomain = "http://phpsdk.qiniuts.com";
  25 +
  26 +$tid = getenv('TRAVIS_JOB_NUMBER');
  27 +if (!empty($tid)) {
  28 + $pid = getmypid();
  29 + $tid = strstr($tid, '.');
  30 + $tid .= '.' . $pid;
  31 +}
  32 +
  33 +function qiniuTempFile($size)
  34 +{
  35 + $fileName = tempnam(sys_get_temp_dir(), 'qiniu_');
  36 + $file = fopen($fileName, 'wb');
  37 + if ($size > 0) {
  38 + fseek($file, $size - 1);
  39 + fwrite($file, ' ');
  40 + }
  41 + fclose($file);
  42 + return $fileName;
  43 +}