Skip to content

Commit a8e2146

Browse files
committed
feat: 完善文档注释
1 parent 8083c14 commit a8e2146

File tree

8 files changed

+296
-82
lines changed

8 files changed

+296
-82
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"guzzlehttp/guzzle": "~6.0"
1313
},
1414
"require-dev": {
15-
"phpunit/phpunit": "~4.0"
15+
"phpunit/phpunit": "~4.0",
16+
"phpdocumentor/phpdocumentor": "^2.9"
1617
},
1718
"autoload": {
1819
"psr-4": { "Upyun\\": "src/Upyun/" }

src/Upyun/Api/Form.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Upyun\Api;
4+
5+
use Upyun\Signature;
6+
use GuzzleHttp\Client;
7+
8+
class Form extends Rest{
9+
10+
public function upload($path, $stream, $params) {
11+
$params['save-key'] = $path;
12+
$params['bucket'] = $this->config->bucketName;
13+
if (!isset($params['expiration'])) {
14+
$params['expiration'] = time() + 30 * 60 * 60; // 30 分钟
15+
}
16+
17+
$result = Signature::getFormSignature($this->config, $params);
18+
$policy = $result['policy'];
19+
$signature = $result['signature'];
20+
$client = new Client([
21+
'timeout' => $this->config->timeout,
22+
]);
23+
24+
$url = ($this->config->useSsl ? 'https://' : 'http://') . $this->endpoint;
25+
26+
$response = $client->request('POST', $url, array(
27+
'multipart' => array(
28+
array(
29+
'name' => 'policy',
30+
'contents' => $policy,
31+
),
32+
array(
33+
'name' => 'signature',
34+
'contents' => $signature,
35+
),
36+
array(
37+
'name' => 'file',
38+
'contents' => $stream,
39+
)
40+
)
41+
));
42+
return $response->getStatusCode() === 200;
43+
}
44+
}

src/Upyun/Api/Multi.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public function __construct(Config $config) {
2222
}
2323

2424
/**
25-
* @param $path upyun storage path
26-
* @param Psr7\stream $resource content that will be stored in upyun
27-
* @param array $params
25+
* @param string $path 文件存储路径
26+
* @param Psr7\stream $stream 通过 `Psr7\stream_for` 方法格式化的流资源
27+
* @param string $fileHash 文件 md5 值
28+
* @param array $params 其他自定义参数
2829
*
2930
* @return Psr7\Response
3031
* @throws \Exception

src/Upyun/Api/Pretreat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function process($source, $tasks) {
2525

2626
$params = array(
2727
'bucket_name' => $this->config->bucketName,
28-
'notify_url' => $this->config->videoNotifyUrl,
28+
'notify_url' => $this->config->processNotifyUrl,
2929
'source' => $source,
3030
'tasks' => $encodedTasks,
3131
'accept' => 'json'

src/Upyun/Config.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,89 @@
88
*/
99
class Config {
1010
/**
11-
* @var string
11+
* @var string 服务名称
1212
*/
1313
public $bucketName;
1414
/**
15-
* @var string
15+
* @var string 操作员名
1616
*/
1717
public $operatorName;
1818
/**
19-
* @var string
19+
* @var string 操作员密码 md5 hash 值
2020
*/
2121
public $operatorPassword;
2222

2323
/**
24-
* @var bool: if true, use ssl
24+
* @var bool 是否使用 https
2525
*/
2626
public $useSsl;
2727

2828
/**
29-
* @var string: REST, use rest api upload file; BLOCK use multipart api upload file; AUTO, decide by file size
29+
* @var string 上传使用的接口类型,可以设置为 `REST`:使用 rest api 上传,`AUTO` 根据文件大小自动判断,`BLOCK` 使用断点续传
30+
* 当上传小文件时,不推荐使用断点续传;上传时如果设置了异步预处理`withAsyncProcess=true`,将会使用表单 api 上传
3031
*/
3132
public $uploadType = 'AUTO';
3233

3334
/**
34-
* @var int: if upload type is AUTO, when file size big than 10M will choose block upload, else use rest api upload
35+
* @var int 上传的接口类型设置为 `AUTO` 时,文件大小的边界值:小于该值时,使用 rest api,否则使用断点续传。 默认 30M
3536
*/
36-
public $sizeBoundary = 10485760;
37+
public $sizeBoundary = 31457280;
3738
/**
38-
* @var int: max block size 5M
39+
* @var int 分块上传`Multi`接口的最大分块值
3940
*/
4041
public $maxBlockSize = 5242880;
4142

43+
/**
44+
* @var int 分块时,每个块的过期时间
45+
*/
4246
public $blockExpiration = 60;
4347

4448
/**
45-
* @var int: request timeout seconds
49+
* @var int request timeout seconds
4650
*/
4751
public $timeout = 60;
48-
49-
50-
public $videoNotifyUrl;
51-
52-
public $formReturnUrl;
53-
public $formNotifyUrl;
54-
52+
53+
54+
/**
55+
* @var string 异步云处理的回调通知地址
56+
*/
57+
public $processNotifyUrl;
58+
5559
private $version = '3.0.0';
5660

5761

5862

5963
/**
60-
* @var string
64+
* @var string 表单 api 的秘钥
6165
*/
6266
private $formApiKey;
6367

6468
/**
65-
* @var string
69+
* @var string rest api 和 form api 的接口地址
6670
*/
6771
static $restApiEndPoint;
6872

6973

7074
/**
71-
* different route type, detail see http://docs.upyun.com/api/
75+
* rest api 和 form api 接口请求地址,详见:http://docs.upyun.com/api/rest_api/
7276
*/
7377
const ED_AUTO = 'v0.api.upyun.com';
7478
const ED_TELECOM = 'v1.api.upyun.com';
7579
const ED_CNC = 'v2.api.upyun.com';
7680
const ED_CTT = 'v3.api.upyun.com';
7781

7882
/**
79-
* multipart api endpoint
83+
* 分块上传接口请求地址
8084
*/
8185
const ED_FORM = 'm0.api.upyun.com';
8286

8387
/**
84-
* media api endpoint
88+
* 异步云处理接口地址
8589
*/
8690
const ED_VIDEO = 'p0.api.upyun.com';
8791

8892
/**
89-
* purge api endpoint
93+
* 刷新接口地址
9094
*/
9195
const ED_PURGE = 'http://purge.upyun.com/purge/';
9296

@@ -113,8 +117,8 @@ public function getFormApiKey() {
113117
public function setFormApiKey($key) {
114118
$this->formApiKey = $key;
115119
}
116-
120+
117121
public function getVersion() {
118-
return $this->version;
122+
return $this->version;
119123
}
120124
}

src/Upyun/Uploader.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Upyun;
33

44
use Upyun\Api\Rest;
5-
use Upyun\Api\Multi;
5+
use Upyun\Api\Form;
66
use GuzzleHttp\Psr7;
77

88
class Uploader {
@@ -18,11 +18,16 @@ public function __construct(Config $config) {
1818
$this->config = $config;
1919
}
2020

21-
public function upload($path, $file, $params) {
21+
public function upload($path, $file, $params, $withAsyncProcess) {
2222
$stream = Psr7\stream_for($file);
2323
$size = $stream->getSize();
2424
$useBlock = $this->needUseBlock($size);
2525

26+
if ($withAsyncProcess) {
27+
$req = new Form($this->config);
28+
return $req->upload($path, $stream, $params);
29+
}
30+
2631
if(! $useBlock) {
2732
$req = new Rest($this->config);
2833
return $req->request('PUT', $path)
@@ -31,11 +36,6 @@ public function upload($path, $file, $params) {
3136
->send();
3237
} else {
3338
return $this->pointUpload($path, $stream, $params);
34-
/**
35-
* 也可以使用分块上传替代, 不推荐使用
36-
*/
37-
//$req = new Multi($this->config);
38-
//return $req->upload($path, $stream, Util::md5Hash($file), $params);
3939
}
4040
}
4141

0 commit comments

Comments
 (0)