Alioss.php 3.7 KB

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