UploadManager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 string $upToken 上传凭证
  26. * @param string $key 上传文件名
  27. * @param resource $data 上传二进制流
  28. * @param array $params 自定义变量,规格参考 http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  29. * @param string $mime 上传数据的mimeType
  30. * @param bool $checkCrc 是否校验crc32
  31. *
  32. * @return array 包含已上传文件的信息,类似:
  33. * [
  34. * "hash" => "<Hash string>",
  35. * "key" => "<Key string>"
  36. * ]
  37. */
  38. public function put($upToken, $key, $data, $params = null, $mime = 'application/octet-stream', $fname = "default_filename")
  39. {
  40. $params = self::trimParams($params);
  41. return FormUploader::put($upToken, $key, $data, $this->config, $params, $mime, $fname);
  42. }
  43. /**
  44. * 上传文件到七牛
  45. *
  46. * @param string $upToken 上传凭证
  47. * @param string $key 上传文件名
  48. * @param string $filePath 上传文件的路径
  49. * @param array $params 自定义变量,规格参考 http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  50. * @param string $mime 上传数据的mimeType
  51. * @param bool $checkCrc 是否校验crc32
  52. *
  53. * @return array 包含已上传文件的信息,类似:
  54. * [
  55. * "hash" => "<Hash string>",
  56. * "key" => "<Key string>"
  57. * ]
  58. */
  59. public function putFile($upToken, $key, $filePath, $params = null, $mime = 'application/octet-stream', $checkCrc = false)
  60. {
  61. $file = fopen($filePath, 'rb');
  62. if ($file === false) {
  63. throw new \Exception("file can not open", 1);
  64. }
  65. $params = self::trimParams($params);
  66. $stat = fstat($file);
  67. $size = $stat['size'];
  68. //不满足分片上传条件
  69. if ($size <= Config::BLOCK_SIZE) {
  70. $data = fread($file, $size);
  71. fclose($file);
  72. if ($data === false) {
  73. throw new \Exception("file can not read", 1);
  74. }
  75. return FormUploader::put($upToken, $key, $data, $this->config, $params, $mime, basename($filePath));
  76. } else {
  77. $up = new ResumeUploader($upToken, $key, $file, $size, $params, $mime, $this->config);
  78. $ret = $up->upload(basename($filePath));
  79. fclose($file);
  80. return $ret;
  81. }
  82. }
  83. public static function trimParams($params)
  84. {
  85. if ($params === null) {
  86. return null;
  87. }
  88. $ret = array();
  89. foreach ($params as $k => $v) {
  90. $pos1 = strpos($k, 'x:');
  91. $pos2 = strpos($k, 'x-qn-meta-');
  92. if (($pos1 === 0 || $pos2 === 0) && !empty($v)) {
  93. $ret[$k] = $v;
  94. }
  95. }
  96. return $ret;
  97. }
  98. }