ResumeUploader.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 $upToken 上传凭证
  29. * @param $key 上传文件名
  30. * @param $inputStream 上传二进制流
  31. * @param $size 上传流的大小
  32. * @param $params 自定义变量
  33. * @param $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,
  43. $mime,
  44. $config
  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;
  52. $this->contexts = array();
  53. $this->config = $config;
  54. list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
  55. if ($err != null) {
  56. return array(null, $err);
  57. }
  58. $upHost = $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. /**
  103. * 创建块
  104. */
  105. private function makeBlock($block, $blockSize)
  106. {
  107. $url = $this->host . '/mkblk/' . $blockSize;
  108. return $this->post($url, $block);
  109. }
  110. private function fileUrl($fname)
  111. {
  112. $url = $this->host . '/mkfile/' . $this->size;
  113. $url .= '/mimeType/' . \Qiniu\base64_urlSafeEncode($this->mime);
  114. if ($this->key != null) {
  115. $url .= '/key/' . \Qiniu\base64_urlSafeEncode($this->key);
  116. }
  117. $url .= '/fname/' . \Qiniu\base64_urlSafeEncode($fname);
  118. if (!empty($this->params)) {
  119. foreach ($this->params as $key => $value) {
  120. $val = \Qiniu\base64_urlSafeEncode($value);
  121. $url .= "/$key/$val";
  122. }
  123. }
  124. return $url;
  125. }
  126. /**
  127. * 创建文件
  128. */
  129. private function makeFile($fname)
  130. {
  131. $url = $this->fileUrl($fname);
  132. $body = implode(',', $this->contexts);
  133. $response = $this->post($url, $body);
  134. if ($response->needRetry()) {
  135. $response = $this->post($url, $body);
  136. }
  137. if (!$response->ok()) {
  138. return array(null, new Error($this->currentUrl, $response));
  139. }
  140. return array($response->json(), null);
  141. }
  142. private function post($url, $data)
  143. {
  144. $this->currentUrl = $url;
  145. $headers = array('Authorization' => 'UpToken ' . $this->upToken);
  146. return Client::post($url, $data, $headers);
  147. }
  148. private function blockSize($uploaded)
  149. {
  150. if ($this->size < $uploaded + Config::BLOCK_SIZE) {
  151. return $this->size - $uploaded;
  152. }
  153. return Config::BLOCK_SIZE;
  154. }
  155. }