Qiniu.php 5.5 KB

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