Completed
Pull Request — master (#293)
by
unknown
21:33
created

BucketManager::buildBatchDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Qiniu\Storage;
3
4
use Qiniu\Auth;
5
use Qiniu\Config;
6
use Qiniu\Zone;
7
use Qiniu\Http\Client;
8
use Qiniu\Http\Error;
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
     * @return string[] 包含所有空间名
34
     */
35 3
    public function buckets($shared = true)
36
    {
37 3
        $includeShared = "false";
38 3
        if ($shared === true) {
39 3
            $includeShared = "true";
40 3
        }
41 3
        return $this->rsGet('/buckets?shared=' . $includeShared);
42
    }
43
44
    /**
45
     * 创建空间
46
     *
47
     * @param $name     创建的空间名
48
     * @param $region    创建的区域,默认华东
49
     *
50
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
51
     */
52
    public function creatBucket($name, $region='z0')
53
    {
54
        $path = '/mkbucketv2/'.$name.'/region/' . $region;
55
        return $this->rsPost($path, null);
56
    }
57
58
    /**
59
     * 删除空间
60
     *
61
     * @param $name     删除的空间名
62
     *
63
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
64
     */
65
    public function deleteBucket($name)
66
    {
67
        $path = '/drop/'.$name;
68
        return $this->rsPost($path, null);
69
    }
70
71
    /**
72
     * 获取指定空间绑定的所有的域名
73
     *
74
     * @return string[] 包含所有空间域名
75
     */
76
    public function domains($bucket)
77
    {
78
        return $this->apiGet('/v6/domain/list?tbl=' . $bucket);
79 3
    }
80
81 3
    /**
82 3
     * 获取指定空间的相关信息
83 3
     *
84 3
     * @return string[] 包含空间信息
85 3
     */
86 3
    public function bucketInfo($bucket){
87 3
        $path = '/v2/bucketInfo?bucket=' . $bucket;
88
        $info = $this->ucPost($path);
89
        return $info;
90
    }
91
92
    /**
93
     * 获取指定zone的空间信息列表
94
     * 在Region 未指定且Global 不为 true 时(包含未指定的情况,下同),返回用户的所有空间。
95
     * 在指定了 region 参数且 global 不为 true 时,只列举非全局空间。
96
     * 在指定了global为 true 时,返回所有全局空间,忽略region 参数
97
     * shared 不指定shared参数或指定shared为rw或false时,返回包含具有读写权限空间,指定shared为rd或true时,返回包含具有读权限空间。
98
     * fs:如果为 true,会返回每个空间当前的文件数和存储量(实时数据)。
99
     *
100
     * @return string[] 包含空间信息
101
     */
102
    public function bucketInfos($region=null, $global='false', $shared='false', $fs='false'){
103
        $path = '/v2/bucketInfos?region=' . $region . '&global=' . $global . '&shared=' . $shared . '&fs=' . $fs;
104
        $info = $this->ucPost($path);
105
        return $info;
106
    }
107 6
108
    /**
109 6
     * 获取空间绑定的域名列表
110 6
     * @return string[] 包含空间绑定的所有域名
111
     */
112
113
    /**
114
     * 列取空间的文件列表
115
     *
116
     * @param $bucket     空间名
117
     * @param $prefix     列举前缀
118
     * @param $marker     列举标识符
119
     * @param $limit      单次列举个数限制
120
     * @param $delimiter  指定目录分隔符
121
     *
122 15
     * @return array    包含文件信息的数组,类似:[
123
     *                                              {
124 15
     *                                                 "hash" => "<Hash string>",
125 15
     *                                                  "key" => "<Key string>",
126 15
     *                                                  "fsize" => "<file size>",
127
     *                                                  "putTime" => "<file modify time>"
128
     *                                              },
129
     *                                              ...
130
     *                                            ]
131
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
132
     */
133
    public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null)
134
    {
135
        $query = array('bucket' => $bucket);
136
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
137
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
138
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
139 3
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
140
        $url = $this->getRsfHost() . '/list?' . http_build_query($query);
141 3
        return $this->get($url);
142
    }
