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
|
12 |
|
public function __construct(Config $config = null) |
19
|
|
|
{ |
20
|
12 |
|
if ($config === null) { |
21
|
9 |
|
$config = new Config(); |
22
|
6 |
|
} |
23
|
12 |
|
$this->config = $config; |
24
|
12 |
|
} |
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
|
3 |
|
public function put( |
44
|
|
|
$upToken, |
45
|
|
|
$key, |
46
|
|
|
$data, |
47
|
|
|
$params = null, |
48
|
|
|
$mime = 'application/octet-stream', |
49
|
|
|
$fname = null |
50
|
|
|
) { |
51
|
|
|
|
52
|
3 |
|
$params = self::trimParams($params); |
53
|
3 |
|
return FormUploader::put( |
54
|
3 |
|
$upToken, |
55
|
3 |
|
$key, |
56
|
3 |
|
$data, |
57
|
3 |
|
$this->config, |
58
|
3 |
|
$params, |
59
|
3 |
|
$mime, |
60
|
1 |
|
$fname |
61
|
2 |
|
); |
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
|
9 |
|
public function putFile( |
83
|
|
|
$upToken, |
84
|
|
|
$key, |
85
|
|
|
$filePath, |
86
|
|
|
$params = null, |
87
|
|
|
$mime = 'application/octet-stream', |
88
|
|
|
$checkCrc = false |
89
|
|
|
) { |
90
|
|
|
|
91
|
9 |
|
$file = fopen($filePath, 'rb'); |
92
|
9 |
|
if ($file === false) { |
93
|
|
|
throw new \Exception("file can not open", 1); |
94
|
|
|
} |
95
|
9 |
|
$params = self::trimParams($params); |
96
|
9 |
|
$stat = fstat($file); |
97
|
9 |
|
$size = $stat['size']; |
98
|
9 |
|
if ($size <= Config::BLOCK_SIZE) { |
99
|
3 |
|
$data = fread($file, $size); |
100
|
3 |
|
fclose($file); |
101
|
3 |
|
if ($data === false) { |
102
|
|
|
throw new \Exception("file can not read", 1); |
103
|
|
|
} |
104
|
3 |
|
return FormUploader::put( |
105
|
3 |
|
$upToken, |
106
|
3 |
|
$key, |
107
|
3 |
|
$data, |
108
|
3 |
|
$this->config, |
109
|
3 |
|
$params, |
110
|
3 |
|
$mime, |
111
|
3 |
|
$checkCrc, |
112
|
3 |
|
basename($filePath) |
|
|
|
|
113
|
2 |
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
$up = new ResumeUploader( |
117
|
6 |
|
$upToken, |
118
|
6 |
|
$key, |
119
|
6 |
|
$file, |
120
|
6 |
|
$size, |
121
|
6 |
|
$params, |
122
|
6 |
|
$mime, |
123
|
6 |
|
$this->config |
124
|
4 |
|
); |
125
|
6 |
|
$ret = $up->upload(basename($filePath)); |
126
|
6 |
|
fclose($file); |
127
|
6 |
|
return $ret; |
128
|
|
|
} |
129
|
|
|
|
130
|
12 |
|
public static function trimParams($params) |
131
|
|
|
{ |
132
|
12 |
|
if ($params === null) { |
133
|
12 |
|
return null; |
134
|
|
|
} |
135
|
|
|
$ret = array(); |
136
|
|
|
foreach ($params as $k => $v) { |
137
|
|
|
$pos = strpos($k, 'x:'); |
138
|
|
|
if ($pos === 0 && !empty($v)) { |
139
|
|
|
$ret[$k] = $v; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
return $ret; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.