1 | <?php |
||
15 | final class ResumeUploader |
||
16 | { |
||
17 | private $upToken; |
||
18 | private $key; |
||
19 | private $inputStream; |
||
20 | private $size; |
||
21 | private $params; |
||
22 | private $mime; |
||
23 | private $contexts; |
||
24 | private $host; |
||
25 | private $currentUrl; |
||
26 | private $config; |
||
27 | |||
28 | /** |
||
29 | * 上传二进制流到七牛 |
||
30 | * |
||
31 | * @param $upToken 上传凭证 |
||
32 | * @param $key 上传文件名 |
||
33 | * @param $inputStream 上传二进制流 |
||
34 | * @param $size 上传流的大小 |
||
35 | * @param $params 自定义变量 |
||
36 | * @param $mime 上传数据的mimeType |
||
37 | * |
||
38 | * @link http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar |
||
39 | */ |
||
40 | 6 | public function __construct( |
|
64 | |||
65 | /** |
||
66 | * 上传操作 |
||
67 | */ |
||
68 | 6 | public function upload() |
|
69 | { |
||
70 | 6 | $uploaded = 0; |
|
71 | 6 | while ($uploaded < $this->size) { |
|
72 | 6 | $blockSize = $this->blockSize($uploaded); |
|
73 | 6 | $data = fread($this->inputStream, $blockSize); |
|
74 | 6 | if ($data === false) { |
|
75 | throw new \Exception("file read failed", 1); |
||
76 | } |
||
77 | 6 | $crc = \Qiniu\crc32_data($data); |
|
78 | 6 | $response = $this->makeBlock($data, $blockSize); |
|
79 | 6 | $ret = null; |
|
80 | 6 | if ($response->ok() && $response->json() != null) { |
|
81 | $ret = $response->json(); |
||
82 | } |
||
83 | 6 | if ($response->statusCode < 0) { |
|
84 | list($bakHost, $err) = $this->config->zone->getBackupUpHostByToken($this->upToken); |
||
85 | if ($err != null) { |
||
86 | return array(null, $err); |
||
87 | } |
||
88 | $this->host = $bakHost; |
||
89 | } |
||
90 | 6 | if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) { |
|
91 | 6 | $response = $this->makeBlock($data, $blockSize); |
|
92 | 6 | $ret = $response->json(); |
|
93 | 6 | } |
|
94 | |||
95 | 6 | if (! $response->ok() || !isset($ret['crc32'])|| $crc != $ret['crc32']) { |
|
96 | 6 | return array(null, new Error($this->currentUrl, $response)); |
|
97 | } |
||
98 | array_push($this->contexts, $ret['ctx']); |
||
99 | $uploaded += $blockSize; |
||
100 | } |
||
101 | return $this->makeFile(); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * 创建块 |
||
106 | */ |
||
107 | 6 | private function makeBlock($block, $blockSize) |
|
112 | |||
113 | private function fileUrl() |
||
114 | { |
||
115 | $url = $this->host . '/mkfile/' . $this->size; |
||
116 | $url .= '/mimeType/' . \Qiniu\base64_urlSafeEncode($this->mime); |
||
117 | if ($this->key != null) { |
||
118 | $url .= '/key/' . \Qiniu\base64_urlSafeEncode($this->key); |
||
119 | } |
||
120 | if (!empty($this->params)) { |
||
121 | foreach ($this->params as $key => $value) { |
||
122 | $val = \Qiniu\base64_urlSafeEncode($value); |
||
123 | $url .= "/$key/$val"; |
||
124 | } |
||
125 | } |
||
126 | return $url; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * 创建文件 |
||
131 | */ |
||
132 | private function makeFile() |
||
133 | { |
||
134 | $url = $this->fileUrl(); |
||
135 | $body = implode(',', $this->contexts); |
||
136 | $response = $this->post($url, $body); |
||
137 | if ($response->needRetry()) { |
||
138 | $response = $this->post($url, $body); |
||
139 | } |
||
140 | if (! $response->ok()) { |
||
141 | return array(null, new Error($this->currentUrl, $response)); |
||
142 | } |
||
143 | return array($response->json(), null); |
||
144 | } |
||
145 | |||
146 | 6 | private function post($url, $data) |
|
152 | |||
153 | 6 | private function blockSize($uploaded) |
|
154 | { |
||
155 | 6 | if ($this->size < $uploaded + Config::BLOCK_SIZE) { |
|
160 | } |
||
161 |