Cos.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Loader;
  9. /**
  10. * COS插件
  11. */
  12. class Cos 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. * @return bool
  33. */
  34. public function enable()
  35. {
  36. return true;
  37. }
  38. /**
  39. * 插件禁用方法
  40. * @return bool
  41. */
  42. public function disable()
  43. {
  44. return true;
  45. }
  46. /**
  47. * 渲染命名空间配置信息
  48. */
  49. public function appInit()
  50. {
  51. if (!class_exists('\Qcloud\Cos\Client')) {
  52. Loader::addNamespace('Qcloud\Cos', $this->addons_path . str_replace('/', DS, 'library/Qcloud/Cos/'));
  53. }
  54. if (!class_exists('\GuzzleHttp\Command\Command')) {
  55. Loader::addNamespace('GuzzleHttp\Command', $this->addons_path . str_replace('/', DS, 'library/Guzzle/command/src/'));
  56. }
  57. if (!class_exists('\GuzzleHttp\Command\Guzzle\Description')) {
  58. Loader::addNamespace('GuzzleHttp\Command\Guzzle', $this->addons_path . str_replace('/', DS, 'library/Guzzle/guzzle-services/src/'));
  59. }
  60. }
  61. /**
  62. *
  63. */
  64. public function uploadConfigInit(&$upload)
  65. {
  66. $config = $this->getConfig();
  67. $data = ['deadline' => time() + $config['expire']];
  68. $signature = hash_hmac('sha1', json_encode($data), $config['secretKey'], true);
  69. $multipart = [
  70. 'costoken' => $config['appId'] . ':' . base64_encode($signature) . ':' . base64_encode(json_encode($data))
  71. ];
  72. $upload = array_merge($upload, [
  73. 'cdnurl' => $config['cdnurl'],
  74. 'uploadurl' => $config['uploadmode'] == 'client' ? $config['uploadurl'] : addon_url('cos/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' => $config['multiple'] ? true : false,
  85. ]);
  86. }
  87. /**
  88. * 附件删除后
  89. */
  90. public function uploadDelete($attachment)
  91. {
  92. $config = $this->getConfig();
  93. if ($attachment['storage'] == 'cos' && isset($config['syncdelete']) && $config['syncdelete']) {
  94. $cosConfig = array(
  95. 'region' => $config['region'],
  96. 'schema' => 'https', //协议头部,默认为http
  97. 'credentials' => array(
  98. 'secretId' => $config['secretId'],
  99. 'secretKey' => $config['secretKey']
  100. )
  101. );
  102. $oss = new Client($cosConfig);
  103. $ret = $oss->deleteObject(array('Bucket' => $config['bucket'], 'Key' => ltrim($attachment->url, '/')));
  104. }
  105. return true;
  106. }
  107. }