Qiniu.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace crmeb\services\upload\storage;
  3. use crmeb\basic\BaseUpload;
  4. use crmeb\exceptions\UploadException;
  5. use Qiniu\Auth;
  6. use Qiniu\Storage\BucketManager;
  7. use Qiniu\Storage\UploadManager;
  8. use Qiniu\Config;
  9. use think\exception\ValidateException;
  10. /**
  11. * TODO 七牛云上传
  12. * Class Qiniu
  13. */
  14. class Qiniu extends BaseUpload
  15. {
  16. /**
  17. * accessKey
  18. * @var mixed
  19. */
  20. protected $accessKey;
  21. /**
  22. * secretKey
  23. * @var mixed
  24. */
  25. protected $secretKey;
  26. /**
  27. * 句柄
  28. * @var object
  29. */
  30. protected $handle;
  31. /**
  32. * 空间域名 Domain
  33. * @var mixed
  34. */
  35. protected $uploadUrl;
  36. /**
  37. * 存储空间名称 公开空间
  38. * @var mixed
  39. */
  40. protected $storageName;
  41. /**
  42. * COS使用 所属地域
  43. * @var mixed|null
  44. */
  45. protected $storageRegion;
  46. /**
  47. * 初始化
  48. * @param array $config
  49. * @return mixed|void
  50. */
  51. public function initialize(array $config)
  52. {
  53. parent::initialize($config);
  54. $this->accessKey = $config['accessKey'] ?? null;
  55. $this->secretKey = $config['secretKey'] ?? null;
  56. $this->uploadUrl = $this->checkUploadUrl($config['uploadUrl'] ?? '');
  57. $this->storageName = $config['storageName'] ?? null;
  58. $this->storageRegion = $config['storageRegion'] ?? null;
  59. }
  60. /**
  61. * 实例化七牛云
  62. * @return object|Auth
  63. */
  64. protected function app()
  65. {
  66. if (!$this->accessKey || !$this->secretKey) {
  67. throw new UploadException('Please configure accessKey and secretKey');
  68. }
  69. $this->handle = new Auth($this->accessKey, $this->secretKey);
  70. return $this->handle;
  71. }
  72. /**
  73. * 上传文件
  74. * @param string $file
  75. * @return array|bool|mixed|\StdClass|string
  76. */
  77. public function move(string $file = 'file')
  78. {
  79. $fileHandle = app()->request->file($file);
  80. if (!$fileHandle) {
  81. return $this->setError('Upload file does not exist');
  82. }
  83. if ($this->validate) {
  84. try {
  85. validate([$file => $this->validate])->check([$file => $fileHandle]);
  86. } catch (ValidateException $e) {
  87. return $this->setError($e->getMessage());
  88. }
  89. }
  90. $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
  91. // $key = null;
  92. $token = $this->app()->uploadToken($this->storageName, $key);
  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, $key);
  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. }