Qiniu.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace addons\qiniu;
  3. use fast\Http;
  4. use Qiniu\Auth;
  5. use think\Addons;
  6. use think\Loader;
  7. /**
  8. * 七牛云储存插件
  9. */
  10. class Qiniu extends Addons
  11. {
  12. /**
  13. * 插件安装方法
  14. * @return bool
  15. */
  16. public function install()
  17. {
  18. return true;
  19. }
  20. /**
  21. * 插件卸载方法
  22. * @return bool
  23. */
  24. public function uninstall()
  25. {
  26. return true;
  27. }
  28. /**
  29. * 上传初始化时
  30. */
  31. public function uploadConfigInit(&$upload)
  32. {
  33. $config = $this->getConfig();
  34. $policy = array(
  35. 'saveKey' => ltrim($config['savekey'], '/'),
  36. );
  37. $config['savekey'] = str_replace(['$(year)', '$(mon)', '$(day)', '$(etag)', '$(ext)'], ['{year}', '{mon}', '{day}', '{filemd5}', '{.suffix}'], $config['savekey']);
  38. $auth = new Auth($config['accessKey'], $config['secretKey']);
  39. $multipart['qiniutoken'] = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
  40. $upload = array_merge($upload, [
  41. 'cdnurl' => $config['cdnurl'],
  42. 'uploadurl' => $config['uploadmode'] == 'client' ? $config['uploadurl'] : addon_url('qiniu/index/upload', [], false, true),
  43. 'uploadmode' => $config['uploadmode'],
  44. 'bucket' => $config['bucket'],
  45. 'maxsize' => $config['maxsize'],
  46. 'mimetype' => $config['mimetype'],
  47. 'savekey' => $config['savekey'],
  48. 'chunking' => (bool)($config['chunking'] ?? $upload['chunking']),
  49. 'chunksize' => (int)($config['chunksize'] ?? $upload['chunksize']),
  50. 'multipart' => $multipart,
  51. 'storage' => $this->getName(),
  52. 'multiple' => $config['multiple'] ? true : false,
  53. ]);
  54. }
  55. /**
  56. * 附件删除后
  57. */
  58. public function uploadDelete($attachment)
  59. {
  60. $config = $this->getConfig();
  61. if ($attachment['storage'] == 'qiniu' && isset($config['syncdelete']) && $config['syncdelete']) {
  62. $auth = new Auth($config['accessKey'], $config['secretKey']);
  63. $entry = $config['bucket'] . ':' . ltrim($attachment->url, '/');
  64. $encodedEntryURI = \Qiniu\base64_urlSafeEncode($entry);
  65. $url = 'http://rs.qiniu.com/delete/' . $encodedEntryURI;
  66. $headers = $auth->authorization($url);
  67. //删除云储存文件
  68. $ret = Http::sendRequest($url, [], 'POST', [CURLOPT_HTTPHEADER => ['Authorization: ' . $headers['Authorization']]]);
  69. //如果是服务端中转,还需要删除本地文件
  70. //if ($config['uploadmode'] == 'server') {
  71. // $filePath = ROOT_PATH . 'public' . str_replace('/', DS, $attachment->url);
  72. // if ($filePath) {
  73. // @unlink($filePath);
  74. // }
  75. //}
  76. }
  77. return true;
  78. }
  79. public function appInit()
  80. {
  81. if (!class_exists('\Qiniu\Config')) {
  82. Loader::addNamespace('Qiniu', ADDON_PATH . 'qiniu' . DS . 'library' . DS . 'Qiniu' . DS);
  83. require_once ADDON_PATH . 'qiniu' . DS . 'library' . DS . 'Qiniu' . DS . 'functions.php';
  84. }
  85. }
  86. }