ResumeUploader.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace Qiniu\Storage;
  3. use Qiniu\Config;
  4. use Qiniu\Http\Client;
  5. use Qiniu\Http\Error;
  6. /**
  7. * 断点续上传类, 该类主要实现了断点续上传中的分块上传,
  8. * 以及相应地创建块和创建文件过程.
  9. *
  10. * @link http://developer.qiniu.com/docs/v6/api/reference/up/mkblk.html
  11. * @link http://developer.qiniu.com/docs/v6/api/reference/up/mkfile.html
  12. */
  13. final class ResumeUploader
  14. {
  15. private $upToken;
  16. private $key;
  17. private $inputStream;
  18. private $size;
  19. private $params;
  20. private $mime;
  21. private $contexts;
  22. private $host;
  23. private $currentUrl;
  24. private $config;
  25. /**
  26. * 上传二进制流到七牛
  27. *
  28. * @param string $upToken 上传凭证
  29. * @param string $key 上传文件名
  30. * @param resource $inputStream 上传二进制流
  31. * @param int $size 上传流的大小
  32. * @param array $params 自定义变量
  33. * @param string $mime 上传数据的mimeType
  34. *
  35. * @link http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  36. */
  37. public function __construct(
  38. $upToken,
  39. $key,
  40. $inputStream,
  41. $size,
  42. $params = null,
  43. $mime = '',
  44. $config = null
  45. ) {
  46. $this->upToken = $upToken;
  47. $this->key = $key;
  48. $this->inputStream = $inputStream;
  49. $this->size = $size;
  50. $this->params = $params;
  51. $this->mime = $mime ? $mime : 'application/octet-stream';
  52. $this->contexts = array();
  53. $this->config = $config ? $config : new Config();
  54. list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
  55. if ($err != null) {
  56. return array(null, $err);
  57. }
  58. $upHost = $this->config->getUpHost($accessKey, $bucket);
  59. if ($err != null) {
  60. throw new \Exception($err->message(), 1);
  61. }
  62. $this->host = $upHost;
  63. }
  64. /**
  65. * 上传操作
  66. */
  67. public function upload($fname)
  68. {
  69. $uploaded = 0;
  70. while ($uploaded < $this->size) {
  71. $blockSize = $this->blockSize($uploaded);
  72. $data = fread($this->inputStream, $blockSize);
  73. if ($data === false) {
  74. throw new \Exception("file read failed", 1);
  75. }
  76. $crc = \Qiniu\crc32_data($data);
  77. $response = $this->makeBlock($data, $blockSize);
  78. $ret = null;
  79. if ($response->ok() && $response->json() != null) {
  80. $ret = $response->json();
  81. }
  82. if ($response->statusCode < 0) {
  83. list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($this->upToken);
  84. if ($err != null) {
  85. return array(null, $err);
  86. }
  87. $upHostBackup = $this->config->getUpBackupHost($accessKey, $bucket);
  88. $this->host = $upHostBackup;
  89. }
  90. if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
  91. $response = $this->makeBlock($data, $blockSize);
  92. $ret = $response->json();
  93. }
  94. if (!$response->ok() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
  95. return array(null, new Error($this->currentUrl, $response));
  96. }
  97. array_push($this->contexts, $ret['ctx']);
  98. $uploaded += $blockSize;
  99. }
  100. return $this->makeFile($fname);
  101. }
  102. public function uploadChunk($index, $file, $size)
  103. {
  104. $blockSize = $this->size;
  105. $data = fread($this->inputStream, $size);
  106. if ($data === false) {
  107. throw new \Exception("file read failed", 1);
  108. }
  109. $crc = \Qiniu\crc32_data($data);
  110. $response = $this->makeBlock($data, $blockSize);
  111. $ret = null;
  112. if ($response->ok() && $response->json() != null) {
  113. $ret = $response->json();
  114. }
  115. if ($response->statusCode < 0) {
  116. list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($this->upToken);
  117. if ($err != null) {
  118. return array(null, $err);
  119. }
  120. $upHostBackup = $this->config->getUpBackupHost($accessKey, $bucket);
  121. $this->host = $upHostBackup;
  122. }
  123. if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
  124. $response = $this->makeBlock($data, $blockSize);
  125. $ret = $response->json();
  126. }
  127. if (!$response->ok() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
  128. return array(null, new Error($this->currentUrl, $response));
  129. }
  130. array_push($this->contexts, $ret['ctx']);
  131. return $ret;
  132. }
  133. public function setContexts($contexts)
  134. {
  135. $this->contexts = is_array($contexts) ? $contexts : explode(',', $contexts);
  136. return $this;
  137. }
  138. /**
  139. * 创建块
  140. */
  141. private function makeBlock($block, $blockSize)
  142. {
  143. $url = $this->host . '/mkblk/' . $blockSize;
  144. return $this->post($url, $block);
  145. }
  146. private function fileUrl($fname)
  147. {
  148. $url = $this->host . '/mkfile/' . $this->size;
  149. $url .= '/mimeType/' . \Qiniu\base64_urlSafeEncode($this->mime);
  150. if ($this->key != null) {
  151. $url .= '/key/' . \Qiniu\base64_urlSafeEncode($this->key);
  152. }
  153. $url .= '/fname/' . \Qiniu\base64_urlSafeEncode($fname);
  154. if (!empty($this->params)) {
  155. foreach ($this->params as $key => $value) {
  156. $val = \Qiniu\base64_urlSafeEncode($value);
  157. $url .= "/$key/$val";
  158. }
  159. }
  160. return $url;
  161. }
  162. /**
  163. * 创建文件
  164. */
  165. public function makeFile($fname)
  166. {
  167. $url = $this->fileUrl($fname);
  168. $body = implode(',', $this->contexts);
  169. $response = $this->post($url, $body);
  170. if ($response->needRetry()) {
  171. $response = $this->post($url, $body);
  172. }
  173. if (!$response->ok()) {
  174. return array(null, new Error($this->currentUrl, $response));
  175. }
  176. return array($response->json(), null);
  177. }
  178. private function post($url, $data)
  179. {
  180. $this->currentUrl = $url;
  181. $headers = array('Authorization' => 'UpToken ' . $this->upToken);
  182. return Client::post($url, $data, $headers);
  183. }
  184. private function blockSize($uploaded)
  185. {
  186. if ($this->size < $uploaded + Config::BLOCK_SIZE) {
  187. return $this->size - $uploaded;
  188. }
  189. return Config::BLOCK_SIZE;
  190. }
  191. }