Skip to content

Commit 73297d4

Browse files
committed
fix: 文件拉取等异步任务处理失败
1 parent bfccb98 commit 73297d4

File tree

4 files changed

+92
-18
lines changed

4 files changed

+92
-18
lines changed

doc.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ Upyun::purge( array|string $urls )
381381
异步云处理
382382

383383
```php
384-
Upyun::process( string $source, array $tasks )
384+
Upyun::process( array $tasks, string $type, string $source )
385385
```
386386

387387
该方法是基于[又拍云云处理](http://docs.upyun.com/cloud/) 服务实现,可以实现音视频的转码、切片、剪辑;文件的压缩解压缩;文件拉取功能
@@ -392,27 +392,35 @@ Upyun::process( string $source, array $tasks )
392392

393393
例如视频转码:
394394
```
395-
process($source, array(
395+
process(array(
396396
array(
397397
'type' => 'video', // video 表示视频任务, audio 表示音频任务
398398
'avopts' => '/s/240p(4:3)/as/1/r/30', // 处理参数,`s` 表示输出的分辨率,`r` 表示视频帧率,`as` 表示是否自动调整分辨率
399399
'save_as' => '/video/240/new.mp4', // 新视频在又拍云存储的保存路径
400400
),
401401
... // 同时还可以添加其他任务
402-
))
402+
), Upyun::$PROCESS_TYPE_MEDIA, $source)
403403
```
404404
注意,被处理的资源需要已经上传到又拍云云存储
405405

406406

407407
**参数列表:**
408408

409409

410-
- **string** `$source`
411-
需要预处理的图片、音视频资源在又拍云存储的路径
412-
413410
- **array** `$tasks`
414411
需要处理的任务
415412

413+
- **string** `$type`
414+
异步云处理任务类型,可选值:
415+
- `Upyun::$PROCESS_TYPE_MEDIA` 异步音视频处理
416+
- `Upyun::$PROCESS_TYPE_ZIP` 文件压缩
417+
- `Upyun::$PROCESS_TYPE_UNZIP` 文件解压
418+
- `Upyun::$PROCESS_TYPE_SYNC_FILE` 文件拉取
419+
- `Upyun::$PROCESS_TYPE_STITCH` 图片拼接
420+
421+
- **string** `$source`
422+
可选参数,处理异步音视频任务时,需要传递该参数,表示需要处理的文件路径
423+
416424

417425
**返回值:**
418426

src/Upyun/Api/Pretreat.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(Config $config)
2323
$this->config = $config;
2424
}
2525

26-
public function process($source, $tasks)
26+
public function process($tasks, $optionalParams = array())
2727
{
2828
$encodedTasks = Util::base64Json($tasks);
2929

@@ -34,11 +34,11 @@ public function process($source, $tasks)
3434
$params = array(
3535
'service' => $this->config->serviceName,
3636
'notify_url' => $this->config->processNotifyUrl,
37-
'source' => $source,
3837
'tasks' => $encodedTasks,
39-
'accept' => 'json'
4038
);
4139

40+
$params = array_merge($params, $optionalParams);
41+
4242
$path = '/pretreatment/';
4343
$method = 'POST';
4444
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path);

src/Upyun/Upyun.php

+66-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ class Upyun
2828
*/
2929
protected $config;
3030

