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 Multi { |
11
|
|
|
/** |
12
|
|
|
* @var Config |
13
|
|
|
*/ |
14
|
|
|
protected $config; |
15
|
|
|
|
16
|
|
|
protected $url; |
17
|
|
|
|
18
|
|
|
public function __construct(Config $config) { |
19
|
|
|
$this->config = $config; |
20
|
|
|
$this->url = ($this->config->useSsl ? 'https://' : 'http://') . Config::ED_FORM . '/'. |
21
|
|
|
$this->config->bucketName; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $path 文件存储路径 |
26
|
|
|
* @param Psr7\stream $stream 通过 `Psr7\stream_for` 方法格式化的流资源 |
27
|
|
|
* @param string $fileHash 文件 md5 值 |
28
|
|
|
* @param array $params 其他自定义参数 |
29
|
|
|
* |
30
|
|
|
* @return Psr7\Response |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
|
|
public function upload($path, $stream, $fileHash, $params = []) { |
34
|
|
|
$path = '/' . ltrim($path, '/'); |
35
|
|
|
$initInfo = $this->initRequest($path, $stream, $fileHash, $params); |
36
|
|
|
$blockStatus = $initInfo->status; |
37
|
|
|
|
38
|
|
|
$newBlockStatus = $blockStatus; |
39
|
|
|
|
40
|
|
|
for($blockId = 0; $blockId < $initInfo->blocks; $blockId++) { |
|
|
|
|
41
|
|
|
if($blockStatus[$blockId] === 0) { |
|
|
|
|
42
|
|
|
$return = $this->blockUpload($initInfo, $blockId, $stream); |
43
|
|
|
$newBlockStatus = $return->status; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if(array_sum($newBlockStatus) === $initInfo->blocks) { |
|
|
|
|
48
|
|
|
return $this->endRequest($initInfo, $params); |
49
|
|
|
} else { |
50
|
|
|
throw new \Exception(sprintf("chunk upload failed! current every block status is : [%s]", implode(',', $newBlockStatus))); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function initRequest($path, Psr7\Stream $stream, $fileHash, $params) { |
55
|
|
|
$metaData = array( |
56
|
|
|
'expiration' => time() + $this->config->blockExpiration, |
57
|
|
|
'file_blocks' => ceil($stream->getSize() / $this->config->maxBlockSize), |
58
|
|
|
'file_hash' => $fileHash, |
59
|
|
|
'file_size' => $stream->getSize(), |
60
|
|
|
'path' => $path |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$metaData = array_merge($metaData, $params); |
64
|
|
|
$policy = Util::base64Json($metaData); |
65
|
|
|
$signature = Signature::getSignature( |
66
|
|
|
$this->config, |
67
|
|
|
$metaData, |
68
|
|
|
Signature::SIGN_MULTIPART |
69
|
|
|
); |
70
|
|
|
$postData = compact('policy', 'signature'); |
71
|
|
|
|
72
|
|
|
$client = new Client(); |
73
|
|
|
$response = $client->request('POST', $this->url, [ |
74
|
|
|
'form_params' => $postData, |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$initInfo = json_decode($response->getBody()->getContents()); |
78
|
|
|
return $initInfo; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function blockUpload($blocksInfo, $blockId, Psr7\Stream $stream, $params = []) { |
82
|
|
|
$startPosition = $blockId * $this->config->maxBlockSize; |
83
|
|
|
$endPosition = $blockId >= $blocksInfo->blocks - 1 ? $stream->getSize() : $startPosition + $this->config->maxBlockSize; |
84
|
|
|
|
85
|
|
|
$stream->seek($startPosition); |
86
|
|
|
|
87
|
|
|
$fileBlock = $stream->read($endPosition - $startPosition); |
88
|
|
|
|
89
|
|
|
$metaData = array( |
90
|
|
|
'save_token' => $blocksInfo->save_token, |
91
|
|
|
'expiration' => $blocksInfo->expired_at, |
92
|
|
|
'block_index' => $blockId, |
93
|
|
|
'block_hash' => md5($fileBlock), |
94
|
|
|
); |
95
|
|
|
$metaData = array_merge($metaData, $params); |
96
|
|
|
$postData['policy'] = Util::base64Json($metaData); |
|
|
|
|
97
|
|
|
$postData['signature'] = Signature::getSignature( |
98
|
|
|
$this->config, |
99
|
|
|
$metaData, |
100
|
|
|
Signature::SIGN_MULTIPART, |
101
|
|
|
$blocksInfo->token_secret |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$multipart = []; |
105
|
|
|
foreach($postData as $key => $value) { |
|
|
|
|
106
|
|
|
$multipart[] = ['name' => $key, 'contents' => $value]; |
107
|
|
|
} |
108
|
|
|
$multipart[] = [ |
109
|
|
|
'name' => 'file', |
110
|
|
|
'contents' => $fileBlock, |
111
|
|
|
'filename' => 'file', //this value must be file |
112
|
|
|
'headers' => ['Content-Type' => 'application/octet-stream'] |
113
|
|
|
]; |
114
|
|
|
$postData['file'] = $fileBlock; |
115
|
|
|
|
116
|
|
|
$client = new Client(); |
117
|
|
|
$response = $client->request('POST', $this->url, [ |
118
|
|
|
'multipart' => $multipart, |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
return json_decode($response->getBody()->getContents()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function endRequest($initInfo, $data = array()) { |
125
|
|
|
$metaData['save_token'] = $initInfo->save_token; |
|
|
|
|
126
|
|
|
$metaData['expiration'] = $initInfo->expired_at; |
127
|
|
|
|
128
|
|
|
$metaData = array_merge($metaData, $data); |
129
|
|
|
$policy = Util::base64Json($metaData); |
130
|
|
|
$signature = Signature::getSignature( |
131
|
|
|
$this->config, |
132
|
|
|
$metaData, |
133
|
|
|
Signature::SIGN_MULTIPART, |
134
|
|
|
$initInfo->token_secret |
135
|
|
|
); |
136
|
|
|
$postData = compact('policy', 'signature'); |
137
|
|
|
|
138
|
|
|
$client = new Client(); |
139
|
|
|
$response = $client->request('POST', $this->url, [ |
140
|
|
|
'form_params' => $postData |
141
|
|
|
]); |
142
|
|
|
return $response; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|