143
144
    /**
145
     * 设置Referer防盗链
146
     *
147
     * @param $bucket     空间名
148
     * @param $mode     0: 表示关闭Referer(使用此选项将会忽略以下参数并将恢复默认值); 1: 表示设置Referer白名单; 2: 表示设置Referer黑名单
149
     * @param $norefer     0: 表示不允许空 Refer 访问; 1: 表示允许空 Refer 访问
150
     * @param $pattern      规则字符串, 当前允许格式分为三种: 一种为空主机头域名, 比如 foo.com; 一种是泛域名, 比如 *.bar.com; 一种是完全通配符, 即一个 *; 多个规则之间用;隔开, 比如: foo.com;*.bar.com;sub.foo.com;*.sub.bar.com
151
     * @param $source_enabled  源站是否支持,默认为0只给CDN配置, 设置为1表示开启源站防盗链
152
     *
153
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
154
     */
155 15
    public function referAntiLeech(){
156
157 15
    }
158 15
159 15
    /**
160 15
     * 增加bucket生命规则
161 3
     *
162 3
     * @param $bucket     空间名
163 15
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
164 15
     * @param $prefix     同一个 bucket 里面前缀不能重复
165
     * @param $delete_after_days      指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除,需大于 to_line_after_days
166
     * @param $to_line_after_days  指定文件上传多少天后转低频存储。指定为0表示不转低频存储,小于0表示上传的文件立即变低频存储
167
     *
168
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
169
     */
170
    public function bucketLifecycleRule($bucket, $name, $prefix, $delete_after_days, $to_line_after_days){
171
        $path = '/rules/add';
172
        if ($bucket) {
173
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
174
        }
175
        if ($name) {
176
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
177
        }
178 3
        if ($prefix) {
179
            $params['prefix'] = $prefix;
180 3
        }
181 3
        if ($delete_after_days) {
182 3
            $params['delete_after_days'] = $delete_after_days;
183 3
        }
184
        if ($to_line_after_days) {
185
            $params['to_line_after_days'] = $to_line_after_days;
186 3
        }
187 3
        $data = http_build_query($params);
188
        $info = $this->ucPost($path, $data);
189
        return $info;
190
    }
191
192
    /**
193
     * 更新bucket生命规则
194
     *
195
     * @param $bucket     空间名
196
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
197
     * @param $prefix     同一个 bucket 里面前缀不能重复
198
     * @param $delete_after_days      指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除,需大于 to_line_after_days
199
     * @param $to_line_after_days  指定文件上传多少天后转低频存储。指定为0表示不转低频存储,小于0表示上传的文件立即变低频存储
200 3
     *
201
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
202 3
     */
203 3
    public function updateBucketLifecycleRule($bucket, $name, $prefix, $delete_after_days, $to_line_after_days){
204 3
        $path = '/rules/update';
205 3
        if ($bucket) {
206 3
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
207
        }
208
        if ($name) {
209
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
210
        }
211
        if ($prefix) {
212
            $params['prefix'] = $prefix;
213
        }
214
        if ($delete_after_days) {
215
            $params['delete_after_days'] = $delete_after_days;
216
        }
217
        if ($to_line_after_days) {
218
            $params['to_line_after_days'] = $to_line_after_days;
219
        }
220
        $data = http_build_query($params);
221
        $info = $this->ucPost($path, $data);
222
        return $info;
223
    }
224
225
    /**
226
     * 获取bucket生命规则
227
     *
228
     * @param $bucket     空间名
229
     * 
230
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
231
     */
232
    public function getBucketLifecycleRules($bucket){
233
        $path = '/rules/get?bucket=' . $bucket;
234
        $info = $this->ucGet($path);
235
        return $info;
236
    }
237
238
    /**
239
     * 删除bucket生命规则
240
     *
241
     * @param $bucket     空间名
242
     * @param $name     规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
243
     * 
244
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
245
     */
246
    public function deleteBucketLifecycleRule($bucket, $name){
247
        $path = '/rules/delete';
248
        if ($bucket) {
249
            $params['bucket'] = $bucket;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
250
        }
251
        if ($name) {
252
            $params['name'] = $name;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
253
        }
254
        $data = http_build_query($params);
255
        $info = $this->ucPost($path, $data);
256
        return $info;
257
    }
