Qiniu.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace modules\qiniu;
  3. use ba\module\moduleException;
  4. use ba\module\Server;
  5. use ba\Version;
  6. use FilesystemIterator;
  7. use RecursiveDirectoryIterator;
  8. use RecursiveIteratorIterator;
  9. use think\App;
  10. use Qiniu\Auth;
  11. use think\Exception;
  12. use think\facade\Cache;
  13. use think\facade\Event;
  14. use app\common\model\Config;
  15. use app\common\model\Attachment;
  16. class Qiniu
  17. {
  18. private $uid = 'qiniu';
  19. public function AppInit()
  20. {
  21. // 上传配置初始化
  22. Event::listen('uploadConfigInit', function (App $app) {
  23. $uploadConfig = get_sys_config('', 'upload');
  24. if ($uploadConfig['upload_mode'] == 'qiniu') {
  25. $auth = new Auth($uploadConfig['upload_access_key'], $uploadConfig['upload_secret_key']);
  26. $upToken = $auth->uploadToken($uploadConfig['upload_bucket']);
  27. $app->request->upload = [
  28. 'cdn' => $uploadConfig['upload_cdn_url'],
  29. 'mode' => $uploadConfig['upload_mode'],
  30. 'url' => $uploadConfig['upload_url'],
  31. 'params' => [
  32. 'token' => $upToken,
  33. ]
  34. ];
  35. }
  36. });
  37. // 附件管理中删除了文件
  38. Event::listen('AttachmentDel', function (Attachment $attachment) {
  39. if ($attachment->storage != 'qiniu') {
  40. return true;
  41. }
  42. $uploadConfig = get_sys_config('', 'upload');
  43. if (!$uploadConfig['upload_access_key'] || !$uploadConfig['upload_secret_key'] || !$uploadConfig['upload_bucket']) {
  44. return true;
  45. }
  46. $auth = new Auth($uploadConfig['upload_access_key'], $uploadConfig['upload_secret_key']);
  47. $config = new \Qiniu\Config();
  48. $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
  49. $url = str_replace(full_url(), '', ltrim($attachment->url, '/'));
  50. $bucketManager->delete($uploadConfig['upload_bucket'], $url);
  51. return true;
  52. });
  53. }
  54. public function enable()
  55. {
  56. Config::addConfigGroup('upload', '上传配置');
  57. if (!Config::where('name', 'upload_mode')->value('id')) {
  58. // 配置数据曾在禁用时被删除
  59. Server::importSql(root_path() . 'modules' . DIRECTORY_SEPARATOR . $this->uid . DIRECTORY_SEPARATOR);
  60. }
  61. // 恢复缓存中的配置数据
  62. if (Version::compare('v1.1.0', \think\facade\Config::get('buildadmin.version'))) {
  63. $config = Cache::pull($this->uid . '-module-config');
  64. if ($config) {
  65. $config = json_decode($config, true);
  66. foreach ($config as $item) {
  67. Config::where('name', $item['name'])->update([
  68. 'value' => $item['value']
  69. ]);
  70. }
  71. }
  72. }
  73. }
  74. public function disable()
  75. {
  76. $config = Config::whereIn('name', ['upload_mode', 'upload_bucket', 'upload_access_key', 'upload_secret_key', 'upload_url', 'upload_cdn_url'])->select();
  77. // 备份配置到缓存
  78. if (Version::compare('v1.1.0', \think\facade\Config::get('buildadmin.version'))) {
  79. if (!$config->isEmpty()) {
  80. $configData = $config->toArray();
  81. Cache::set($this->uid . '-module-config', json_encode($configData), 3600);
  82. }
  83. }
  84. foreach ($config as $item) {
  85. $item->delete();
  86. }
  87. Config::removeConfigGroup('upload');
  88. }
  89. public function update()
  90. {
  91. // 兼容系统v1.1.2
  92. // 寻找安装时备份中的baInput.ts文件,如果有,还原到mixins内
  93. $ebakDir = root_path() . 'modules' . DIRECTORY_SEPARATOR . 'ebak' . DIRECTORY_SEPARATOR;
  94. $zipFile = $ebakDir . $this->uid . '-install.zip';
  95. $zipDir = false;
  96. if (is_file($zipFile)) {
  97. try {
  98. $zipDir = Server::unzip($zipFile);
  99. } catch (moduleException|Exception $e) {
  100. // skip
  101. }
  102. }
  103. if ($zipDir) {
  104. $oldBaInput = path_transform('web\src\components\baInput\components\baUpload.ts');
  105. @unlink(root_path() . $oldBaInput);
  106. foreach (
  107. $iterator = new RecursiveIteratorIterator(
  108. new RecursiveDirectoryIterator($zipDir, FilesystemIterator::SKIP_DOTS),
  109. RecursiveIteratorIterator::SELF_FIRST
  110. ) as $item
  111. ) {
  112. $ebakFile = path_transform($iterator->getSubPathName());
  113. if (!$item->isDir() && $ebakFile == $oldBaInput) {
  114. copy($item, path_transform(root_path() . 'web/src/components/mixins/baUpload.ts'));
  115. }
  116. }
  117. }
  118. deldir($zipDir);
  119. }
  120. }