Qiniu.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace crmeb\services\upload\storage;
  3. use crmeb\basic\BaseUpload;
  4. use crmeb\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. * @param string $path
  77. * @return array|bool|mixed|\StdClass|string
  78. */
  79. public function move(string $file = 'file',$path=null)
  80. {
  81. $fileHandle = app()->request->file($file);
  82. if (!$fileHandle) {
  83. return $this->setError('Upload file does not exist');
  84. }
  85. if ($this->validate) {
  86. try {
  87. validate([$file => $this->validate])->check([$file => $fileHandle]);
  88. } catch (ValidateException $e) {
  89. return $this->setError($e->getMessage());
  90. }
  91. }
  92. $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
  93. $token = $this->app()->uploadToken($this->storageName);
  94. try {
  95. $uploadMgr = new UploadManager();
  96. [$result, $error] = $uploadMgr->putFile($token, $key, $fileHandle->getRealPath());
  97. if ($error !== null) {
  98. return $this->setError($error->message());
  99. }
  100. $this->fileInfo->uploadInfo = $result;
  101. $this->fileInfo->filePath = $this->uploadUrl . '/' . $key;
  102. $this->fileInfo->fileName = $key;
  103. return $this->fileInfo;
  104. } catch (UploadException $e) {
  105. return $this->setError($e->getMessage());
  106. }
  107. }
  108. /**
  109. * 文件流上传
  110. * @param string $fileContent
  111. * @param string|null $key
  112. * @return array|bool|mixed|\StdClass
  113. */
  114. public function stream(string $fileContent, string $key = null)
  115. {
  116. $token = $this->app()->uploadToken($this->storageName);
  117. if (!$key) {
  118. $key = $this->saveFileName();
  119. }
  120. try {
  121. $uploadMgr = new UploadManager();
  122. [$result, $error] = $uploadMgr->put($token, $key, $fileContent);
  123. if ($error !== null) {
  124. return $this->setError($error->message());
  125. }
  126. $this->fileInfo->uploadInfo = $result;
  127. $this->fileInfo->filePath = $this->uploadUrl . '/' . $key;
  128. $this->fileInfo->fileName = $key;
  129. return $this->fileInfo;
  130. } catch (UploadException $e) {
  131. return $this->setError($e->getMessage());
  132. }
  133. }
  134. /**
  135. * 获取上传配置信息
  136. * @return array
  137. */
  138. public function getSystem()
  139. {
  140. $token = $this->app()->uploadToken($this->storageName);
  141. $domain = $this->uploadUrl;
  142. $key = $this->saveFileName();
  143. return compact('token', 'domain', 'key');
  144. }
  145. /**
  146. * TODO 删除资源
  147. * @param $key
  148. * @param $bucket
  149. * @return mixed
  150. */
  151. public function delete(string $key)
  152. {
  153. $bucketManager = new BucketManager($this->app(), new Config());
  154. return $bucketManager->delete($this->storageName, $key);
  155. }
  156. /**
  157. * 获取七牛云上传密钥
  158. * @return mixed|string
  159. */
  160. public function getTempKeys()
  161. {
  162. $token = $this->app()->uploadToken($this->storageName);
  163. $domain = $this->uploadUrl;
  164. $key = $this->saveFileName(NULL, 'mp4');
  165. $type = 'QINIU';
  166. return compact('token', 'domain', 'key', 'type');
  167. }
  168. }