Qiniu.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. $token = $this->app()->uploadToken($this->storageName);
  92. try {
  93. $uploadMgr = new UploadManager();
  94. [$result, $error] = $uploadMgr->putFile($token, $key, $fileHandle->getRealPath());
  95. if ($error !== null) {
  96. return $this->setError($error->message());
  97. }
  98. $this->fileInfo->uploadInfo = $result;
  99. $this->fileInfo->filePath = $this->uploadUrl . '/' . $key;
  100. $this->fileInfo->fileName = $key;
  101. return $this->fileInfo;
  102. } catch (UploadException $e) {
  103. return $this->setError($e->getMessage());
  104. }
  105. }
  106. /**
  107. * 文件流上传
  108. * @param string $fileContent
  109. * @param string|null $key
  110. * @return array|bool|mixed|\StdClass
  111. */
  112. public function stream(string $fileContent, string $key = null)
  113. {
  114. $token = $this->app()->uploadToken($this->storageName);
  115. if (!$key) {
  116. $key = $this->saveFileName();
  117. }
  118. try {
  119. $uploadMgr = new UploadManager();
  120. [$result, $error] = $uploadMgr->put($token, $key, $fileContent);
  121. if ($error !== null) {
  122. return $this->setError($error->message());
  123. }
  124. $this->fileInfo->uploadInfo = $result;
  125. $this->fileInfo->filePath = $this->uploadUrl . '/' . $key;
  126. $this->fileInfo->fileName = $key;
  127. return $this->fileInfo;
  128. } catch (UploadException $e) {
  129. return $this->setError($e->getMessage());
  130. }
  131. }
  132. /**
  133. * 获取上传配置信息
  134. * @return array
  135. */
  136. public function getSystem()
  137. {
  138. $token = $this->app()->uploadToken($this->storageName);
  139. $domain = $this->uploadUrl;
  140. $key = $this->saveFileName();
  141. return compact('token', 'domain', 'key');
  142. }
  143. /**
  144. * TODO 删除资源
  145. * @param $key
  146. * @param $bucket
  147. * @return mixed
  148. */
  149. public function delete(string $key)
  150. {
  151. $bucketManager = new BucketManager($this->app(), new Config());
  152. return $bucketManager->delete($this->storageName, $key);
  153. }
  154. }