Completed
Pull Request — master (#180)
by r
06:27
created

BucketManager::listFiles()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 4
eloc 13
c 4
b 1
f 0
nc 6
nop 5
dl 0
loc 18
ccs 13
cts 14
cp 0.9286
crap 4.0058
rs 9.2
1
<?php
2
namespace Qiniu\Storage;
3
4
use Qiniu\Auth;
5
use Qiniu\Config;
6
use Qiniu\Http\Client;
7
use Qiniu\Http\Error;
8
9
/**
10
 * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
11
 *
12
 * @link http://developer.qiniu.com/docs/v6/api/reference/rs/
13
 */
14
final class BucketManager
15
{
16
    private $auth;
17
18 39
    public function __construct(Auth $auth)
19
    {
20 39
        $this->auth = $auth;
21 39
    }
22
23
    /**
24
     * 获取指定账号下所有的空间名。
25
     *
26
     * @return string[] 包含所有空间名
27
     */
28 3
    public function buckets()
29
    {
30 3
        return $this->rsGet('/buckets');
31
    }
32
33
    /**
34
     * 列取空间的文件列表
35
     *
36
     * @param $bucket     空间名
37
     * @param $prefix     列举前缀
38
     * @param $marker     列举标识符
39
     * @param $limit      单次列举个数限制
40
     * @param $delimiter  指定目录分隔符
41
     *
42
     * @return array    包含文件信息的数组,类似:[
43
     *                                              {
44
     *                                                 "hash" => "<Hash string>",
45
     *                                                  "key" => "<Key string>",
46
     *                                                  "fsize" => "<file size>",
47
     *                                                  "putTime" => "<file modify time>"
48
     *                                              },
49
     *                                              ...
50
     *                                            ]
51
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
52
     */
53 3
    public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null)
54
    {
55 3
        $query = array('bucket' => $bucket);
56
57 3
        if ($prefix !== '') {
58 3
            $query['prefix'] = $prefix;
59 2
        }
60 3
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
61 3
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
62 3
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
63 3
        $url = Config::RSF_HOST . '/list?' . http_build_query($query);
64 3
        list($ret, $error) = $this->get($url);
65 3
        if ($ret === null) {
66
            return array(null, null, $error);
67
        }
68 3
        $marker = array_key_exists('marker', $ret) ? $ret['marker'] : null;
69 3
        return array($ret['items'], $marker, null);
70
    }
71
72
    /**
73
     * 获取资源的元信息,但不返回文件内容
74
     *
75
     * @param $bucket     待获取信息资源所在的空间
76
     * @param $key        待获取资源的文件名
77
     *
78
     * @return array    包含文件信息的数组,类似:
79
     *                                              [
80
     *                                                  "hash" => "<Hash string>",
81
     *                                                  "key" => "<Key string>",
82
     *                                                  "fsize" => "<file size>",
83
     *                                                  "putTime" => "<file modify time>"
84
     *                                              ]
85
     *
86
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html
87
     */
88 3
    public function stat($bucket, $key)
89
    {
90 3
        $path = '/stat/' . \Qiniu\entry($bucket, $key);
91 3
        return $this->rsGet($path);
92
    }
93
94
    /**
95
     * 删除指定资源
96
     *
97
     * @param $bucket     待删除资源所在的空间
98
     * @param $key        待删除资源的文件名
99
     *
100
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
101
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html
102
     */
103 15
    public function delete($bucket, $key)
104
    {
105 15
        $path = '/delete/' . \Qiniu\entry($bucket, $key);
106 15
        list(, $error) = $this->rsPost($path);
107 15
        return $error;
108
    }
109
110
111
    /**
112
     * 给资源进行重命名,本质为move操作。
113
     *
114
     * @param $bucket     待操作资源所在空间
115
     * @param $oldname    待操作资源文件名
116
     * @param $newname    目标资源文件名
117
     *
118
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
119
     */
120 3
    public function rename($bucket, $oldname, $newname)
121
    {
122 3
        return $this->move($bucket, $oldname, $bucket, $newname);
123
    }
124
125
    /**
126
     * 给资源进行重命名,本质为move操作。
127
     *
128
     * @param $from_bucket     待操作资源所在空间
129
     * @param $from_key        待操作资源文件名
130
     * @param $to_bucket       目标资源空间名
131
     * @param $to_key          目标资源文件名
132
     *
133
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
134
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html
135
     */
136 12
    public function copy($from_bucket, $from_key, $to_bucket, $to_key)
137
    {
138 12
        $from = \Qiniu\entry($from_bucket, $from_key);
139 12
        $to = \Qiniu\entry($to_bucket, $to_key);
140 12
        $path = '/copy/' . $from . '/' . $to;
141 12
        list(, $error) = $this->rsPost($path);
142 12
        return $error;
143
    }
144
145
    /**
146
     * 将资源从一个空间到另一个空间
147
     *
148
     * @param $from_bucket     待操作资源所在空间
149
     * @param $from_key        待操作资源文件名
150
     * @param $to_bucket       目标资源空间名
151
     * @param $to_key          目标资源文件名
152
     *
153
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
154
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/move.html
155
     */
156 3
    public function move($from_bucket, $from_key, $to_bucket, $to_key)
157
    {
158 3
        $from = \Qiniu\entry($from_bucket, $from_key);
159 3
        $to = \Qiniu\entry($to_bucket, $to_key);
160 3
        $path = '/move/' . $from . '/' . $to;
161 3
        list(, $error) = $this->rsPost($path);
162 3
        return $error;
163
    }
164
165
    /**
166
     * 主动修改指定资源的文件类型
167
     *
168
     * @param $bucket     待操作资源所在空间
169
     * @param $key        待操作资源文件名
170
     * @param $mime       待操作文件目标mimeType
171
     *
172
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
173
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html
174
     */
175 3
    public function changeMime($bucket, $key, $mime)
176
    {
177 3
        $resource = \Qiniu\entry($bucket, $key);
178 3
        $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
179 3
        $path = '/chgm/' . $resource . '/mime/' .$encode_mime;
180 3
        list(, $error) = $this->rsPost($path);
181 3
        return $error;
182
    }
183
184
    /**
185
     * 从指定URL抓取资源,并将该资源存储到指定空间中
186
     *
187
     * @param $url        指定的URL
188
     * @param $bucket     目标资源空间
189
     * @param $key        目标资源文件名
190
     *
191
     * @return array    包含已拉取的文件信息。
192
     *                         成功时:  [
193
     *                                          [
194
     *                                              "hash" => "<Hash string>",
195
     *                                              "key" => "<Key string>"
196
     *                                          ],
197
     *                                          null
198
     *                                  ]
199
     *
200
     *                         失败时:  [
201
     *                                          null,
202
     *                                         Qiniu/Http/Error
203
     *                                  ]
204
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html
205
     */
206 3
    public function fetch($url, $bucket, $key = null)
207
    {
208
209 3
        $resource = \Qiniu\base64_urlSafeEncode($url);
210 3
        $to = \Qiniu\entry($bucket, $key);
211 3
        $path = '/fetch/' . $resource . '/to/' . $to;
212 3
        return $this->ioPost($path);
213
    }
214
215
    /**
216
     * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
217
     *
218
     * @param $bucket     待获取资源所在的空间
219
     * @param $key        代获取资源文件名
220
     *
221
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
222
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html
223
     */
224 3
    public function prefetch($bucket, $key)
225
    {
226 3
        $resource = \Qiniu\entry($bucket, $key);
227 3
        $path = '/prefetch/' . $resource;
228 3
        list(, $error) = $this->ioPost($path);
229 3
        return $error;
230
    }
231
232
    /**
233
     * 在单次请求中进行多个资源管理操作
234
     *
235
     * @param $operations     资源管理操作数组
236
     *
237
     * @return array 每个资源的处理情况,结果类似:
238
     *              [
239
     *                   { "code" => <HttpCode int>, "data" => <Data> },
240
     *                   { "code" => <HttpCode int> },
241
     *                   { "code" => <HttpCode int> },
242
     *                   { "code" => <HttpCode int> },
243
     *                   { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
244
     *                   ...
245
     *               ]
246
     * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
247
     */
248 12
    public function batch($operations)
249
    {
250 12
        $params = 'op=' . implode('&op=', $operations);
251 12
        return $this->rsPost('/batch', $params);
252
    }
253
254 24
    private function rsPost($path, $body = null)
255
    {
256 24
        $url = Config::RS_HOST . $path;
257 24
        return $this->post($url, $body);
258
    }
259
260 6
    private function rsGet($path)
261
    {
262 6
        $url = Config::RS_HOST . $path;
263 6
        return $this->get($url);
264
    }
265
266 6
    private function ioPost($path, $body = null)
267
    {
268 6
        $url = Config::IO_HOST . $path;
269 6
        return $this->post($url, $body);
270
    }
271
272 9
    private function get($url)
273
    {
274 9
        $headers = $this->auth->authorization($url);
275 9
        $ret = Client::get($url, $headers);
276 9
        if (!$ret->ok()) {
277 6
            return array(null, new Error($url, $ret));
278
        }
279 9
        return array($ret->json(), null);
280
    }
281
282 30
    private function post($url, $body)
283
    {
284 30
        $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
285 30
        $ret = Client::post($url, $body, $headers);
286 30
        if (!$ret->ok()) {
287 3
            return array(null, new Error($url, $ret));
288
        }
289 27
        $r = ($ret->body === null) ? array() : $ret->json();
290 27
        return array($r, null);
291
    }
292
293 3
    public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket)
