From 2a95de4225c079d53c836a4efd74315c6b4b0c57 Mon Sep 17 00:00:00 2001 From: wikiboss Date: Sat, 17 Oct 2026 15:56:29 +0800 Subject: [PATCH] =?UTF-8?q?fix(upload):=20=E6=94=AF=E6=8C=81=E9=99=84?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E8=87=AA=E5=8A=A8=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E8=87=B3=E8=85=BE=E8=AE=AF=E4=BA=91COS=E5=8F=8ACDN=E9=A2=84?= =?UTF-8?q?=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/general/File.php | 4 ++ .../admin/view/general/file/index.html | 4 +- application/common/library/Upload.php | 59 +++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/application/admin/controller/general/File.php b/application/admin/controller/general/File.php index dae13aa..70f7c3e 100644 --- a/application/admin/controller/general/File.php +++ b/application/admin/controller/general/File.php @@ -45,10 +45,14 @@ class File extends Backend 'order' => $order ], $rows, $page); + $cosConfig = function_exists('get_addon_config') ? get_addon_config('cos') : []; + $cdnUrl = (!empty($cosConfig['cdnurl'])) ? rtrim($cosConfig['cdnurl'], '/') : ''; + if (!empty($result['list'])) { foreach ($result['list'] as &$v) { $v['checked'] = false; $v['showTools'] = false; + $v['fullurl'] = (!empty($cdnUrl) && stripos($v['url'], 'http') !== 0) ? ($cdnUrl . $v['url']) : cdnurl($v['url'], true); } } diff --git a/application/admin/view/general/file/index.html b/application/admin/view/general/file/index.html index 9ed3355..96fc5a2 100644 --- a/application/admin/view/general/file/index.html +++ b/application/admin/view/general/file/index.html @@ -300,7 +300,7 @@
- +
@@ -315,7 +315,7 @@
{/if} -
+
diff --git a/application/common/library/Upload.php b/application/common/library/Upload.php index f9c75d4..c43d39d 100644 --- a/application/common/library/Upload.php +++ b/application/common/library/Upload.php @@ -446,10 +446,69 @@ class Upload $attachment->data(array_filter($params)); $attachment->save(); + $this->syncToCos($attachment); + \think\Hook::listen("upload_after", $attachment); return $attachment; } + /** + * 同步文件到腾讯云COS + * @param Attachment $attachment + */ + protected function syncToCos($attachment) + { + try { + $addonInfo = get_addon_info('cos'); + if (!$addonInfo || empty($addonInfo['state'])) { + return; + } + $cosConfig = get_addon_config('cos'); + if (empty($cosConfig) || empty($cosConfig['secretId']) || empty($cosConfig['secretKey']) || empty($cosConfig['bucket'])) { + return; + } + + if (!class_exists('\Qcloud\Cos\Client')) { + $cosAddonPath = (defined('ADDON_PATH') ? ADDON_PATH : (ROOT_PATH . 'addons' . DS)) . 'cos' . DIRECTORY_SEPARATOR; + \think\Loader::addNamespace('Qcloud\Cos', $cosAddonPath . 'library' . DIRECTORY_SEPARATOR . 'Qcloud' . DIRECTORY_SEPARATOR . 'Cos' . DIRECTORY_SEPARATOR); + \think\Loader::addNamespace('GuzzleHttp\Command', $cosAddonPath . 'library' . DIRECTORY_SEPARATOR . 'Guzzle' . DIRECTORY_SEPARATOR . 'command' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR); + \think\Loader::addNamespace('GuzzleHttp\Command\Guzzle', $cosAddonPath . 'library' . DIRECTORY_SEPARATOR . 'Guzzle' . DIRECTORY_SEPARATOR . 'guzzle-services' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR); + \think\Loader::addNamespace('GuzzleHttp\UriTemplate', $cosAddonPath . 'library' . DIRECTORY_SEPARATOR . 'Guzzle' . DIRECTORY_SEPARATOR . 'uri-template' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR); + } + + $realPath = ROOT_PATH . 'public' . str_replace('/', DS, $attachment->url); + if (!file_exists($realPath)) { + return; + } + + $client = new \Qcloud\Cos\Client([ + 'region' => $cosConfig['region'], + 'schema' => 'https', + 'credentials' => [ + 'secretId' => $cosConfig['secretId'], + 'secretKey' => $cosConfig['secretKey'], + ] + ]); + + $key = ltrim($attachment->url, '/'); + $fileHandle = fopen($realPath, 'rb'); + $client->upload($cosConfig['bucket'], $key, $fileHandle); + if (is_resource($fileHandle)) { + fclose($fileHandle); + } + + $attachment->storage = 'cos'; + $attachment->save(); + + // 如果设置了不保留本地备份,则删除本地文件 + if (isset($cosConfig['serverbackup']) && $cosConfig['serverbackup'] == 0) { + @unlink($realPath); + } + } catch (\Exception $e) { + \think\Log::write('COS Upload Error: ' . $e->getMessage(), 'error'); + } + } + /** * 设置错误信息 * @param $msg