截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\common\server\storage\engine;
  3. use Qiniu\Auth;
  4. use Qiniu\Storage\UploadManager;
  5. use Qiniu\Storage\BucketManager;
  6. /**
  7. * 七牛云存储引擎
  8. * Class Qiniu
  9. * @package app\common\library\storage\engine
  10. */
  11. class Qiniu extends Server
  12. {
  13. private $config;
  14. /**
  15. * 构造方法
  16. * Qiniu constructor.
  17. * @param $config
  18. */
  19. public function __construct($config)
  20. {
  21. parent::__construct();
  22. $this->config = $config;
  23. }
  24. /**
  25. * 执行上传
  26. * @param $save_dir (保存路径)
  27. * @return bool|mixed
  28. */
  29. public function upload($save_dir)
  30. {
  31. // 要上传图片的本地路径
  32. $realPath = $this->getRealPath();
  33. // 构建鉴权对象
  34. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  35. // 要上传的空间
  36. $token = $auth->uploadToken($this->config['bucket']);
  37. // 初始化 UploadManager 对象并进行文件的上传
  38. $uploadMgr = new UploadManager();
  39. try {
  40. // 调用 UploadManager 的 putFile 方法进行文件的上传
  41. $key = $save_dir . '/' . $this->fileName;
  42. list(, $error) = $uploadMgr->putFile($token, $key, $realPath);
  43. if ($error !== null) {
  44. $this->error = $error->message();
  45. return false;
  46. }
  47. return true;
  48. } catch (\Exception $e) {
  49. $this->error = $e->getMessage();
  50. return false;
  51. }
  52. }
  53. /**
  54. * Notes: 抓取远程资源
  55. * @param $url
  56. * @param null $key
  57. * @author 张无忌(2021/3/2 14:03)
  58. * @return bool
  59. */
  60. public function fetch($url, $key=null)
  61. {
  62. try {
  63. if (substr($url, 0, 1) !== '/' || strstr($url, 'http://') || strstr($url, 'https://')) {
  64. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  65. $bucketManager = new BucketManager($auth);
  66. list(, $err) = $bucketManager->fetch($url, $this->config['bucket'], $key);
  67. } else {
  68. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  69. $token = $auth->uploadToken($this->config['bucket']);
  70. $uploadMgr = new UploadManager();
  71. list(, $err) = $uploadMgr->putFile($token, $key, $url);
  72. }
  73. if ($err !== null) {
  74. $this->error = $err->message();
  75. return false;
  76. }
  77. return true;
  78. } catch (\Exception $e) {
  79. $this->error = $e->getMessage();
  80. return false;
  81. }
  82. }
  83. /**
  84. * 删除文件
  85. * @param $fileName
  86. * @return bool|mixed
  87. */
  88. public function delete($fileName)
  89. {
  90. // 构建鉴权对象
  91. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  92. // 初始化 UploadManager 对象并进行文件的上传
  93. $bucketMgr = new BucketManager($auth);
  94. try {
  95. $error = $bucketMgr->delete($this->config['bucket'], $fileName);
  96. if ($error !== null) {
  97. $this->error = $error->message();
  98. return false;
  99. }
  100. return true;
  101. } catch (\Exception $e) {
  102. $this->error = $e->getMessage();
  103. return false;
  104. }
  105. }
  106. /**
  107. * 返回文件路径
  108. * @return mixed
  109. */
  110. public function getFileName()
  111. {
  112. return $this->fileName;
  113. }
  114. }