Cos.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace addons\cos;
  3. use addons\cos\library\Auth;
  4. use app\common\library\Menu;
  5. use fast\Http;
  6. use Qcloud\Cos\Client;
  7. use think\Addons;
  8. use think\App;
  9. use think\Loader;
  10. /**
  11. * COS插件
  12. */
  13. class Cos 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. * @return bool
  34. */
  35. public function enable()
  36. {
  37. return true;
  38. }
  39. /**
  40. * 插件禁用方法
  41. * @return bool
  42. */
  43. public function disable()
  44. {
  45. return true;
  46. }
  47. /**
  48. * 渲染命名空间配置信息
  49. */
  50. public function appInit()
  51. {
  52. if (!class_exists('\Qcloud\Cos\Client')) {
  53. Loader::addNamespace('Qcloud\Cos', $this->addons_path . str_replace('/', DS, 'library/Qcloud/Cos/'));
  54. }
  55. if (!class_exists('\GuzzleHttp\Command\Command')) {
  56. Loader::addNamespace('GuzzleHttp\Command', $this->addons_path . str_replace('/', DS, 'library/Guzzle/command/src/'));
  57. }
  58. if (!class_exists('\GuzzleHttp\Command\Guzzle\Description')) {
  59. Loader::addNamespace('GuzzleHttp\Command\Guzzle', $this->addons_path . str_replace('/', DS, 'library/Guzzle/guzzle-services/src/'));
  60. }
  61. if (!class_exists('\GuzzleHttp\UriTemplate\UriTemplate')) {
  62. Loader::addNamespace('GuzzleHttp\UriTemplate', $this->addons_path . str_replace('/', DS, 'library/Guzzle/uri-template/src/'));
  63. }
  64. }
  65. /**
  66. * 判断是否来源于API上传
  67. */
  68. public function moduleInit($request)
  69. {
  70. $config = $this->getConfig();
  71. $module = strtolower($request->module());
  72. if ($module == 'api' && ($config['apiupload'] ?? 0) &&
  73. strtolower($request->controller()) == 'common' &&
  74. strtolower($request->action()) == 'upload') {
  75. request()->param('isApi', true);
  76. App::invokeMethod(["\\addons\\cos\\controller\\Index", "upload"], ['isApi' => true]);
  77. }
  78. }
  79. /**
  80. *
  81. */
  82. public function uploadConfigInit(&$upload)
  83. {
  84. $config = $this->getConfig();
  85. $module = request()->module();
  86. $module = $module ? strtolower($module) : 'index';
  87. $data = ['deadline' => time() + $config['expire']];
  88. $signature = hash_hmac('sha1', json_encode($data), $config['secretKey'], true);
  89. $token = '';
  90. if (Auth::isModuleAllow()) {
  91. $token = $config['appId'] . ':' . base64_encode($signature) . ':' . base64_encode(json_encode($data));
  92. }
  93. $multipart = [
  94. 'costoken' => $token
  95. ];
  96. $upload = array_merge($upload, [
  97. 'cdnurl' => $config['cdnurl'],
  98. 'uploadurl' => $config['uploadmode'] == 'client' ? $config['uploadurl'] : addon_url('cos/index/upload', [], false, true),
  99. 'uploadmode' => $config['uploadmode'],
  100. 'bucket' => $config['bucket'],
  101. 'maxsize' => $config['maxsize'],
  102. 'mimetype' => $config['mimetype'],
  103. 'savekey' => $config['savekey'],
  104. 'chunking' => (bool)($config['chunking'] ?? $upload['chunking']),
  105. 'chunksize' => (int)($config['chunksize'] ?? $upload['chunksize']),
  106. 'multipart' => $multipart,
  107. 'storage' => $this->getName(),
  108. 'multiple' => (bool)$config['multiple'],
  109. ]);
  110. }
  111. /**
  112. * 附件删除后
  113. */
  114. public function uploadDelete($attachment)
  115. {
  116. $config = $this->getConfig();
  117. if ($attachment['storage'] == 'cos' && isset($config['syncdelete']) && $config['syncdelete']) {
  118. $cosConfig = array(
  119. 'region' => $config['region'],
  120. 'schema' => 'https', //协议头部,默认为http
  121. 'credentials' => array(
  122. 'secretId' => $config['secretId'],
  123. 'secretKey' => $config['secretKey']
  124. )
  125. );
  126. $oss = new Client($cosConfig);
  127. $ret = $oss->deleteObject(array('Bucket' => $config['bucket'], 'Key' => ltrim($attachment->url, '/')));
  128. //如果是服务端中转,还需要删除本地文件
  129. //if ($config['uploadmode'] == 'server') {
  130. // $filePath = ROOT_PATH . 'public' . str_replace('/', DS, $attachment->url);
  131. // if ($filePath) {
  132. // @unlink($filePath);
  133. // }
  134. //}
  135. }
  136. return true;
  137. }
  138. }