UploadManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Qiniu\Storage;
  3. use Qiniu\Config;
  4. use Qiniu\Http\HttpClient;
  5. use Qiniu\Http\RequestOptions;
  6. use Qiniu\Storage\ResumeUploader;
  7. use Qiniu\Storage\FormUploader;
  8. /**
  9. * 主要涉及了资源上传接口的实现
  10. *
  11. * @link http://developer.qiniu.com/docs/v6/api/reference/up/
  12. */
  13. final class UploadManager
  14. {
  15. private $config;
  16. /**
  17. * @var RequestOptions
  18. */
  19. private $reqOpt;
  20. /**
  21. * @param Config|null $config
  22. * @param RequestOptions|null $reqOpt
  23. */
  24. public function __construct(Config $config = null, RequestOptions $reqOpt = null)
  25. {
  26. if ($config === null) {
  27. $config = new Config();
  28. }
  29. $this->config = $config;
  30. if ($reqOpt === null) {
  31. $reqOpt = new RequestOptions();
  32. }
  33. $this->reqOpt = $reqOpt;
  34. }
  35. /**
  36. * 上传二进制流到七牛
  37. *
  38. * @param string $upToken 上传凭证
  39. * @param string $key 上传文件名
  40. * @param string $data 上传二进制流
  41. * @param array<string, string> $params 自定义变量,规格参考
  42. * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  43. * @param string $mime 上传数据的mimeType
  44. * @param string $fname
  45. * @param RequestOptions $reqOpt
  46. * @return array 包含已上传文件的信息,类似:
  47. * [
  48. * "hash" => "<Hash string>",
  49. * "key" => "<Key string>"
  50. * ]
  51. */
  52. public function put(
  53. $upToken,
  54. $key,
  55. $data,
  56. $params = null,
  57. $mime = 'application/octet-stream',
  58. $fname = "default_filename",
  59. $reqOpt = null
  60. ) {
  61. $reqOpt = $reqOpt === null ? $this->reqOpt : $reqOpt;
  62. $params = self::trimParams($params);
  63. return FormUploader::put(
  64. $upToken,
  65. $key,
  66. $data,
  67. $this->config,
  68. $params,
  69. $mime,
  70. $fname,
  71. $reqOpt
  72. );
  73. }
  74. /**
  75. * 上传文件到七牛
  76. *
  77. * @param string $upToken 上传凭证
  78. * @param string $key 上传文件名
  79. * @param string $filePath 上传文件的路径
  80. * @param array<string, mixed> $params 定义变量,规格参考
  81. * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  82. * @param boolean $mime 上传数据的mimeType
  83. * @param string $checkCrc 是否校验crc32
  84. * @param string $resumeRecordFile 断点续传文件路径 默认为null
  85. * @param string $version 分片上传版本 目前支持v1/v2版本 默认v1
  86. * @param int $partSize 分片上传v2字段 默认大小为4MB 分片大小范围为1 MB - 1 GB
  87. *
  88. * @return array<string, mixed> 包含已上传文件的信息,类似:
  89. * [
  90. * "hash" => "<Hash string>",
  91. * "key" => "<Key string>"
  92. * ]
  93. * @throws \Exception
  94. */
  95. public function putFile(
  96. $upToken,
  97. $key,
  98. $filePath,
  99. $params = null,
  100. $mime = 'application/octet-stream',
  101. $checkCrc = false,
  102. $resumeRecordFile = null,
  103. $version = 'v1',
  104. $partSize = config::BLOCK_SIZE,
  105. $reqOpt = null
  106. ) {
  107. $reqOpt = $reqOpt === null ? $this->reqOpt : $reqOpt;
  108. $file = fopen($filePath, 'rb');
  109. if ($file === false) {
  110. throw new \Exception("file can not open", 1);
  111. }
  112. $params = self::trimParams($params);
  113. $stat = fstat($file);
  114. $size = $stat['size'];
  115. if ($size <= Config::BLOCK_SIZE) {
  116. $data = fread($file, $size);
  117. fclose($file);
  118. if ($data === false) {
  119. throw new \Exception("file can not read", 1);
  120. }
  121. return FormUploader::put(
  122. $upToken,
  123. $key,
  124. $data,
  125. $this->config,
  126. $params,
  127. $mime,
  128. basename($filePath),
  129. $reqOpt
  130. );
  131. }
  132. $up = new ResumeUploader(
  133. $upToken,
  134. $key,
  135. $file,
  136. $size,
  137. $params,
  138. $mime,
  139. $this->config,
  140. $resumeRecordFile,
  141. $version,
  142. $partSize,
  143. $reqOpt
  144. );
  145. $ret = $up->upload(basename($filePath));
  146. fclose($file);
  147. return $ret;
  148. }
  149. public static function trimParams($params)
  150. {
  151. if ($params === null) {
  152. return null;
  153. }
  154. $ret = array();
  155. foreach ($params as $k => $v) {
  156. $pos1 = strpos($k, 'x:');
  157. $pos2 = strpos($k, 'x-qn-meta-');
  158. if (($pos1 === 0 || $pos2 === 0) && !empty($v)) {
  159. $ret[$k] = $v;
  160. }
  161. }
  162. return $ret;
  163. }
  164. }