1
|
|
|
<?php |
2
|
|
|
namespace Upyun\Api; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Psr7; |
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Upyun\Config; |
7
|
|
|
use Upyun\Signature; |
8
|
|
|
use Upyun\Util; |
9
|
|
|
|
10
|
|
|
class Pretreat |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Config |
15
|
|
|
*/ |
16
|
|
|
protected $config; |
17
|
|
|
|
18
|
3 |
|
public function __construct(Config $config) |
19
|
|
|
{ |
20
|
3 |
|
if (!$config->processNotifyUrl) { |
21
|
|
|
throw new \Exception("should config prosessNotifyUrl first."); |
22
|
|
|
} |
23
|
3 |
|
$this->config = $config; |
24
|
3 |
|
} |
25
|
|
|
|
26
|
1 |
|
public function process($source, $tasks) |
27
|
|
|
{ |
28
|
1 |
|
$encodedTasks = Util::base64Json($tasks); |
29
|
|
|
|
30
|
1 |
|
$client = new Client([ |
31
|
1 |
|
'timeout' => $this->config->timeout, |
32
|
1 |
|
]); |
33
|
|
|
|
34
|
|
|
$params = array( |
35
|
1 |
|
'service' => $this->config->bucketName, |
36
|
1 |
|
'notify_url' => $this->config->processNotifyUrl, |
37
|
1 |
|
'source' => $source, |
38
|
1 |
|
'tasks' => $encodedTasks, |
39
|
|
|
'accept' => 'json' |
40
|
1 |
|
); |
41
|
|
|
|
42
|
1 |
|
$path = '/pretreatment/'; |
43
|
1 |
|
$method = 'POST'; |
44
|
1 |
|
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path); |
45
|
|
|
|
46
|
1 |
|
$url = $this->config->getPretreatEndPoint() . $path; |
47
|
1 |
|
$response = $client->request($method, $url, [ |
48
|
1 |
|
'headers' => $signedHeaders, |
49
|
|
|
'form_params' => $params |
50
|
1 |
|
]); |
51
|
|
|
|
52
|
1 |
|
$body = $response->getBody()->getContents(); |
53
|
1 |
|
return json_decode($body, true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
2 |
|
public function query($taskIds, $path) |
58
|
|
|
{ |
59
|
2 |
|
$client = new Client([ |
60
|
2 |
|
'timeout' => $this->config->timeout, |
61
|
2 |
|
]); |
62
|
|
|
|
63
|
|
|
$params = array( |
64
|
2 |
|
'service' => $this->config->bucketName, |
65
|
2 |
|
'task_ids' => implode(',', $taskIds) |
66
|
2 |
|
); |
67
|
2 |
|
$path = $path . '?' . http_build_query($params); |
68
|
|
|
|
69
|
2 |
|
$method = 'GET'; |
70
|
2 |
|
$url = $this->config->getPretreatEndPoint() . $path; |
71
|
2 |
|
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path); |
72
|
2 |
|
$response = $client->request($method, $url, [ |
73
|
|
|
'headers' => $signedHeaders |
74
|
2 |
|
]); |
75
|
|
|
|
76
|
2 |
|
if ($response->getStatusCode() === 200) { |
77
|
2 |
|
$body = $response->getBody()->getContents(); |
78
|
2 |
|
$result = json_decode($body, true); |
79
|
2 |
|
if (is_array($result)) { |
80
|
2 |
|
return $result['tasks']; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|