31+
// 异步云处理任务类型
32+
/**
33+
* @var string 异步音视频处理
34+
*/
35+
public static $PROCESS_TYPE_MEDIA = 'media';
36+
/**
37+
* @var string 文件压缩
38+
*/
39+
public static $PROCESS_TYPE_ZIP = 'zip-file';
40+
/**
41+
* @var string 解压缩
42+
*/
43+
public static $PROCESS_TYPE_UNZIP = 'unzip-file';
44+
/**
45+
* @var string 文件拉取
46+
*/
47+
public static $PROCESS_TYPE_SYNC_FILE = 'sync-remote-file-to-upyun';
48+
/**
49+
* @var string 文档转换
50+
*/
51+
public static $PROCESS_TYPE_CONVERT = 'document-type-convert';
52+
/**
53+
* @var string 异步图片拼接
54+
*/
55+
public static $PROCESS_TYPE_STITCH = 'picture-stitch';
56+
3157
/**
3258
* Upyun constructor.
3359
*
@@ -304,31 +330,64 @@ public function purge($urls)
304330
*
305331
* 例如视频转码:
306332
* ```
307-
* process($source, array(
333+
* process(array(
308334
* array(
309335
* 'type' => 'video', // video 表示视频任务, audio 表示音频任务
310336
* 'avopts' => '/s/240p(4:3)/as/1/r/30', // 处理参数,`s` 表示输出的分辨率,`r` 表示视频帧率,`as` 表示是否自动调整分辨率
311337
* 'save_as' => '/video/240/new.mp4', // 新视频在又拍云存储的保存路径
312338
* ),
313339
* ... // 同时还可以添加其他任务
314-
* ))
340+
* ), Upyun::$PROCESS_TYPE_MEDIA, $source)
315341
* ```
316342
*
317-
* @param string $source 需要预处理的图片、音视频资源在又拍云存储的路径
318343
* @param array $tasks 需要处理的任务
344+
* @param string $type 异步云处理任务类型,可选值:
345+
* - `Upyun::$PROCESS_TYPE_MEDIA` 异步音视频处理
346+
* - `Upyun::$PROCESS_TYPE_ZIP` 文件压缩
347+
* - `Upyun::$PROCESS_TYPE_UNZIP` 文件解压
348+
* - `Upyun::$PROCESS_TYPE_SYNC_FILE` 文件拉取
349+
* - `Upyun::$PROCESS_TYPE_STITCH` 图片拼接
350+
* @param string $source 可选参数,处理异步音视频任务时,需要传递该参数,表示需要处理的文件路径
319351
*
320352
* @return array 任务 ID,提交了多少任务,便会返回多少任务 ID,与提交任务的顺序保持一致。可以通过任务 ID 查询处理进度。格式如下:
321353
* ```
322354
* array(
323-
* '35f0148d414a688a275bf915ba7cebb2',
324-
* '98adbaa52b2f63d6d7f327a0ff223348',
355+
* '35f0148d414a688a275bf915ba7cebb2',
356+
* '98adbaa52b2f63d6d7f327a0ff223348',
325357
* )
326358
* ```
359+
* @throws \Exception
327360
*/
328-
public function process($source, $tasks)
361+
public function process($tasks, $type, $source = '')
329362
{
330363
$video = new Api\Pretreat($this->config);
331-
return $video->process($source, $tasks);
364+
365+
$options = array();
366+
switch($type) {
367+
case self::$PROCESS_TYPE_MEDIA:
368+
$options['accept'] = 'json';
369+
$options['source'] = $source;
370+
break;
371+
case self::$PROCESS_TYPE_ZIP:
372+
$options['app_name'] = 'compress';
373+
break;
374+
case self::$PROCESS_TYPE_UNZIP:
375+
$options['app_name'] = 'depress';
376+
break;
377+
case self::$PROCESS_TYPE_SYNC_FILE:
378+
$options['app_name'] = 'spiderman';
379+
break;
380+
case self::$PROCESS_TYPE_SYNC_FILE:
381+
$options['app_name'] = 'uconvert';
382+
break;
383+
case self::$PROCESS_TYPE_STITCH:
384+
$options['app_name'] = 'jigsaw';
385+
break;
386+
default:
387+
throw new \Exception('upyun - not support process type.');
388+
389+
}
390+
return $video->process($tasks, $options);
332391
}
333392

334393
/**

tests/UpyunTest.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,18 @@ public function testProcess()
218218
{
219219
$source = 'php-sdk-sample.mp4';
220220
self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
221-
$result = self::$upyun->process($source, array(
221+
$result = self::$upyun->process(array(
222222
array('type' => 'video', 'avopts' => '/s/240p(4:3)/as/1/r/30', 'return_info' => true, 'save_as' => '/video/result.mp4')
223-
));
223+
), Upyun::$PROCESS_TYPE_MEDIA, $source);
224224
$this->assertTrue(strlen($result[0]) === 32);
225225
self::$taskId = $result[0];
226+
227+
// test zip
228+
$result2 = self::$upyun->process(array(array(
229+
'sources' => ['./php-sdk-sample.mp4'],
230+
'save_as' => '/php-sdk-sample-mp4.zip'
231+
)), Upyun::$PROCESS_TYPE_ZIP);
232+
$this->assertTrue(strlen($result2[0]) === 32);
226233
}
227234

228235
/**

0 commit comments

Comments
 (0)