258
259
    /**
260
     * 获取资源的元信息,但不返回文件内容
261
     *
262
     * @param $bucket     待获取信息资源所在的空间
263
     * @param $key        待获取资源的文件名
264
     *
265
     * @return array    包含文件信息的数组,类似:
266
     *                                              [
267
     *                                                  "hash" => "<Hash string>",
268 3
     *                                                  "key" => "<Key string>",
269
     *                                                  "fsize" => <file size>,
270
     *                                                  "putTime" => "<file modify time>"
271 3
     *                                                  "fileType" => <file type>
272 3
     *                                              ]
273 3
     *
274
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html
275 3
     */
276 3
    public function stat($bucket, $key)
277
    {
278 3
        $path = '/stat/' . \Qiniu\entry($bucket, $key);
279 3
        return $this->rsGet($path);
280
    }
281
282
    /**
283
     * 删除指定资源
284
     *
285
     * @param $bucket     待删除资源所在的空间
286
     * @param $key        待删除资源的文件名
287
     *
288
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
289
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html
290
     */
291 3
    public function delete($bucket, $key)
292
    {
293 3
        $path = '/delete/' . \Qiniu\entry($bucket, $key);
294 3
        list(, $error) = $this->rsPost($path);
295
        return $error;
296 3
    }
297 3
298
299 3
    /**
300 3
     * 给资源进行重命名,本质为move操作。
301 3
     *
302
     * @param $bucket     待操作资源所在空间
303
     * @param $oldname    待操作资源文件名
304
     * @param $newname    目标资源文件名
305
     *
306
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
307
     */
308
    public function rename($bucket, $oldname, $newname)
309
    {
310
        return $this->move($bucket, $oldname, $bucket, $newname);
311
    }
312
313
    /**
314
     * 给资源进行重命名,本质为move操作。
315
     *
316
     * @param $from_bucket     待操作资源所在空间
317
     * @param $from_key        待操作资源文件名
318
     * @param $to_bucket       目标资源空间名
319
     * @param $to_key          目标资源文件名
320 12
     *
321
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
322 12
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html
323 12
     */
324
    public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
325
    {
326
        $from = \Qiniu\entry($from_bucket, $from_key);
327
        $to = \Qiniu\entry($to_bucket, $to_key);
328
        $path = '/copy/' . $from . '/' . $to;
329
        if ($force === true) {
330
            $path .= '/force/true';
331
        }
332
        list(, $error) = $this->rsPost($path);
333
        return $error;
334
    }
335
336 3
    /**
337
     * 将资源从一个空间到另一个空间
338 3
     *
339 3
     * @param $from_bucket     待操作资源所在空间
340 3
     * @param $from_key        待操作资源文件名
341 3
     * @param $to_bucket       目标资源空间名
342
     * @param $to_key          目标资源文件名
343
     *
344 3
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
345
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/move.html
346 3
     */
347 3
    public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
348
    {
349
        $from = \Qiniu\entry($from_bucket, $from_key);
350 3
        $to = \Qiniu\entry($to_bucket, $to_key);
351
        $path = '/move/' . $from . '/' . $to;
352
        if ($force) {
353 33
            $path .= '/force/true';
354
        }
355 33
        list(, $error) = $this->rsPost($path);
356 33
        return $error;
357
    }
358
359 33
    /**
360
     * 主动修改指定资源的文件元信息
361
     *
362
     * @param $bucket     待操作资源所在空间
363
     * @param $key        待操作资源文件名
364
     * @param $mime       待操作文件目标mimeType
365
     *
366
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
367
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html
368
     */
369
    public function changeMime($bucket, $key, $mime)
370
    {
371 27
        $resource = \Qiniu\entry($bucket, $key);
372
        $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
373 27
        $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
374 27
        list(, $error) = $this->rsPost($path);
375
        return $error;
376
    }
377
378
379
    /**
380
     * 修改指定资源的存储类型
381
     *
382
     * @param $bucket     待操作资源所在空间
383 9
     * @param $key        待操作资源文件名
384
     * @param $fileType       待操作文件目标文件类型
385 9
     *
386 9
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
387
     * @link  https://developer.qiniu.com/kodo/api/3710/modify-the-file-type
388
     */
389 12
    public function changeType($bucket, $key, $fileType)
390
    {
391 12
        $resource = \Qiniu\entry($bucket, $key);
392 12
        $path = '/chtype/' . $resource . '/type/' . $fileType;
393 12
        list(, $error) = $this->rsPost($path);
394 8
        return $error;
395
    }
