Completed
Pull Request — master (#164)
by Bai
25:40
created

BucketManager   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 328
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 91.67%

Importance

Changes 13
Bugs 2 Features 1
Metric Value
wmc 32
c 13
b 2
f 1
lcom 2
cbo 4
dl 0
loc 328
ccs 99
cts 108
cp 0.9167
rs 9.6

24 Methods

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