Issues (7)

src/Upyun/Upyun.php (1 issue)

1
<?php
2
/**
3
 * 又拍云 PHP-SDK
4
 */
5
namespace Upyun;
6
7
use Upyun\Api\Rest;
8
9
use GuzzleHttp\Client;
10
use GuzzleHttp\Psr7;
11
use GuzzleHttp;
12
use GuzzleHttp\Psr7\Utils;
13
14
/**
15
 * Class Upyun
16
 *
17
 * 又拍云云存储、云处理接口
18
 *
19
 * Upyun 类实现了又拍云云存储和云处理的所有接口,通过该类可以实现文件上传、下载;图片视频等多媒体资源云处理。
20
 * 本文档中,提到的"服务"是指又拍云文件加速回又拍云源类型的服务(即原先的存储类空间)。
21
 *
22
 * @package Upyun
23
 */
24
class Upyun
25
{
26
27
    /**
28
     * @var Config: 服务配置
29
     */
30
    protected $config;
31
32
    // 异步云处理任务类型
33
    /**
34
     * @var string 异步音视频处理
35
     */
36
    public static $PROCESS_TYPE_MEDIA = 'media';
37
    /**
38
     * @var string 文件压缩
39
     */
40
    public static $PROCESS_TYPE_ZIP = 'zip-file';
41
    /**
42
     * @var string 解压缩
43
     */
44
    public static $PROCESS_TYPE_UNZIP = 'unzip-file';
45
    /**
46
     * @var string 文件拉取
47
     */
48
    public static $PROCESS_TYPE_SYNC_FILE = 'sync-remote-file-to-upyun';
49
    /**
50
     * @var string 文档转换
51
     */
52
    public static $PROCESS_TYPE_CONVERT = 'document-type-convert';
53
    /**
54
     * @var string 异步图片拼接
55
     */
56
    public static $PROCESS_TYPE_STITCH = 'picture-stitch';
57
58
    /**
59
     * Upyun constructor.
60
     *
61
     * @param Config $config 服务配置
62
     */
63 2
    public function __construct(Config $config)
64
    {
65 2
        $this->setConfig($config);
66
    }
67
68
    /**
69
     * 配置服务信息
70
     *
71
     * 当需要操作的新的服务时,使用该方法传入新的服务配置即可
72
     *
73
     * @param Config $config 服务配置
74
     *
75
     * @return $this
76
     */
77 2
    public function setConfig(Config $config)
78
    {
79 2
        $this->config = $config;
80 2
        return $this;
81
    }
82
83
    /**
84
     * 上传一个文件到又拍云存储
85
     *
86
     * 上传的文件格式支持文件流或者字符串方式上传。除简单的文件上传外,针对多媒体资源(图片、音视频),还可以设置同步/异步预处理多媒体资源,例如:图片的裁剪缩放,音视频的转码截图等等众多又拍云强大的云处理功能
87
     *
88
     * @param string $path 被上传的文件在又拍云存储服务中保存的路径
89
     * @param string|resource $content 被上传的文件内容(字符串),或者打开该文件获得的文件句柄(文件流)。当上传本地大文件时,推荐使用文件流的方式上传
90
     * @param array $params 上传文件时,附加的自定义参数。支持 Content-MD5 Content-Type Content-Secret 等,详见 [上传参数](http://docs.upyun.com/api/rest_api/#_2),例如:
91
     * - 设置文件[保护秘钥](http://docs.upyun.com/api/rest_api/#Content-Secret) `write($path, $content, array('Content-Secret' => 'my-secret'))`;
92
     * - 添加[文件元信息](http://docs.upyun.com/api/rest_api/#metadata) `write($path, $content, array('X-Upyun-Meta-Foo' =>
93
     * 'bar'))`
94
     * - [图片同步预处理](http://docs.upyun.com/cloud/image/#_5) `write($path, $content, array('x-gmkerl-thumb' => '/format/png'))`
95
     * @param bool $withAsyncProcess  默认为 `false`,当上传图片或者音视频资源时,可以设置该参数为 `true`,开启图片音视频的[异步处理功能](http://docs.upyun.com/api/form_api/#_6) ,例如:
96
     *```
97
     * // 以下参数会将新上传的图片,再异步生成另一份 png 格式的图片,原图不受影响
98
     * write($path, $content, array(
99
     *    'apps' => array(
100
     *        array(
101
     *            'name' => 'thumb',         //异步图片处理任务
102
     *            'x-gmkerl-thumb' => '/format/png', // 格式化图片为 png 格式
103
     *            'save_as': '/iamge/png/new.png',   // 处理成功后的图片保存路径
104
     *            'notify_url': 'http://your.notify.url'  // 异步任务完成后的回调地址
105
     *        )
106
     *    )
107
     * ), true);
108
     *```
109
     *
110
     *
111
     *
112
     * @return array|bool 若文件是图片则返回图片基本信息,如:`array('x-upyun-width' => 123, 'x-upyun-height' => 50, 'x-upyun-frames'
113
     * => 1, 'x-upyun-file-type' => 'JPEG')`,否则返回空数组。当使用异步预处理功能时,返回结果为布尔值,成功为 `true`。
114
     *
115
     * @throws \Exception 上传失败时,抛出异常
116
     */
117 15
    public function write($path, $content, $params = array(), $withAsyncProcess = false)
118
    {
119 15
        if (!$content) {
120
            throw new \Exception('write content can not be empty.');
121
        }
122
123 15
        $upload = new Uploader($this->config);
124 15
        $response = $upload->upload($path, $content, $params, $withAsyncProcess);
125 14
        if ($withAsyncProcess) {
126 1
            return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response also could return the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type array|boolean.
Loading history...
127
        }
128 13
        return Util::getHeaderParams($response->getHeaders());
129
    }
130
131
    /**
132
     * 读取云存储文件/目录内容
133
     *
134
     * @param string $path 又拍云存储中的文件或者目录路径
135
     * @param resource $saveHandler 文件内容写入本地文件流。例如 `$saveHandler = fopen('/local/file', 'w')
136
     * `。当设置该参数时,将以文件流的方式,直接将又拍云中的文件写入本地的文件流,或其他可以写入的流
137
     * @param array $params  可选参数,读取目录内容时,需要设置三个参数: `X-List-Iter` 分页开始位置(第一页不需要设置),`X-List-Limit` 获取的文件数量(默认 100,最大
138
     * 10000),`X-List-Order` 结果以时间正序或者倒序
139
     *
140
     * @return mixed $return 当读取文件且没有设置 `$saveHandler` 参数时,返回一个字符串类型,表示文件内容;设置了 `$saveHandler` 参数时,返回布尔值
141
     * `true`。当读取目录时,返回一个数组,表示目录下的文件列表。目录下文件内容过多时,需要通过判断返回数组中的 `is_end` 属性,进行分页读取内容
142
     *
143
     * @throws \Exception
144
     */
145 3
    public function read($path, $saveHandler = null, $params = array())
146
    {
147 3
        $req = new Rest($this->config);
148 3
        $response = $req->request('GET', $path)
149 3
            ->withHeaders($params)
150 3
            ->send();
151
152
153 2
        $params = Util::getHeaderParams($response->getHeaders());
154
155
156 2
        if (! isset($params['x-upyun-list-iter'])) {
157 1
            if (is_resource($saveHandler)) {
158 1
                Utils::copyToStream($response->getBody(), Utils::streamFor($saveHandler));
159 1
                return true;
160
            } else {
161 1
                return $response->getBody()->getContents();
162
            }
163
        } else {
164 1
            $files = Util::parseDir($response->getBody()->getContents());
165 1
            return array('files' => $files, 'is_end' => $params['x-upyun-list-iter'] === 'g2gCZAAEbmV4dGQAA2VvZg', 'iter' => $params['x-upyun-list-iter']);
166
        }
167
    }
168
169
    /**
170
     * 判断文件是否存在于又拍云存储
171
     *
172
     * 注意: 对刚删除的文件, 立即调用该方法可能会返回 true, 因为服务端执行删除操作后可能会有很短暂的延迟.
173
     *
174
     * @param string $path 云存储的文件路径
175
     *
176
     * @return bool 存在时返回 `true`,否则返回 `false`
177
     * @throws \Exception
178
     */
179 4
    public function has($path)
180
    {
181 4
        $req = new Rest($this->config);
182
        try {
183 4
            $req->request('HEAD', $path)
184 4
                            ->send();
185 2
        } catch (GuzzleHttp\Exception\BadResponseException $e) {
186 2
            $statusCode = $e->getResponse()->getStatusCode();
187 2
            if ($statusCode === 404) {
188 2
                return false;
189
            } else {
190
                throw $e;
191
            }
192
        }
193
194 4
        return true;
195
    }
196
197
    /**
198
     * 获取云存储文件/目录的基本信息
199
     *
200
     * @param string $path 云存储的文件路径
201
     * @param array $otherHeaders 设置了后,方法将返回其他 http header 中的信息,默认为空
202
     *
203
     * @return array 返回一个数组,默认包含以下 key
204
     * - `x-upyun-file-type` 当 $path 是目录时,值为 *folder*,当 $path 是文件时,值为 *file*,
205
     * - `x-upyun-file-size` 文件大小
206
     * - `x-upyun-file-date` 文件的创建时间
207
     */
208 2
    public function info($path, $otherHeaders = array())
209
    {
210 2
        $req = new Rest($this->config);
211 2
        $response = $req->request('HEAD', $path)
212 2
                        ->send();
213 2
        return Util::getHeaderParams($response->getHeaders(), $otherHeaders);
214
    }
215
216
    /**
217
     * 获取文件的文档类型
218
     * @param string $path 云存储文件路径
219
     * @return string 文档类型,e.g: `appcation/json`,获取失败返回空字符串
220
     */
221 1
    public function getMimetype($path)
222
    {
223 1
        $params = $this->info($path, array('content-type'));
224 1
        if (isset($params['content-type'])) {
225 1
            return explode(';', $params['content-type'])[0];
226
        }
227
        return '';
228
    }
229
230
    /**
231
     * 删除文件或者目录
232
     *
233
     * @param string $path 文件或目录在又拍云存储的路径
234
     * @param bool $async 是否异步删除,默认为 false,表示同步删除。当需要批量删除大量文件时,必须选择异步删除
235
     *
236
     * @return bool 删除成功返回 true,否则 false
237
     * @throws \Exception 删除不存在的文件将会抛出异常
238
     */
239 4
    public function delete($path, $async = false)
240
    {
241 4
        $req = new Rest($this->config);
242 4
        $req->request('DELETE', $path);
243 4
        if ($async) {
244
            $req->withHeader('x-upyun-async', 'true');
245
        }
246 4
        $res = $req->send();
247 3
        return $res->getStatusCode() === 200;
248
    }
249
250
    /**
251
     * 创建目录
252
     *
253
     * @param string $path 需要在又拍云存储创建的目录路径
254
     *
255
     * @return bool 创建成功返回 true,否则返回 false
256
     * @throws \Exception
257
     */
258 2
    public function createDir($path)
259
    {
260 2
        $path = rtrim($path, '/') . '/';
261 2
        $req = new Rest($this->config);
262 2
        $res = $req->request('POST', $path)
263 2
            ->withHeader('folder', 'true')
264 2
            ->send();
265 2
        return $res->getStatusCode() === 200;
266
    }
267
268
    /**
269
     * 删除文件或者目录
270
     *
271
     * @param string $path 需要被删除的云存储文件或目录路径
272
     *
273
     * @return bool 成功返回 true,否则 false
274
     * @throws \Exception
275
     */
276 1
    public function deleteDir($path)
277
    {
278 1
        return $this->delete($path);
279
    }
280
281
    /**
282
     * 获取目录下存储使用量
283
     *
284
     * @param string $path 云存储目录路径,默认为根目录,表示整个云存储服务使用的空间大小
285
     * @return string 存储使用量,单位字节
286
     * @throws \Exception
287
     */
288 1
    public function usage($path = '/')
289
    {
290 1
        $path = rtrim($path, '/') . '/';
291 1
        $req = new Rest($this->config);
292 1
        $response = $req->request('GET', $path . '?usage')
293 1
            ->send();
294
295 1
        return $response->getBody()->getContents();
296
    }
297
298
    /**
299
     * 复制文件。只能操作文件,不能操作文件夹。
300
     *
301
     * @param string $source 源文件地址
302
     * @param string $target 目标文件地址
303
     * @return bool 复制成功返回 true,否则 false
304
     * @throws \Exception
305
     */
306 1
    public function copy($source, $target)
307
    {
308 1
        $source = '/' . $this->config->serviceName . '/' . ltrim($source, '/');
309 1
        $req = new Rest($this->config);
310 1
        $response = $req->request('PUT', $target)
311 1
            ->withHeader('X-Upyun-Copy-Source', $source)
312 1
            ->send();
313 1
        return Util::isSuccess($response->getStatusCode());
314
    }
315
316
    /**
317
     * 移动文件。可以进行文件重命名、文件移动,只能操作文件,不能操作文件夹。
318
     *
319
     * @param string $source 源文件地址
320
     * @param string $target 目标文件地址
321
     * @return bool 移动成功返回 true,否则 false
322
     * @throws \Exception
323
     */
324 1
    public function move($source, $target)
325
    {
326 1
        $source = '/' . $this->config->serviceName . '/' . ltrim($source, '/');
327 1
        $req = new Rest($this->config);
328 1
        $response = $req->request('PUT', $target)
329 1
            ->withHeader('X-Upyun-Move-Source', $source)
330 1
            ->send();
331 1
        return Util::isSuccess($response->getStatusCode());
332
    }
333
334
    /**
335
     * 刷新缓存
336
     *
337
     * @param array|string $urls 需要刷新的文件 url 列表
338
     *
339
     * @return array 刷新失败的 url 列表,若全部刷新成功则为空数组
340
     */
341 1
    public function purge($urls)
342
    {
343 1
        $urlString = $urls;
344 1
        if (is_array($urls)) {
345
            $urlString = implode("\n", $urls);
346
        }
347
348 1
        $client = new Client([
349 1
            'timeout' => $this->config->timeout
350 1
        ]);
351 1
        $response = $client->request('POST', Config::ED_PURGE, [
352 1
            'headers' =>  Signature::getPurgeSignHeader($this->config, $urlString),
353 1
            'form_params' => ['purge' => $urlString]
354 1
        ]);
355 1
        $result = json_decode($response->getBody()->getContents(), true);
356 1
        return $result['invalid_domain_of_url'];
357
    }
358
359
    /**
360
     * 异步云处理
361
     *
362
     * 该方法是基于[又拍云云处理](http://docs.upyun.com/cloud/) 服务实现,可以实现音视频的转码、切片、剪辑;文件的压缩解压缩;文件拉取功能
363
     *
364
     * 注意:
365
     * - 所有需要调用该方法处理的资源,必须已经上传到云存储服务
366
     * - 使用 `process` 之前,必须配置 `config->processNotifyUrl`,否则会提交任务失败
367
     *
368
     * 例如视频转码:
369
     * ```
370
     *  process(array(
371
     *    array(
372
     *        'type' => 'video',  // video 表示视频任务, audio 表示音频任务
373
     *        'avopts' => '/s/240p(4:3)/as/1/r/30', // 处理参数,`s` 表示输出的分辨率,`r` 表示视频帧率,`as` 表示是否自动调整分辨率
374
     *        'save_as' => '/video/240/new.mp4', // 新视频在又拍云存储的保存路径
375
     *    ),
376
     *    ... // 同时还可以添加其他任务
377
     * ), Upyun::$PROCESS_TYPE_MEDIA, $source)
378
     * ```
379
     *
380
     * @param array $tasks 需要处理的任务
381
     * @param string $type 异步云处理任务类型,可选值:
382
     * - `Upyun::$PROCESS_TYPE_MEDIA` 异步音视频处理
383
     * - `Upyun::$PROCESS_TYPE_ZIP` 文件压缩
384
     * - `Upyun::$PROCESS_TYPE_UNZIP` 文件解压
385
     * - `Upyun::$PROCESS_TYPE_SYNC_FILE` 文件拉取
386
     * - `Upyun::$PROCESS_TYPE_STITCH` 图片拼接
387
     * @param string $source 可选参数,处理异步音视频任务时,需要传递该参数,表示需要处理的文件路径
388
     *
389
     * @return array 任务 ID,提交了多少任务,便会返回多少任务 ID,与提交任务的顺序保持一致。可以通过任务 ID 查询处理进度。格式如下:
390
     * ```
391
     * array(
392
     * '35f0148d414a688a275bf915ba7cebb2',
393
     * '98adbaa52b2f63d6d7f327a0ff223348',
394
     * )
395
     * ```
396
     * @throws \Exception
397
     */
398 1
    public function process($tasks, $type, $source = '')
399
    {
400 1
        $video = new Api\Pretreat($this->config);
401
402 1
        $options = array();
403
        switch($type) {
404 1
            case self::$PROCESS_TYPE_MEDIA:
405 1
                $options['accept'] = 'json';
406 1
                $options['source'] = $source;
407 1
                break;
408 1
            case self::$PROCESS_TYPE_ZIP:
409 1
                $options['app_name'] = 'compress';
410 1
                break;
411
            case self::$PROCESS_TYPE_UNZIP:
412
                $options['app_name'] = 'depress';
413
                break;
414
            case self::$PROCESS_TYPE_SYNC_FILE:
415
                $options['app_name'] = 'spiderman';
416
                break;
417
            case self::$PROCESS_TYPE_CONVERT:
418
                $options['app_name'] = 'uconvert';
419
                break;
420
            case self::$PROCESS_TYPE_STITCH:
421
                $options['app_name'] = 'jigsaw';
422
                break;
423
            default:
424
                throw new \Exception('upyun - not support process type.');
425
426
        }
427 1
        return $video->process($tasks, $options);
428
    }
429
430
    /**
431
     * 查询异步云处理任务进度
432
     *
433
     * 根据 `process` 方法返回的任务 ID,通过该访问查询处理进度
434
     *
435
     * @param array $taskIds 任务 ID
436
     *
437
     * @return bool|array 查询失败返回布尔值 `false`,否则返回每个任务的百分比进度信息,格式如下:
438
     * ```
439
     * array(
440
     *     '35f0148d414a688a275bf915ba7cebb2' => 100,  // 100 表示任务完成
441
     *     'c3103189fa906a5354d29bd807e8dc51' => 35,
442
     *     '98adbaa52b2f63d6d7f327a0ff223348' => null, // null 表示任务未开始,或异常
443
     * )
444
     * ```
445
     */
446 1
    public function queryProcessStatus($taskIds)
447
    {
448 1
        $video = new Api\Pretreat($this->config);
449 1
        return $video->query($taskIds, '/status/');
450
    }
451
452
    /**
453
     *  查询异步云处理任务结果
454
     *
455
     * 根据 `process` 方法返回的任务 ID,通过该访问查询处理结果,会包含每个任务详细信息
456
     * @param array $taskIds 任务 ID
457
     *
458
     * @return bool|mixed 查询失败返回 `false`,否则返回每个任务的处理结果,格式如下:
459
     * ```
460
     * array(
461
     *    '9d9c32b63a1034834e77672c6f51f661' => array(
462
     *         'path' => array('/v2.mp4'),
463
     *         'signature' => '4042c1f07f546d28',
464
     *         'status_code' => 200,
465
     *         'service_name' => 'your_storage_service',
466
     *         'description' => 'OK',
467
     *         'task_id' => '9d9c32b63a1034834e77672c6f51f661',
468
     *         'timestamp' => 1472010684
469
     *    )
470
     * )
471
     * ```
472
     */
473 1
    public function queryProcessResult($taskIds)
474
    {
475 1
        $video = new Api\Pretreat($this->config);
476 1
        return $video->query($taskIds, '/result/');
477
    }
478
479
    /**
480
     * 多个 m3u8 文件拼接
481
     * @param array $files  保存在又拍云云存储中的多个 m3u8 文件路径
482
     * @param string $saveAs 拼接生成的新 m3u8 文件保存路径
483
     *
484
     * @return array 见 [m3u8 拼接 - 响应](http://docs.upyun.com/cloud/sync_video/#_3)
485
     */
486
    public function m3u8Concat($files, $saveAs)
487
    {
488
        $p = new Api\SyncVideo($this->config);
489
        return $p->process([
490
            'm3u8s' => $files,
491
            'save_as' => $saveAs,
492
        ], '/m3u8er/concat');
493
    }
494
495
    /**
496
     * 单个 m3u8 文件剪辑
497
     * @param string $file 需要剪辑的又拍云云存储中的 m3u8 文件路径
498
     * @param string $saveAs 剪辑完成后新的 m3u8 文件保存路径
499
     * @param array $slice 需要被保留或删除的片段。
500
     * @param bool $isInclude 默认为 `true` 表示 `$slice` 参数描述的片段被保留,否则表示 `$slice` 参数描述的片段被删除
501
     * @param bool $index 指定 `$slice` 参数的格式,默认为 `false` 表示使用时间范围描述片段,单位秒:`[<开始时间>, <结束时间>]`;`true` 表示使用 `m3u8` 文件的分片序号,从 0 开始,这种方式可以一次对多个片段操作
502
     *
503
     * @return array 见 [m3u8 剪辑 - 响应](http://docs.upyun.com/cloud/sync_video/#_6)
504
     */
505
    public function m3u8Clip($file, $saveAs, $slice = array(), $isInclude = true, $index = false)
506
    {
507
        $p = new Api\SyncVideo($this->config);
508
        $params = [
509
            'm3u8' => $file,
510
            'save_as' => $saveAs,
511
            'index' => $index,
512
        ];
513
        if ($isInclude) {
514
            $params['include'] = $slice;
515
        } else {
516
            $params['exclude'] = $slice;
517
        }
518
        return $p->process($params, '/m3u8er/clip');
519
    }
520
521
    /**
522
     * 获取单个 m3u8 文件描述信息
523
     * @param string $file 又拍云云存储的中的 m3u8 文件路径
524
     *
525
     * @return array 见 [获取 m3u8 信息 - 响应](http://docs.upyun.com/cloud/sync_video/#_6)
526
     */
527
    public function m3u8Meta($file)
528
    {
529
        $p = new Api\SyncVideo($this->config);
530
        return $p->process([
531
            'm3u8' => $file,
532
        ], '/m3u8er/get_meta');
533
    }
534
535
    /**
536
     * 视频截图,可以对 mp4、m3u8 等视频文件进行截图
537
     * @param string $file 需要截图的又拍云云存储中的视频文件路径
538
     * @param string $saveAs 截图保存路径
539
     * @param string $point 截图时间点,`HH:MM:SS` 格式
540
     * @param string $size 截图尺寸 `宽x高` 格式的字符串。默认和视频尺寸一致
541
     * @param string $format 截图保存的格式,默认根据 `$saveAs` 参数的后缀生成,可以指定 `jpg | png | webp` 三种格式
542
     *
543
     * @return array 见 [视频截图 - 响应](http://docs.upyun.com/cloud/sync_video/#m3u8_2)
544
     */
545 1
    public function snapshot($file, $saveAs, $point, $size = '', $format = '')
546
    {
547 1
        $p = new Api\SyncVideo($this->config);
548 1
        $params = [
549 1
            'source' => $file,
550 1
            'save_as' => $saveAs,
551 1
            'point' => $point,
552 1
        ];
553 1
        if ($size) {
554 1
            $params['size'] = $size;
555
        }
556 1
        if ($format) {
557 1
            $params['format'] = $format;
558
        }
559 1
        return $p->process($params, '/snapshot');
560
    }
561
562
    /**
563
     * 获取音视频文件元信息
564
     * @param string $file 又拍云云存储的中的音视频文件路径
565
     *
566
     * @return array 见 [获取音视频文件信息 - 响应](http://docs.upyun.com/cloud/sync_video/#_16)
567
     */
568 1
    public function avMeta($file)
569
    {
570 1
        $p = new Api\SyncVideo($this->config);
571 1
        return $p->process([
572 1
            'source' => $file,
573 1
        ], '/avmeta/get_meta');
574
    }
575
}
576