294
    {
295 3
        return self::twoKeyBatch('copy', $source_bucket, $key_pairs, $target_bucket);
296
    }
297
298
299 3
    public static function buildBatchRename($bucket, $key_pairs)
300
    {
301 3
        return self::buildBatchMove($bucket, $key_pairs, $bucket);
302
    }
303
304
305 6
    public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket)
306
    {
307 6
        return self::twoKeyBatch('move', $source_bucket, $key_pairs, $target_bucket);
308
    }
309
310
311 3
    public static function buildBatchDelete($bucket, $keys)
312
    {
313 3
        return self::oneKeyBatch('delete', $bucket, $keys);
314
    }
315
316
317 3
    public static function buildBatchStat($bucket, $keys)
318
    {
319 3
        return self::oneKeyBatch('stat', $bucket, $keys);
320
    }
321
322 6
    private static function oneKeyBatch($operation, $bucket, $keys)
323
    {
324 6
        $data = array();
325 6
        foreach ($keys as $key) {
326 6
            array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
327 4
        }
328 6
        return $data;
329
    }
330
331 9
    private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket)
332
    {
333 9
        if ($target_bucket === null) {
334
            $target_bucket = $source_bucket;
335
        }
336 9
        $data = array();
337 9
        foreach ($key_pairs as $from_key => $to_key) {
338 9
            $from = \Qiniu\entry($source_bucket, $from_key);
339 9
            $to = \Qiniu\entry($target_bucket, $to_key);
340 9
            array_push($data, $operation . '/' . $from . '/' . $to);
341 6
        }
342 9
        return $data;
343
    }
344
}
345