396 8
397
    /**
398
     * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
399 33
     *
400
     * @param $bucket     待操作资源所在空间
401 33
     * @param $key        待操作资源文件名
402 33
     * @param $status       待操作文件目标文件类型
403 33
     *
404 9
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
405
     * @link  https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
406 30
     */
407 30
    public function changeStatus($bucket, $key, $status)
408
    {
409
        $resource = \Qiniu\entry($bucket, $key);
410 3
        $path = '/chstatus/' . $resource . '/status/' . $status;
411
        list(, $error) = $this->rsPost($path);
412 3
        return $error;
413
    }
414
415
    /**
416 3
     * 从指定URL抓取资源,并将该资源存储到指定空间中
417
     *
418 3
     * @param $url        指定的URL
419
     * @param $bucket     目标资源空间
420
     * @param $key        目标资源文件名
421
     *
422 6
     * @return array    包含已拉取的文件信息。
423
     *                         成功时:  [
424 6
     *                                          [
425
     *                                              "hash" => "<Hash string>",
426
     *                                              "key" => "<Key string>"
427
     *                                          ],
428 3
     *                                          null
429
     *                                  ]
430 3
     *
431
     *                         失败时:  [
432
     *                                          null,
433
     *                                         Qiniu/Http/Error
434 3
     *                                  ]
435
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html
436 3
     */
437
    public function fetch($url, $bucket, $key = null)
438
    {
439
440
        $resource = \Qiniu\base64_urlSafeEncode($url);
441
        $to = \Qiniu\entry($bucket, $key);
442
        $path = '/fetch/' . $resource . '/to/' . $to;
443
444
        $ak = $this->auth->getAccessKey();
445
        $ioHost = $this->config->getIovipHost($ak, $bucket);
446
447
        $url = $ioHost . $path;
448
        return $this->post($url, null);
449
    }
450
451
    /**
452
     * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
453
     *
454
     * @param $bucket     待获取资源所在的空间
455
     * @param $key        代获取资源文件名
456
     *
457
     * @return mixed      成功返回NULL,失败返回对象Qiniu\Http\Error
458
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html
459
     */
460
    public function prefetch($bucket, $key)
461
    {
462
        $resource = \Qiniu\entry($bucket, $key);
463
        $path = '/prefetch/' . $resource;
464
465
        $ak = $this->auth->getAccessKey();
466 6
        $ioHost = $this->config->getIovipHost($ak, $bucket);
467
468 6
        $url = $ioHost . $path;
469 6
        list(, $error) = $this->post($url, null);
470 6
        return $error;
471 6
    }
472 6
473
    /**
474
     * 在单次请求中进行多个资源管理操作
475 9
     *
476
     * @param $operations     资源管理操作数组
477 9
     *
478
     * @return array 每个资源的处理情况,结果类似:
479
     *              [
480 9
     *                   { "code" => <HttpCode int>, "data" => <Data> },
481 9
     *                   { "code" => <HttpCode int> },
482 9
     *                   { "code" => <HttpCode int> },
483 9
     *                   { "code" => <HttpCode int> },
484 9
     *                   { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
485 9
     *                   ...
486 9
     *               ]
487 9
     * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
488 9
     */
489 9
    public function batch($operations)
490 9
    {
491
        $params = 'op=' . implode('&op=', $operations);
492
        return $this->rsPost('/batch', $params);
493
    }
494
495
    /**
496
     * 设置文件的生命周期
497
     *
498
     * @param $bucket 设置文件生命周期文件所在的空间
499
     * @param $key    设置文件生命周期文件的文件名
500
     * @param $days   设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
501
     *
502
     * @return Mixed
503
     * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
504
     */
505
    public function deleteAfterDays($bucket, $key, $days)
506
    {
507
        $entry = \Qiniu\entry($bucket, $key);
508
        $path = "/deleteAfterDays/$entry/$days";
509
        list(, $error) = $this->rsPost($path);
510
        return $error;
511
    }
512
513
    private function getRsfHost()
514
    {
515
        $scheme = "http://";
516
        if ($this->config->useHTTPS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
517
            $scheme = "https://";
518
        }
519
        return $scheme . Config::RSF_HOST;
520
    }
