Alioss.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace addons\alioss;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. use think\Addons;
  6. use think\App;
  7. use think\Config;
  8. use think\Loader;
  9. /**
  10. * 阿里云OSS上传插件
  11. */
  12. class Alioss extends Addons
  13. {
  14. /**
  15. * 插件安装方法
  16. * @return bool
  17. */
  18. public function install()
  19. {
  20. return true;
  21. }
  22. /**
  23. * 插件卸载方法
  24. * @return bool
  25. */
  26. public function uninstall()
  27. {
  28. return true;
  29. }
  30. /**
  31. * 添加命名空间
  32. */
  33. public function appInit()
  34. {
  35. if (!class_exists("\OSS\OssClient")) {
  36. //添加支付包的命名空间
  37. Loader::addNamespace('OSS', ADDON_PATH . 'alioss' . DS . 'library' . DS . 'OSS' . DS);
  38. }
  39. }
  40. /**
  41. * 判断是否来源于API上传
  42. */
  43. public function moduleInit($request)
  44. {
  45. $config = $this->getConfig();
  46. $module = strtolower($request->module());
  47. if ($module == 'api' && ($config['apiupload'] ?? 0) && in_array($module, explode(',', $config['uploadmodule'] ?? '')) &&
  48. strtolower($request->controller()) == 'common' &&
  49. strtolower($request->action()) == 'upload') {
  50. request()->param('isApi', true);
  51. App::invokeMethod(["\\addons\\alioss\\controller\\Index", "upload"], ['isApi' => true]);
  52. }
  53. }
  54. /**
  55. * 加载配置
  56. */
  57. public function uploadConfigInit(&$upload)
  58. {
  59. $config = $this->getConfig();
  60. $module = request()->module();
  61. $module = $module ? strtolower($module) : 'index';
  62. $uploadModule = array_filter(explode(',', $config['uploadmodule'] ?? ''));
  63. if (!in_array($module, $uploadModule)) {
  64. return $upload;
  65. }
  66. $data = ['deadline' => time() + $config['expire']];
  67. $signature = hash_hmac('sha1', json_encode($data), $config['accessKeySecret'], true);
  68. $token = '';
  69. $noNeedLogin = array_filter(explode(',', $config['noneedlogin'] ?? ''));
  70. if (in_array($module, $noNeedLogin) || ($module == 'admin' && \app\admin\library\Auth::instance()->id) || ($module != 'admin' && \app\common\library\Auth::instance()->id)) {
  71. $token = $config['accessKeyId'] . ':' . base64_encode($signature) . ':' . base64_encode(json_encode($data));
  72. }
  73. $multipart = [
  74. 'aliosstoken' => $token
  75. ];
  76. $config['uploadurl'] = 'https://' . $config['bucket'] . '.' . $config['endpoint'];
  77. $upload = array_merge($upload, [
  78. 'cdnurl' => $config['cdnurl'],
  79. 'uploadurl' => $config['uploadmode'] == 'client' ? $config['uploadurl'] : addon_url('alioss/index/upload', [], false, true),
  80. 'uploadmode' => $config['uploadmode'],
  81. 'bucket' => $config['bucket'],
  82. 'maxsize' => $config['maxsize'],
  83. 'mimetype' => $config['mimetype'],
  84. 'savekey' => $config['savekey'],
  85. 'chunking' => (bool)($config['chunking'] ?? $upload['chunking']),
  86. 'chunksize' => (int)($config['chunksize'] ?? $upload['chunksize']),
  87. 'multipart' => $multipart,
  88. 'storage' => $this->getName(),
  89. 'multiple' => $config['multiple'] ? true : false,
  90. ]);
  91. }
  92. /**
  93. * 附件删除后
  94. */
  95. public function uploadDelete($attachment)
  96. {
  97. $config = $this->getConfig();
  98. if ($attachment['storage'] == 'alioss' && isset($config['syncdelete']) && $config['syncdelete']) {
  99. try {
  100. $ossClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);
  101. $ossClient->deleteObject($config['bucket'], ltrim($attachment->url, '/'));
  102. } catch (OssException $e) {
  103. return false;
  104. }
  105. //如果是服务端中转,还需要删除本地文件
  106. //if ($config['uploadmode'] == 'server') {
  107. // $filePath = ROOT_PATH . 'public' . str_replace('/', DS, $attachment->url);
  108. // if ($filePath) {
  109. // @unlink($filePath);
  110. // }
  111. //}
  112. }
  113. return true;
  114. }
  115. }