Passed
Pull Request — master (#346)
by huang
43:36 queued 18:42
created

BucketManager::putBucketEvent()   C

Complexity

Conditions 10
Paths 256

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 10
eloc 24
nc 256
nop 8
dl 0
loc 43
ccs 0
cts 13
cp 0
crap 110
rs 6.1333
c 2
b 0
f 1

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Qiniu\Storage;
4
5
use Qiniu\Auth;
6
use Qiniu\Config;
7
use Qiniu\Http\Error;
8
use Qiniu\Http\Client;
9
10
/**
11
 * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
12
 *
13
 * @link https://developer.qiniu.com/kodo/api/1274/rs
14
 */
15
final class BucketManager
16
{
17
    private $auth;
18
    private $config;
19
20 42
    public function __construct(Auth $auth, Config $config = null)
21
    {
22 42
        $this->auth = $auth;
23 42
        if ($config == null) {
24 42
            $this->config = new Config();
25 42
        } else {
26
            $this->config = $config;
27
        }
28 42
    }
29
30
    /**
31
     * 获取指定账号下所有的空间名
32
     *
33
     * @param bool $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
34
     * @return array 包含所有空间名
35 3
     */
36
    public function buckets($shared = true)
37 3
    {
38 3
        $includeShared = "false";
39 3
        if ($shared === true) {
40 3
            $includeShared = "true";
41 3
        }
42
        return $this->rsGet('/buckets?shared=' . $includeShared);
43
    }
44
45
    /**
46
     * 列举空间,返回bucket列表
47
     *
48
     * @param string $region 区域
49
     * @param string $line
50
     * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
51
     * @return array
52
     */
53
    public function listbuckets(
54
        $region = null,
55
        $line = 'false',
56
        $shared = 'false'
57
    ) {
58
        $path = '/v3/buckets?region=' . $region . '&line=' . $line . '&shared=' . $shared;
59
        return $this->ucPost($path);
60
    }
61
62
    /**
63
     * 创建空间
64
     *
65
     * @param string $name 创建的空间名
66
     * @param string $region 创建的区域,默认华东
67
     *
68
     * @return array
69
     * @link https://developer.qiniu.com/kodo/api/1382/mkbucketv3
70
     */
71
    public function createBucket($name, $region = 'z0')
72
    {
73
        $path = '/mkbucketv3/' . $name . '/region/' . $region;
74
        return $this->rsPost($path, null);
75
    }
76
77
    /**
78
     * 删除空间
79
     *
80
     * @param string $name 需要删除的目标空间名
81
     *
82
     * @return array
83
     * @link https://developer.qiniu.com/kodo/api/1601/drop-bucket
84
     */
85
    public function deleteBucket($name)
86
    {
87
        $path = '/drop/' . $name;
88
        return $this->rsPost($path, null);
89
    }
90
91
    /**
92
     * 获取指定空间绑定的所有的域名
93
     *
94
     * @param string $bucket 空间名称
95
     * @return array
96
     */
97
    public function domains($bucket)
98
    {
99
        return $this->apiGet('/v6/domain/list?tbl=' . $bucket);
100
    }
101
102
    /**
103
     * 获取指定空间的相关信息
104
     *
105
     * @param string $bucket 空间名称
106
     * @return array
107
     */
108
    public function bucketInfo($bucket)
109
    {
110
        $path = '/v2/bucketInfo?bucket=' . $bucket;
111
        return $this->ucPost($path);
112
    }
113
114
    /**
115
     * 获取指定zone的空间信息列表
116
     *
117
     * @param string $region 区域
118
     * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
119
     * @param string $fs 如果为 true,会返回每个空间当前的文件数和存储量(实时数据)
120
     * @return array
121
     */
122
    public function bucketInfos($region = null, $shared = 'false', $fs = 'false')
123
    {
124
        $path = '/v2/bucketInfos?region=' . $region . '&shared=' . $shared . '&fs=' . $fs;
125
        return $this->ucPost($path);
126
    }
127
128
    /**
129
     * 列取空间的文件列表
130
     *
131
     * @param string $bucket 空间名
132
     * @param string $prefix 列举前缀
133
     * @param string $marker 列举标识符
134
     * @param int $limit 单次列举个数限制
135
     * @param string $delimiter 指定目录分隔符
136
     *
137
     * @return array
138
     * @link  https://developer.qiniu.com/kodo/api/1284/list
139
     */
140
    public function listFiles(
141
        $bucket,
142
        $prefix = null,
143
        $marker = null,
144
        $limit = 1000,
145
        $delimiter = null
146
    ) {
147
        $query = array('bucket' => $bucket);
148
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
149
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
150
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
151
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
152
        $url = $this->getRsfHost() . '/list?' . http_build_query($query);
153
        return $this->get($url);
154
    }
155
156
    /**
157 3
     * 列取空间的文件列表
158
     *
159
     * @param string $bucket 空间名
160
     * @param string $prefix 列举前缀
161
     * @param string $marker 列举标识符
162
     * @param int $limit 单次列举个数限制
163
     * @param string $delimiter 指定目录分隔符
164 3
     * @param bool $skipconfirm 是否跳过已删除条目的确认机制
165 3
     *
166 3
     * @return array
167 3
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
168 3
     */
169 3
    public function listFilesv2(
170 3
        $bucket,
171
        $prefix = null,
172
        $marker = null,
173
        $limit = 1000,
174
        $delimiter = null,
175
        $skipconfirm = true
176
    ) {
177
        $query = array('bucket' => $bucket);
178
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
179
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
180
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
181
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
182
        \Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm);
183
        $path = '/v2/list?' . http_build_query($query);
184
        $url = $this->getRsfHost() . $path;
185
        $headers = $this->auth->authorization($url, null, 'application/x-www-form-urlencoded');
186
        $ret = Client::post($url, null, $headers);
187
        if (!$ret->ok()) {
188
            return array(null, new Error($url, $ret));
189
        }
190
        $r = explode("\n", $ret->body);
191
        array_pop($r);
192
        return array($r, null);
193
    }
194
195
    /**
196
     * 增加bucket生命规则
197
     *
198
     * @param string $bucket 空间名
199
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为
200
     * 字母、数字、下划线
201
     * @param string $prefix 同一个 bucket 里面前缀不能重复
202
     * @param int $delete_after_days 指定上传文件多少天后删除,指定为0表示不删除,
203
     * 大于0表示多少天后删除,需大于 to_line_after_days
204
     * @param int $to_line_after_days 指定文件上传多少天后转低频存储。指定为0表示
205
     * 不转低频存储,小于0表示上传的文件立即变低频存储
206
     * @return array
207
     */
208
    public function bucketLifecycleRule(
209
        $bucket,
210
        $name,
211
        $prefix,
212
        $delete_after_days,
213
        $to_line_after_days
214
    ) {
215
        $path = '/rules/add';
216
        $params = array();
217
        if ($bucket) {
218
            $params['bucket'] = $bucket;
219
        }
220
        if ($name) {
221
            $params['name'] = $name;
222
        }
223
        if ($prefix) {
224
            $params['prefix'] = $prefix;
225
        }
226
        if ($delete_after_days) {
227
            $params['delete_after_days'] = $delete_after_days;
228
        }
229
        if ($to_line_after_days) {
230
            $params['to_line_after_days'] = $to_line_after_days;
231
        }
232
        $data = http_build_query($params);
233
        $info = $this->ucPost($path, $data);
234
        return $info;
235
    }
236
237
    /**
238
     * 更新bucket生命规则
239
     *
240
     * @param string $bucket 空间名
241
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、
242
     * 数字、下划线
243
     * @param string $prefix 同一个 bucket 里面前缀不能重复
244
     * @param int $delete_after_days 指定上传文件多少天后删除,指定为0表示不删除,
245
     * 大于0表示多少天后删除,需大于 to_line_after_days
246
     * @param int $to_line_after_days 指定文件上传多少天后转低频存储。指定为0表示不
247
     * 转低频存储,小于0表示上传的文件立即变低频存储
248
     * @return array
249
     */
250
    public function updateBucketLifecycleRule(
251
        $bucket,
252
        $name,
253
        $prefix,
254
        $delete_after_days,
255
        $to_line_after_days
256
    ) {
257
        $path = '/rules/update';
258
        $params = array();
259
        if ($bucket) {
260
            $params['bucket'] = $bucket;
261
        }
262
        if ($name) {
263
            $params['name'] = $name;
264
        }
265
        if ($prefix) {
266
            $params['prefix'] = $prefix;
267
        }
268
        if ($delete_after_days) {
269
            $params['delete_after_days'] = $delete_after_days;
270
        }
271
        if ($to_line_after_days) {
272
            $params['to_line_after_days'] = $to_line_after_days;
273
        }
274
        $data = http_build_query($params);
275
        return $this->ucPost($path, $data);
276
    }
277
278
    /**
279
     * 获取bucket生命规则
280
     *
281
     * @param string $bucket 空间名
282
     * @return array
283
     */
284
    public function getBucketLifecycleRules($bucket)
285
    {
286
        $path = '/rules/get?bucket=' . $bucket;
287
        $info = $this->ucGet($path);
288
        return $info;
289
    }
290
291
    /**
292
     * 删除bucket生命规则
293
     *
294
     * @param string $bucket 空间名
295
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
296
     * 只能为字母、数字、下划线()
297
     * @return array
298
     */
299
    public function deleteBucketLifecycleRule($bucket, $name)
300
    {
301
        $path = '/rules/delete';
302
        $params = array();
303
        if ($bucket) {
304
            $params['bucket'] = $bucket;
305
        }
306
        if ($name) {
307
            $params['name'] = $name;
308
        }
309
        $data = http_build_query($params);
310
        $info = $this->ucPost($path, $data);
311
        return $info;
312
    }
313
314
    /**
315
     * 增加bucket事件通知规则
316
     *
317
     * @param string $bucket 空间名
318
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
319
     * 只能为字母、数字、下划线()
320
     * @param string $prefix 同一个 bucket 里面前缀不能重复
321
     * @param string $suffix 可选,文件配置的后缀
322
     * @param array $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,
323
     * disable,enable,deleteMarkerCreate
324
     * @param string $callbackURL 通知URL,可以指定多个,失败依次重试
325
     * @param string $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
326
     * @param string $host 可选,通知请求的host
327
     *
328
     * @return array
329
     */
330
    public function putBucketEvent(
331
        $bucket,
332
        $name,
333
        $prefix,
334
        $suffix,
335
        $event,
336
        $callbackURL,
337
        $access_key = null,
338
        $host = null
339
    ) {
340
        $path = '/events/add';
341
        $params = array();
342
        if (!empty($bucket)) {
343
            $params['bucket'] = $bucket;
344
        }
345
        if (!empty($name)) {
346
            $params['name'] = $name;
347
        }
348
        if (!empty($prefix)) {
349
            $params['prefix'] = $prefix;
350
        }
351
        if (!empty($suffix)) {
352
            $params['suffix'] = $suffix;
353
        }
354
        if (!empty($callbackURL)) {
355
            $params['callbackURL'] = $callbackURL;
356
        }
357
        if (!empty($access_key)) {
358
            $params['access_key'] = $access_key;
359
        }
360
        if (!empty($host)) {
361
            $params['host'] = $host;
362
        }
363
        $data = http_build_query($params);
364
        if (!empty($event)) {
365
            $eventpath = "";
366
            foreach ($event as $key => $value) {
367
                $eventpath .= "&event=$value";
368
            }
369
            $data .= $eventpath;
370
        }
371
        $info = $this->ucPost($path, $data);
372
        return $info;
373
    }
374
375
    /**
376
     * 更新bucket事件通知规则
377
     *
378
     * @param string $bucket 空间名
379
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
380
     * 只能为字母、数字、下划线()
381
     * @param string $prefix 同一个 bucket 里面前缀不能重复
382
     * @param string $suffix 可选,文件配置的后缀
383
     * @param array $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,
384
     * enable,deleteMarkerCreate
385
     * @param string $callbackURL 通知URL,可以指定多个,失败依次重试
386
     * @param string $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
387
     * @param string $host 可选,通知请求的host
388
     *
389
     * @return array
390
     */
391
    public function updateBucketEvent(
392
        $bucket,
393
        $name,
394
        $prefix,
395
        $suffix,
396
        $event,
397
        $callbackURL,
398
        $access_key = null,
399
        $host = null
400
    ) {
401
        $path = '/events/update';
402
        $params = array();
403
        if (!empty($bucket)) {
404
            $params['bucket'] = $bucket;
405
        }
406
        if (!empty($name)) {
407
            $params['name'] = $name;
408
        }
409
        if (!empty($prefix)) {
410
            $params['prefix'] = $prefix;
411
        }
412
        if ($suffix) {
413
            $params['suffix'] = $suffix;
414
        }
415
        if (!empty($event)) {
416
            $params['event'] = $event;
417
        }
418
        if (!empty($callbackURL)) {
419
            $params['callbackURL'] = $callbackURL;
420
        }
421
        if (!empty($access_key)) {
422
            $params['access_key'] = $access_key;
423
        }
424
        if (!empty($host)) {
425
            $params['host'] = $host;
426
        }
427
        $data = http_build_query($params);
428
        if (!empty($event)) {
429
            $eventpath = "";
430
            foreach ($event as $key => $value) {
431
                $eventpath .= "&event=$value";
432
            }
433
            $data .= $eventpath;
434
        }
435
        return $this->ucPost($path, $data);
436
    }
437
438
    /**
439
     * 获取bucket事件通知规则
440
     *
441
     * @param string $bucket 空间名
442
     * @return array
443
     */
444
    public function getBucketEvents($bucket)
445
    {
446
        $path = '/events/get?bucket=' . $bucket;
447
        return $this->ucGet($path);
448
    }
449
450
    /**
451
     * 删除bucket事件通知规则
452
     *
453
     * @param string $bucket 空间名
454
     * @param string $name 规则名称bucket内唯一,长度小于50,不能为空,只能为字母、数字、下划线
455
     * @return array
456
     */
457
    public function deleteBucketEvent($bucket, $name)
458
    {
459
        $path = '/events/delete';
460
        $params = array();
461
        if ($bucket) {
462
            $params['bucket'] = $bucket;
463
        }
464
        if ($name) {
465
            $params['name'] = $name;
466
        }
467
        $data = http_build_query($params);
468
        return $this->ucPost($path, $data);
469
    }
470
471
    /**
472
     * 获取bucket的跨域信息
473
     *
474
     * @param string $bucket 空间名
475
     * @return array
476
     */
477
    public function getCorsRules($bucket)
478
    {
479
        $path = '/corsRules/get/' . $bucket;
480
        return $this->ucGet($path);
481
    }
482
483
    /**
484
     * 开关原图保护
485
     *
486
     * @param string $bucket 空间名称
487
     * @param int $mode mode 为1表示开启原图保护,0表示关闭
488
     * @return array
489
     */
490
    public function putBucketAccessStyleMode($bucket, $mode)
491
    {
492
        $path = '/accessMode/' . $bucket . '/mode/' . $mode;
493
        return $this->ucPost($path, null);
494
    }
495
496
    /**
497
     * 设置私有属性
498
     *
499
     * @param string $bucket 空间名称
500
     * @param int $private private为0表示公开,为1表示私有
501
     * @return array
502
     */
503
    public function putBucketAccessMode($bucket, $private)
504
    {
505
        $path = "/private?bucket=$bucket&private=$private";
506
        return $this->ucPost($path, null);
507
    }
508
509
    /**
510
     * 设置 referer 防盗链
511
     *
512
     * @param string $bucket 空间名称
513
     * @param int $mode 0:关闭Referer(使用此选项将会忽略以下参数并将恢复默认值);
514
     *                  1:设置Referer白名单; 2:设置Referer黑名单
515
     * @param string $norefer 0:不允许空 Refer 访问; 1:表示允许空Refer访问
516
     * @param string $pattern 规则字符串
517
     * @param int $enabled 源站是否支持,默认为0只给CDN配置, 设置为1表示开启源站防盗链
518
     * @return array
519
     * @link https://developer.qiniu.com/kodo/manual/6093/set-the-hotlinking-prevention
520
     */
521
    public function putReferAntiLeech($bucket, $mode, $norefer, $pattern, $enabled = 1)
522
    {
523
        $path = "/referAntiLeech?bucket=$bucket&mode=$mode&norefer=$norefer&pattern=$pattern&source_enabled=$enabled";
524
        return $this->ucPost($path, null);
525
    }
526
527
    /**
528
     * 设置Bucket的maxAge
529
     *
530
     * @param string $bucket 空间名称
531
     * @param int $maxAge maxAge为0或者负数表示为默认值(31536000)
532
     * @return array
533
     */
534
    public function putBucketMaxAge($bucket, $maxAge)
535
    {
536
        $path = '/maxAge?bucket=' . $bucket . '&maxAge=' . $maxAge;
537
        return $this->ucPost($path, null);
538
    }
539
540
    /**
541
     * 设置空间配额
542
     *
543
     * @param string $bucket 空间名称,不支持授权空间
544
     * @param string $size 空间存储量配额,参数传入0或不传表示不更改当前配置,传入-1表示取消限额,新创建的空间默认没有限额
545
     * @param string $count 空间文件数配额,参数含义同<size>
546
     * @return array
547
     */
548
    public function putBucketQuota($bucket, $size, $count)
549
    {
550
        $path = '/setbucketquota/' . $bucket . '/size/' . $size . '/count/' . $count;
551
        return $this->apiPost($path, null);
552
    }
553
554
    /**
555
     * 获取空间配额
556
     *
557
     * @param string $bucket 空间名称
558
     * @return array
559
     */
560
    public function getBucketQuota($bucket)
561
    {
562
        $path = '/getbucketquota/' . $bucket;
563
        return $this->apiPost($path, null);
564
    }
565
566
    /**
567
     * 获取资源的元信息,但不返回文件内容
568
     *
569
     * @param string $bucket 待获取信息资源所在的空间
570
     * @param string $key 待获取资源的文件名
571
     *
572
     * @return array
573
     * @link  https://developer.qiniu.com/kodo/api/1308/stat
574
     */
575
    public function stat($bucket, $key)
576
    {
577
        $path = '/stat/' . \Qiniu\entry($bucket, $key);
578
        return $this->rsGet($path);
579
    }
580
581
    /**
582
     * 删除指定资源
583
     *
584
     * @param string $bucket 待删除资源所在的空间
585
     * @param string $key 待删除资源的文件名
586
     *
587
     * @return array
588
     * @link  https://developer.qiniu.com/kodo/api/1257/delete
589
     */
590
    public function delete($bucket, $key)
591
    {
592
        $path = '/delete/' . \Qiniu\entry($bucket, $key);
593
        return $this->rsPost($path);
594
    }
595
596
    /**
597
     * 给资源进行重命名,本质为move操作。
598
     *
599
     * @param string $bucket 待操作资源所在空间
600
     * @param string $oldname 待操作资源文件名
601
     * @param string $newname 目标资源文件名
602
     *
603
     * @return array
604
     */
605
    public function rename($bucket, $oldname, $newname)
606
    {
607
        return $this->move($bucket, $oldname, $bucket, $newname);
608
    }
609
610
    /**
611
     * 对资源进行复制。
612
     *
613
     * @param string $from_bucket 待操作资源所在空间
614
     * @param string $from_key 待操作资源文件名
615
     * @param string $to_bucket 目标资源空间名
616
     * @param string $to_key 目标资源文件名
617
     *
618
     * @return array
619
     * @link  https://developer.qiniu.com/kodo/api/1254/copy
620
     */
621
    public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
622
    {
623
        $from = \Qiniu\entry($from_bucket, $from_key);
624
        $to = \Qiniu\entry($to_bucket, $to_key);
625
        $path = '/copy/' . $from . '/' . $to;
626
        if ($force === true) {
627
            $path .= '/force/true';
628
        }
629
        return $this->rsPost($path);
630
    }
631
632
    /**
633
     * 将资源从一个空间到另一个空间
634
     *
635
     * @param string $from_bucket 待操作资源所在空间
636
     * @param string $from_key 待操作资源文件名
637
     * @param string $to_bucket 目标资源空间名
638
     * @param string $to_key 目标资源文件名
639
     *
640
     * @return array
641
     * @link  https://developer.qiniu.com/kodo/api/1288/move
642
     */
643
    public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
644
    {
645
        $from = \Qiniu\entry($from_bucket, $from_key);
646
        $to = \Qiniu\entry($to_bucket, $to_key);
647
        $path = '/move/' . $from . '/' . $to;
648
        if ($force) {
649
            $path .= '/force/true';
650
        }
651
        return $this->rsPost($path);
652
    }
653
654
    /**
655
     * 主动修改指定资源的文件元信息
656
     *
657
     * @param string $bucket 待操作资源所在空间
658
     * @param string $key 待操作资源文件名
659
     * @param string $mime 待操作文件目标mimeType
660
     *
661
     * @return array
662
     * @link  https://developer.qiniu.com/kodo/api/1252/chgm
663
     */
664
    public function changeMime($bucket, $key, $mime)
665
    {
666
        $resource = \Qiniu\entry($bucket, $key);
667
        $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
668
        $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
669
        return $this->rsPost($path);
670
    }
671
672
673
    /**
674
     * 修改指定资源的存储类型
675
     *
676
     * @param string $bucket 待操作资源所在空间
677 6
     * @param string $key 待操作资源文件名
678
     * @param int $fileType 0 表示标准存储;1 表示低频存储;2 表示归档存储
679 6
     *
680 6
     * @return array
681
     * @link  https://developer.qiniu.com/kodo/api/3710/chtype
682
     */
683
    public function changeType($bucket, $key, $fileType)
684
    {
685
        $resource = \Qiniu\entry($bucket, $key);
686
        $path = '/chtype/' . $resource . '/type/' . $fileType;
687
        return $this->rsPost($path);
688
    }
689
690
    /**
691
     * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
692 15
     *
693
     * @param string $bucket 待操作资源所在空间
694 15
     * @param string $key 待操作资源文件名
695 15
     * @param int $status 0表示启用;1表示禁用
696 15
     *
697
     * @return array
698
     * @link  https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
699
     */
700
    public function changeStatus($bucket, $key, $status)
701
    {
702
        $resource = \Qiniu\entry($bucket, $key);
703
        $path = '/chstatus/' . $resource . '/status/' . $status;
704
        return $this->rsPost($path);
705
    }
706
707
    /**
708
     * 从指定URL抓取资源,并将该资源存储到指定空间中
709 3
     *
710
     * @param string $url 指定的URL
711 3
     * @param string $bucket 目标资源空间
712
     * @param string $key 目标资源文件名
713
     *
714
     * @return array
715
     * @link  https://developer.qiniu.com/kodo/api/1263/fetch
716
     */
717
    public function fetch($url, $bucket, $key = null)
718
    {
719
720
        $resource = \Qiniu\base64_urlSafeEncode($url);
721
        $to = \Qiniu\entry($bucket, $key);
722
        $path = '/fetch/' . $resource . '/to/' . $to;
723
724
        $ak = $this->auth->getAccessKey();
725 15
        $ioHost = $this->config->getIovipHost($ak, $bucket);
726
727 15
        $url = $ioHost . $path;
728 15
        return $this->post($url, null);
729 15
    }
730 15
731 3
    /**
732 3
     * 从指定URL异步抓取资源,并将该资源存储到指定空间中
733 15
     *
734 15
     * @param string $url 需要抓取的url
735
     * @param string $bucket 所在区域的bucket
736
     * @param string $host 从指定url下载数据时使用的Host
737
     * @param string $key 文件存储的key
738
     * @param string $md5 文件md5
739
     * @param string $etag 文件etag
740
     * @param string $callbackurl 回调URL
741
     * @param string $callbackbody 回调Body
742
     * @param string $callbackbodytype 回调Body内容类型,默认为"application/x-www-form-urlencoded"
743
     * @param string $callbackhost 回调时使用的Host
744
     * @param int $file_type 存储文件类型 0:标准存储(默认),1:低频存储,2:归档存储
745
     * @param bool $ignore_same_key 如果空间中已经存在同名文件则放弃本次抓取
746
     * @return array
747
     * @link  https://developer.qiniu.com/kodo/api/4097/asynch-fetch
748 3
     */
749
    public function asynchFetch(
750 3
        $url,
751 3
        $bucket,
752 3
        $host = null,
753 3
        $key = null,
754
        $md5 = null,
755
        $etag = null,
756 3
        $callbackurl = null,
757 3
        $callbackbody = null,
758
        $callbackbodytype = 'application/x-www-form-urlencoded',
759
        $callbackhost = null,
760
        $file_type = 0,
761
        $ignore_same_key = false
762
    ) {
763
        $path = '/sisyphus/fetch';
764
765
        $params = array('url' => $url, 'bucket' => $bucket);
766
        \Qiniu\setWithoutEmpty($params, 'host', $host);
767
        \Qiniu\setWithoutEmpty($params, 'key', $key);
768
        \Qiniu\setWithoutEmpty($params, 'md5', $md5);
769
        \Qiniu\setWithoutEmpty($params, 'etag', $etag);
770 3
        \Qiniu\setWithoutEmpty($params, 'callbackurl', $callbackurl);
771
        \Qiniu\setWithoutEmpty($params, 'callbackbody', $callbackbody);
772 3
        \Qiniu\setWithoutEmpty($params, 'callbackbodytype', $callbackbodytype);
773 3
        \Qiniu\setWithoutEmpty($params, 'callbackhost', $callbackhost);
774 3
        \Qiniu\setWithoutEmpty($params, 'file_type', $file_type);
775 3
        \Qiniu\setWithoutEmpty($params, 'ignore_same_key', $ignore_same_key);
776 3
        $data = json_encode($params);
777
778
        $ak = $this->auth->getAccessKey();
779
        $apiHost = $this->config->getApiHost($ak, $bucket);
780
        $url = $apiHost . $path;
781
782
        return $this->postV2($url, $data);
783
    }
784
785
786
    /**
787
     * 查询异步第三方资源抓取任务状态
788
     *
789
     * @param string $zone
790
     * @param string $id
791
     * @return array
792
     * @link  https://developer.qiniu.com/kodo/api/4097/asynch-fetch
793
     */
794
    public function asynchFetchStatus($zone, $id)
795
    {
796
        $scheme = "http://";
797
798
        if ($this->config->useHTTPS === true) {
799
            $scheme = "https://";
800
        }
801
802
        $url = $scheme . "api-" . $zone . ".qiniu.com/sisyphus/fetch?id=" . $id;
803
804
        $response = $this->getV2($url);
805
806
        if (!$response->ok()) {
807
            print("statusCode: " . $response->statusCode);
808
            return array(null, new Error($url, $response));
809
        }
810
        return array($response->json(), null);
811
    }
812
813
814
    /**
815
     * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
816
     *
817
     * @param string $bucket 待获取资源所在的空间
818
     * @param string $key 代获取资源文件名
819
     *
820
     * @return array
821
     * @link  https://developer.qiniu.com/kodo/api/1293/prefetch
822
     */
823
    public function prefetch($bucket, $key)
824
    {
825
        $resource = \Qiniu\entry($bucket, $key);
826
        $path = '/prefetch/' . $resource;
827
828
        $ak = $this->auth->getAccessKey();
829
        $ioHost = $this->config->getIovipHost($ak, $bucket);
830
831
        $url = $ioHost . $path;
832
        return $this->post($url, null);
833
    }
834
835
    /**
836
     * 在单次请求中进行多个资源管理操作
837
     *
838 3
     * @param array $operations 资源管理操作数组
839
     *
840
     * @return array 每个资源的处理情况,结果类似:
841 3
     *              [
842 3
     *                   { "code" => <HttpCode int>, "data" => <Data> },
843 3
     *                   { "code" => <HttpCode int> },
844
     *                   { "code" => <HttpCode int> },
845 3
     *                   { "code" => <HttpCode int> },
846 3
     *                   { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
847
     *                   ...
848 3
     *               ]
849 3
     * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
850
     */
851
    public function batch($operations)
852
    {
853
        $params = 'op=' . implode('&op=', $operations);
854
        return $this->rsPost('/batch', $params);
855
    }
856
857
    /**
858
     * 设置文件的生命周期
859
     *
860
     * @param string $bucket 设置文件生命周期文件所在的空间
861 3
     * @param string $key 设置文件生命周期文件的文件名
862
     * @param int $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
863 3
     *
864 3
     * @return array
865
     * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
866 3
     */
867 3
    public function deleteAfterDays($bucket, $key, $days)
868
    {
869 3
        $entry = \Qiniu\entry($bucket, $key);
870 3
        $path = "/deleteAfterDays/$entry/$days";
871 3
        return $this->rsPost($path);
872
    }
873
874
    private function getRsfHost()
875
    {
876
        $scheme = "http://";
877
        if ($this->config->useHTTPS === true) {
878
            $scheme = "https://";
879
        }
880
        return $scheme . Config::RSF_HOST;
881
    }
882
883
    private function getRsHost()
884
    {
885
        $scheme = "http://";
886
        if ($this->config->useHTTPS === true) {
887
            $scheme = "https://";
888
        }
889
        return $scheme . Config::RS_HOST;
890 12
    }
891
892 12
    private function getApiHost()
893 12
    {
894
        $scheme = "http://";
895
        if ($this->config->useHTTPS === true) {
896
            $scheme = "https://";
897
        }
898
        return $scheme . Config::API_HOST;
899
    }
900
901
    private function getUcHost()
902
    {
903
        $scheme = "http://";
904
        if ($this->config->useHTTPS === true) {
905
            $scheme = "https://";
906 3
        }
907
        return $scheme . Config::UC_HOST;
908 3
    }
909 3
910 3
    private function rsPost($path, $body = null)
911 3
    {
912
        $url = $this->getRsHost() . $path;
913
        return $this->post($url, $body);
914 3
    }
915
916 3
    private function apiPost($path, $body = null)
917 3
    {
918
        $url = $this->getApiHost() . $path;
919
        return $this->post($url, $body);
920 3
    }
921
922
    private function ucPost($path, $body = null)
923 33
    {
924
        $url = $this->getUcHost() . $path;
925 33
        return $this->post($url, $body);
926 33
    }
927
928
    private function ucGet($path)
929 33
    {
930
        $url = $this->getUcHost() . $path;
931
        return $this->get($url);
932
    }
933
934
    private function apiGet($path)
935
    {
936
        $url = $this->getApiHost() . $path;
937
        return $this->get($url);
938
    }
939
940
    private function rsGet($path)
941
    {
942
        $url = $this->getRsHost() . $path;
943
        return $this->get($url);
944
    }
945
946
    private function get($url)
947
    {
948
        $headers = $this->auth->authorization($url);
949
        $ret = Client::get($url, $headers);
950 27
        if (!$ret->ok()) {
951
            return array(null, new Error($url, $ret));
952 27
        }
953 27
        return array($ret->json(), null);
954
    }
955
956
    private function getV2($url)
957
    {
958
        $headers = $this->auth->authorizationV2($url, 'GET');
959
        return Client::get($url, $headers);
960
    }
961
962
    private function post($url, $body)
963
    {
964
        $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
965
        $ret = Client::post($url, $body, $headers);
966
        if (!$ret->ok()) {
967
            return array(null, new Error($url, $ret));
968
        }
969
        $r = ($ret->body === null) ? array() : $ret->json();
970
        return array($r, null);
971
    }
972
973
    private function postV2($url, $body)
974
    {
975
        $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/json');
976
        $headers["Content-Type"] = 'application/json';
977
        $ret = Client::post($url, $body, $headers);
978
        if (!$ret->ok()) {
979
            return array(null, new Error($url, $ret));
980 9
        }
981
        $r = ($ret->body === null) ? array() : $ret->json();
982 9
        return array($r, null);
983 9
    }
984
985
    public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
986 12
    {
987
        return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
988 12
    }
989 12
990 12
991 6
    public static function buildBatchRename($bucket, $key_pairs, $force)
992
    {
993 12
        return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
994
    }
995
996 33
997
    public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
998 33
    {
999 33
        return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
1000 33
    }
1001 9
1002
1003 30
    public static function buildBatchDelete($bucket, $keys)
1004 30
    {
1005
        return self::oneKeyBatch('/delete', $bucket, $keys);
1006
    }
1007
1008
1009
    public static function buildBatchStat($bucket, $keys)
1010
    {
1011
        return self::oneKeyBatch('/stat', $bucket, $keys);
1012
    }
1013
1014
    public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
1015
    {
1016
        $data = array();
1017
        foreach ($key_day_pairs as $key => $day) {
1018
            array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
1019
        }
1020
        return $data;
1021
    }
1022
1023
    public static function buildBatchChangeMime($bucket, $key_mime_pairs)
1024
    {
1025 3
        $data = array();
1026
        foreach ($key_mime_pairs as $key => $mime) {
1027 3
            array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
1028
        }
1029
        return $data;
1030
    }
1031 3
1032
    public static function buildBatchChangeType($bucket, $key_type_pairs)
1033 3
    {
1034
        $data = array();
1035
        foreach ($key_type_pairs as $key => $type) {
1036
            array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
1037 6
        }
1038
        return $data;
1039 6
    }
1040
1041
    private static function oneKeyBatch($operation, $bucket, $keys)
1042
    {
1043 3
        $data = array();
1044
        foreach ($keys as $key) {
1045 3
            array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
1046
        }
1047
        return $data;
1048
    }
1049 3
1050
    private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
1051 3
    {
1052
        if ($target_bucket === null) {
1053
            $target_bucket = $source_bucket;
1054
        }
1055
        $data = array();
1056
        $forceOp = "false";
1057
        if ($force) {
1058
            $forceOp = "true";
1059
        }
1060
        foreach ($key_pairs as $from_key => $to_key) {
1061
            $from = \Qiniu\entry($source_bucket, $from_key);
1062
            $to = \Qiniu\entry($target_bucket, $to_key);
1063
            array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
1064
        }
1065
        return $data;
1066
    }
1067
}
1068