1
|
|
|
<?php |
2
|
|
|
namespace Upyun; |
3
|
|
|
|
4
|
|
|
class Video { |
5
|
|
|
/** |
6
|
|
|
* @var Config |
7
|
|
|
*/ |
8
|
|
|
protected $config; |
9
|
|
|
|
10
|
|
|
public function __construct(Config $bucketConfig) { |
11
|
|
|
$this->setConfig($bucketConfig); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function setConfig(Config $bucketConfig) { |
15
|
|
|
$this->config = $bucketConfig; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function pretreat($source, $notifyUrl, $tasks) { |
19
|
|
|
$postParams['tasks'] = Util::base64Json($tasks); |
|
|
|
|
20
|
|
|
$postParams['source'] = $source; |
21
|
|
|
$postParams['notify_url'] = $notifyUrl; |
22
|
|
|
$postParams['bucket_name'] = $this->config->bucketName; |
23
|
|
|
$sign = Signature::getSignature( |
24
|
|
|
$this->config, |
25
|
|
|
$postParams, |
26
|
|
|
Signature::SIGN_VIDEO |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
$response = Request::post( |
30
|
|
|
sprintf('http://%s/%s/', Config::ED_VIDEO, 'pretreatment'), |
31
|
|
|
array('Authorization' => "UpYun {$this->config->operatorName}:$sign"), |
32
|
|
|
$postParams |
33
|
|
|
); |
34
|
|
|
|
35
|
|
View Code Duplication |
if($response->status_code !== 200) { |
|
|
|
|
36
|
|
|
$body = json_decode($response->body, true); |
37
|
|
|
throw new \Exception(sprintf('%s, with x-request-id=%s', $body['msg'], $body['id']), $body['code']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
$taskIds = json_decode($response->body, true); |
42
|
|
|
return $taskIds; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
public function status($taskIds) { |
47
|
|
|
$limit = 20; |
48
|
|
|
if(count($taskIds) <= $limit) { |
|
|
|
|
49
|
|
|
$taskIds = implode(',', $taskIds); |
50
|
|
|
} else { |
51
|
|
|
throw new \Exception('can not query more than ' . $limit . ' tasks at one time!'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$query['task_ids'] = $taskIds; |
|
|
|
|
55
|
|
|
$query['bucket_name'] = $this->config->bucketName; |
56
|
|
|
$sign = Signature::getSignature( |
57
|
|
|
$this->config, |
58
|
|
|
$query, |
59
|
|
|
Signature::SIGN_VIDEO |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$response = Request::get( |
63
|
|
|
sprintf('http://%s/%s/', Config::ED_VIDEO, 'status'), |
64
|
|
|
array('Authorization' => "UpYun {$this->config->operatorName}:$sign"), |
65
|
|
|
$query |
66
|
|
|
); |
67
|
|
|
|
68
|
|
View Code Duplication |
if($response->status_code !== 200) { |
|
|
|
|
69
|
|
|
$body = json_decode($response->body, true); |
70
|
|
|
throw new \Exception(sprintf('%s, with x-request-id=%s', $body['msg'], $body['id']), $body['code']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$status = json_decode($response->body, true); |
74
|
|
|
return $status; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function callbackSignVerify() { |
|
|
|
|
78
|
|
|
$callbackKeys = array( |
79
|
|
|
'bucket_name', |
80
|
|
|
'status_code', |
81
|
|
|
'path', |
82
|
|
|
'description', |
83
|
|
|
'task_id', |
84
|
|
|
'info', |
85
|
|
|
'signature', |
86
|
|
|
); |
87
|
|
|
$callbackParams = array(); |
88
|
|
|
foreach($callbackKeys as $key) { |
|
|
|
|
89
|
|
|
if(isset($_POST[$key])) { |
|
|
|
|
90
|
|
|
$callbackParams[$key] = Util::trim($_POST[$key]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
if(isset($callbackParams['signature'])) { |
|
|
|
|
95
|
|
|
$sign = $callbackParams['signature']; |
96
|
|
|
unset($callbackParams['signature']); |
97
|
|
|
return $sign === Signature::getSignature( |
98
|
|
|
$this->config, |
99
|
|
|
$callbackParams, |
100
|
|
|
Signature::SIGN_VIDEO |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
if(isset($data['non_signature'])) { |
|
|
|
|
105
|
|
|
$sign = $callbackParams['non_signature']; |
106
|
|
|
unset($callbackParams['non_signature']); |
107
|
|
|
return $sign === Signature::getSignature( |
108
|
|
|
$this->config, |
109
|
|
|
$callbackParams, |
110
|
|
|
Signature::SIGN_VIDEO_NO_OPERATOR |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
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:
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 thebar
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.