Qiniu.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace library\services\upload\storage;
  3. use library\basic\BaseUpload;
  4. use library\exceptions\UploadException;
  5. use Qiniu\Auth;
  6. use function Qiniu\base64_urlSafeEncode;
  7. use Qiniu\Storage\BucketManager;
  8. use Qiniu\Storage\UploadManager;
  9. use Qiniu\Config;
  10. use think\exception\ValidateException;
  11. /**
  12. * TODO 七牛云上传
  13. * Class Qiniu
  14. */
  15. class Qiniu extends BaseUpload
  16. {
  17. /**
  18. * accessKey
  19. * @var mixed
  20. */
  21. protected $accessKey;
  22. /**
  23. * secretKey
  24. * @var mixed
  25. */
  26. protected $secretKey;
  27. /**
  28. * 句柄
  29. * @var object
  30. */
  31. protected $handle;
  32. /**
  33. * 空间域名 Domain
  34. * @var mixed
  35. */
  36. protected $uploadUrl;
  37. /**
  38. * 存储空间名称 公开空间
  39. * @var mixed
  40. */
  41. protected $storageName;
  42. /**
  43. * COS使用 所属地域
  44. * @var mixed|null
  45. */
  46. protected $storageRegion;
  47. /**
  48. * 初始化
  49. * @param array $config
  50. * @return mixed|void
  51. */
  52. public function initialize(array $config)
  53. {
  54. parent::initialize($config);
  55. $this->accessKey = $config['accessKey'] ?? null;
  56. $this->secretKey = $config['secretKey'] ?? null;
  57. $this->uploadUrl = $this->checkUploadUrl($config['uploadUrl'] ?? '');
  58. $this->storageName = $config['storageName'] ?? null;
  59. $this->storageRegion = $config['storageRegion'] ?? null;
  60. }
  61. /**
  62. * 实例化七牛云
  63. * @return object|Auth
  64. */
  65. protected function app()
  66. {
  67. if (!$this->accessKey || !$this->secretKey) {
  68. throw new UploadException('Please configure accessKey and secretKey');
  69. }
  70. $this->handle = new Auth($this->accessKey, $this->secretKey);
  71. return $this->handle;
  72. }
  73. /**
  74. * 上传文件
  75. * @param string $file
  76. * @return array|bool|mixed|\StdClass|string
  77. */
  78. public function move(string $file = 'file')
  79. {
  80. $fileHandle = app()->request->file($file);
  81. if (!$fileHandle) {
  82. return $this->setError('Upload file does not exist');
  83. }
  84. if ($this->validate) {
  85. try {
  86. validate([$file => $this->validate])->check([$file => $fileHandle]);
  87. } catch (ValidateException $e) {
  88. return $this->setError($e->getMessage());
  89. }
  90. }
  91. $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
  92. $token = $this->app()->uploadToken($this->storageName);
  93. try {
  94. $uploadMgr = new UploadManager();
  95. [$result, $error] = $uploadMgr->putFile($token, $key, $fileHandle->getRealPath());
  96. if ($error !== null) {
  97. return $this->setError($error->message());
  98. }
  99. $this->fileInfo->uploadInfo = $result;
  100. $this->fileInfo->filePath = $this->uploadUrl . '/' . $key;
  101. $this->fileInfo->fileName = $key;
  102. return $this->fileInfo;
  103. } catch (UploadException $e) {
  104. return $this->setError($e->getMessage());
  105. }
  106. }
  107. /**
  108. * 文件流上传
  109. * @param string $fileContent
  110. * @param string|null $key
  111. * @return array|bool|mixed|\StdClass
  112. */
  113. public function stream(string $fileContent, string $key = null)
  114. {
  115. $token = $this->app()->uploadToken($this->storageName);
  116. if (!$key) {
  117. $key = $this->saveFileName();
  118. }
  119. try {
  120. $uploadMgr = new UploadManager();
  121. [$result, $error] = $uploadMgr->put($token, $key, $fileContent);
  122. if ($error !== null) {
  123. return $this->setError($error->message());
  124. }
  125. $this->fileInfo->uploadInfo = $result;
  126. $this->fileInfo->filePath = $this->uploadUrl . '/' . $key;
  127. $this->fileInfo->fileName = $key;
  128. return $this->fileInfo;
  129. } catch (UploadException $e) {
  130. return $this->setError($e->getMessage());
  131. }
  132. }
  133. /**
  134. * 获取上传配置信息
  135. * @return array
  136. */
  137. public function getSystem()
  138. {
  139. $token = $this->app()->uploadToken($this->storageName);
  140. $domain = $this->uploadUrl;
  141. $key = $this->saveFileName();
  142. return compact('token', 'domain', 'key');
  143. }
  144. /**
  145. * TODO 删除资源
  146. * @param $key
  147. * @param $bucket
  148. * @return mixed
  149. */
  150. public function delete(string $key)
  151. {
  152. $bucketManager = new BucketManager($this->app(), new Config());
  153. return $bucketManager->delete($this->storageName, $key);
  154. }
  155. /**
  156. * 获取七牛云上传密钥
  157. * @return mixed|string
  158. */
  159. public function getTempKeys()
  160. {
  161. $token = $this->app()->uploadToken($this->storageName);
  162. $domain = $this->uploadUrl;
  163. $key = $this->saveFileName(NULL, 'mp4');
  164. $type = 'QINIU';
  165. return compact('token', 'domain', 'key', 'type');
  166. }
  167. }