521
522
    private function getRsHost()
523
    {
524
        $scheme = "http://";
525
        if ($this->config->useHTTPS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
526
            $scheme = "https://";
527
        }
528
        return $scheme . Config::RS_HOST;
529
    }
530
531
    private function getApiHost()
532
    {
533
        $scheme = "http://";
534
        if ($this->config->useHTTPS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
535
            $scheme = "https://";
536
        }
537
        return $scheme . Config::API_HOST;
538
    }
539
540
    private function getUcHost()
541
    {
542
        $scheme = "http://";
543
        if ($this->config->useHTTPS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
544
            $scheme = "https://";
545
        }
546
        return $scheme . Config::UC_HOST;
547
    }
548
549
    private function rsPost($path, $body = null)
550
    {
551
        $url = $this->getRsHost() . $path;
552
        return $this->post($url, $body);
553
    }
554
555
    private function apiPost($path, $body = null)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
556
    {
557
        $url = $this->getApiHost() . $path;
558
        return $this->post($url, $body);
559
    }
560
561
    private function ucPost($path, $body = null)
562
    {
563
        $url = $this->getUcHost() . $path;
564
        return $this->post($url, $body);
565
    }
566
567
    private function ucGet($path)
568
    {
569
        $url = $this->getUcHost() . $path;
570
        return $this->get($url);
571
    }
572
573
    private function apiGet($path)
574
    {
575
        $url = $this->getApiHost() . $path;
576
        return $this->get($url);
577
    }
578
579
    private function rsGet($path)
580
    {
581
        $url = $this->getRsHost() . $path;
582
        return $this->get($url);
583
    }
584
585
    private function get($url)
586
    {
587
        $headers = $this->auth->authorization($url);
588
        $ret = Client::get($url, $headers);
589
        if (!$ret->ok()) {
590
            return array(null, new Error($url, $ret));
591
        }
592
        return array($ret->json(), null);
593
    }
594
595
    private function post($url, $body)
596
    {
597
        $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
598
        $ret = Client::post($url, $body, $headers);
599
        if (!$ret->ok()) {
600
            return array(null, new Error($url, $ret));
601
        }
602
        $r = ($ret->body === null) ? array() : $ret->json();
603
        return array($r, null);
604
    }
605
606
    public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
607
    {
608
        return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
609
    }
610
611
612
    public static function buildBatchRename($bucket, $key_pairs, $force)
613
    {
614
        return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
615
    }
616
617
618
    public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
619
    {
620
        return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
621
    }
622
623
624
    public static function buildBatchDelete($bucket, $keys)
625
    {
626
        return self::oneKeyBatch('/delete', $bucket, $keys);
627
    }
628
629
630
    public static function buildBatchStat($bucket, $keys)
631
    {
632
        return self::oneKeyBatch('/stat', $bucket, $keys);
633
    }
634
635
    public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
636
    {
637
        $data = array();
638
        foreach ($key_day_pairs as $key => $day) {
639
            array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
640
        }
641
        return $data;
642
    }
643
644
    public static function buildBatchChangeMime($bucket, $key_mime_pairs)
645
    {
646
        $data = array();
647
        foreach ($key_mime_pairs as $key => $mime) {
648
            array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
649
        }
650
        return $data;
651
    }
652
653
    public static function buildBatchChangeType($bucket, $key_type_pairs)
654
    {
655
        $data = array();
656
        foreach ($key_type_pairs as $key => $type) {
657
            array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
658
        }
659
        return $data;
660
    }
661
662
    private static function oneKeyBatch($operation, $bucket, $keys)
663
    {
664
        $data = array();
665
        foreach ($keys as $key) {
666
            array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
667
        }
668
        return $data;
669
    }
670
671
    private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
672
    {
673
        if ($target_bucket === null) {
674
            $target_bucket = $source_bucket;
675
        }
676
        $data = array();
677
        $forceOp = "false";
678
        if ($force) {
679
            $forceOp = "true";
680
        }
681
        foreach ($key_pairs as $from_key => $to_key) {
682
            $from = \Qiniu\entry($source_bucket, $from_key);
683
            $to = \Qiniu\entry($target_bucket, $to_key);
684
            array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
685
        }
686
        return $data;
687
    }
688
}
689