1 | <?php |
||
2 | namespace Qiniu\Storage; |
||
3 | |||
4 | use Qiniu\Config; |
||
5 | use Qiniu\Http\HttpClient; |
||
6 | use Qiniu\Http\RequestOptions; |
||
7 | use Qiniu\Storage\ResumeUploader; |
||
8 | use Qiniu\Storage\FormUploader; |
||
9 | |||
10 | /** |
||
11 | * 主要涉及了资源上传接口的实现 |
||
12 | * |
||
13 | * @link http://developer.qiniu.com/docs/v6/api/reference/up/ |
||
14 | */ |
||
15 | final class UploadManager |
||
16 | { |
||
17 | private $config; |
||
18 | 12 | /** |
|
19 | * @var RequestOptions |
||
20 | 12 | */ |
|
21 | 9 | private $reqOpt; |
|
22 | 9 | ||
23 | 12 | /** |
|
24 | 12 | * @param Config|null $config |
|
25 | * @param RequestOptions|null $reqOpt |
||
26 | */ |
||
27 | public function __construct(Config $config = null, RequestOptions $reqOpt = null) |
||
28 | { |
||
29 | if ($config === null) { |
||
30 | $config = new Config(); |
||
31 | } |
||
32 | $this->config = $config; |
||
33 | |||
34 | if ($reqOpt === null) { |
||
35 | $reqOpt = new RequestOptions(); |
||
36 | } |
||
37 | |||
38 | $this->reqOpt = $reqOpt; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * 上传二进制流到七牛 |
||
43 | 3 | * |
|
44 | * @param string $upToken 上传凭证 |
||
45 | * @param string $key 上传文件名 |
||
46 | * @param string $data 上传二进制流 |
||
47 | * @param array<string, string> $params 自定义变量,规格参考 |
||
48 | * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar |
||
49 | * @param string $mime 上传数据的mimeType |
||
50 | * @param string $fname |
||
51 | * @param RequestOptions $reqOpt |
||
52 | 3 | * @return array 包含已上传文件的信息,类似: |
|
53 | 3 | * [ |
|
54 | 3 | * "hash" => "<Hash string>", |
|
55 | 3 | * "key" => "<Key string>" |
|
56 | 3 | * ] |
|
57 | 3 | */ |
|
58 | 3 | public function put( |
|
59 | 3 | $upToken, |
|
60 | $key, |
||
61 | 3 | $data, |
|
62 | $params = null, |
||
63 | $mime = 'application/octet-stream', |
||
64 | $fname = "default_filename", |
||
65 | $reqOpt = null |
||
66 | ) { |
||
67 | $reqOpt = $reqOpt === null ? $this->reqOpt : $reqOpt; |
||
68 | |||
69 | $params = self::trimParams($params); |
||
70 | return FormUploader::put( |
||
71 | $upToken, |
||
72 | $key, |
||
73 | $data, |
||
74 | $this->config, |
||
75 | $params, |
||
76 | $mime, |
||
77 | $fname, |
||
78 | $reqOpt |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | 9 | ||
83 | /** |
||
84 | * 上传文件到七牛 |
||
85 | * |
||
86 | * @param string $upToken 上传凭证 |
||
87 | * @param string $key 上传文件名 |
||
88 | * @param string $filePath 上传文件的路径 |
||
89 | * @param array<string, mixed> $params 定义变量,规格参考 |
||
90 | * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar |
||
91 | 9 | * @param boolean $mime 上传数据的mimeType |
|
92 | 9 | * @param string $checkCrc 是否校验crc32 |
|
93 | * @param string $resumeRecordFile 断点续传文件路径 默认为null |
||
94 | * @param string $version 分片上传版本 目前支持v1/v2版本 默认v1 |
||
95 | 9 | * @param int $partSize 分片上传v2字段 默认大小为4MB 分片大小范围为1 MB - 1 GB |
|
96 | 9 | * |
|
97 | 9 | * @return array<string, mixed> 包含已上传文件的信息,类似: |
|
98 | 9 | * [ |
|
99 | 3 | * "hash" => "<Hash string>", |
|
100 | 3 | * "key" => "<Key string>" |
|
101 | 3 | * ] |
|
102 | * @throws \Exception |
||
103 | */ |
||
104 | 3 | public function putFile( |
|
105 | 3 | $upToken, |
|
106 | 3 | $key, |
|
107 | 3 | $filePath, |
|
108 | 3 | $params = null, |
|
109 | 3 | $mime = 'application/octet-stream', |
|
110 | 3 | $checkCrc = false, |
|
0 ignored issues
–
show
|
|||
111 | 3 | $resumeRecordFile = null, |
|
112 | 3 | $version = 'v1', |
|
113 | $partSize = config::BLOCK_SIZE, |
||
114 | $reqOpt = null |
||
115 | 6 | ) { |
|
116 | 6 | $reqOpt = $reqOpt === null ? $this->reqOpt : $reqOpt; |
|
117 | 6 | ||
118 | 6 | $file = fopen($filePath, 'rb'); |
|
119 | 6 | if ($file === false) { |
|
120 | 6 | throw new \Exception("file can not open", 1); |
|
121 | 6 | } |
|
122 | 6 | $params = self::trimParams($params); |
|
123 | 6 | $stat = fstat($file); |
|
124 | 6 | $size = $stat['size']; |
|
125 | 6 | if ($size <= Config::BLOCK_SIZE) { |
|
126 | 6 | $data = fread($file, $size); |
|
127 | fclose($file); |
||
128 | if ($data === false) { |
||
129 | 12 | throw new \Exception("file can not read", 1); |
|
130 | } |
||
131 | 12 | return FormUploader::put( |
|
132 | 12 | $upToken, |
|
133 | $key, |
||
134 | $data, |
||
135 | $this->config, |
||
136 | $params, |
||
137 | $mime, |
||
138 | basename($filePath), |
||
139 | $reqOpt |
||
140 | ); |
||
141 | } |
||
142 | |||
143 | $up = new ResumeUploader( |
||
144 | $upToken, |
||
145 | $key, |
||
146 | $file, |
||
147 | $size, |
||
148 | $params, |
||
149 | $mime, |
||
150 | $this->config, |
||
151 | $resumeRecordFile, |
||
152 | $version, |
||
153 | $partSize, |
||
154 | $reqOpt |
||
155 | ); |
||
156 | $ret = $up->upload(basename($filePath)); |
||
157 | fclose($file); |
||
158 | return $ret; |
||
159 | } |
||
160 | |||
161 | public static function trimParams($params) |
||
162 | { |
||
163 | if ($params === null) { |
||
164 | return null; |
||
165 | } |
||
166 | $ret = array(); |
||
167 | foreach ($params as $k => $v) { |
||
168 | $pos1 = strpos($k, 'x:'); |
||
169 | $pos2 = strpos($k, 'x-qn-meta-'); |
||
170 | if (($pos1 === 0 || $pos2 === 0) && !empty($v)) { |
||
171 | $ret[$k] = $v; |
||
172 | } |
||
173 | } |
||
174 | return $ret; |
||
175 | } |
||
176 | } |
||
177 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.