UploadManager.php 3.9 KB

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