Qiniu.php 4.0 KB

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