feat(upload): 添加华为 OBS 对象存储支持

- 在 addons.php 中添加了与华为 OBS 相关的钩子
- 新增了对华为 OBS 上传功能的实现,包括分片上传和合并
- 优化了上传参数处理和错误处理
- 支持客户端和服务端两种上传模式
This commit is contained in:
2025-04-26 15:49:23 +08:00
parent c6a4e1f5f6
commit 32612f3103
37 changed files with 18296 additions and 3 deletions

152
addons/hwobs/Hwobs.php Normal file
View File

@@ -0,0 +1,152 @@
<?php
namespace addons\hwobs;
use addons\hwobs\library\Auth;
use Obs\ObsClient;
use think\Addons;
use think\App;
use think\Loader;
/**
* 华为云存储插件
*/
class Hwobs extends Addons
{
/**
* 插件安装方法
* @return bool
*/
public function install()
{
return true;
}
/**
* 插件卸载方法
* @return bool
*/
public function uninstall()
{
return true;
}
/**
* 插件启用方法
* @return bool
*/
public function enable()
{
return true;
}
/**
* 插件禁用方法
* @return bool
*/
public function disable()
{
return true;
}
/**
* 判断是否来源于API上传
*/
public function moduleInit($request)
{
$config = $this->getConfig();
$module = strtolower($request->module());
if ($module == 'api' && ($config['apiupload'] ?? 0) &&
strtolower($request->controller()) == 'common' &&
strtolower($request->action()) == 'upload') {
request()->param('isApi', true);
App::invokeMethod(["\\addons\\hwobs\\controller\\Index", "upload"], ['isApi' => true]);
}
}
/**
* 上传配置初始化
*/
public function uploadConfigInit(&$upload)
{
$config = $this->getConfig();
$module = request()->module();
$module = $module ? strtolower($module) : 'index';
$data = ['deadline' => time() + $config['expire']];
$signature = hash_hmac('sha1', json_encode($data), $config['secretKey'], true);
$token = '';
if (Auth::isModuleAllow()) {
$token = $config['accessKey'] . ':' . base64_encode($signature) . ':' . base64_encode(json_encode($data));
}
$multipart = [
'hwobstoken' => $token
];
$upload = array_merge($upload, [
'cdnurl' => $config['cdnurl'],
'uploadurl' => $config['uploadmode'] == 'client' ? $config['uploadurl'] : addon_url('hwobs/index/upload', [], false, true),
'uploadmode' => $config['uploadmode'],
'bucket' => $config['bucket'],
'maxsize' => $config['maxsize'],
'mimetype' => $config['mimetype'],
'savekey' => $config['savekey'],
'chunking' => (bool)($config['chunking'] ?? $upload['chunking']),
'chunksize' => (int)($config['chunksize'] ?? $upload['chunksize']),
'multipart' => $multipart,
'storage' => $this->getName(),
'multiple' => (bool)$config['multiple'],
]);
}
/**
* 附件删除后
*/
public function uploadDelete($attachment)
{
$config = $this->getConfig();
if ($attachment['storage'] == 'hwobs' && isset($config['syncdelete']) && $config['syncdelete']) {
try {
//删除云储存文件
$config = get_addon_config('hwobs');
$obs = new ObsClient([
'key' => $config['accessKey'],
'secret' => $config['secretKey'],
'endpoint' => $config['endpoint']
]);
$result = $obs->deleteObject([
'Bucket' => $config['bucket'],
'Key' => ltrim($attachment->url, '/')
]);
} catch (\Exception $e) {
return false;
}
//如果是服务端中转,还需要删除本地文件
//if ($config['uploadmode'] == 'server') {
// $filePath = ROOT_PATH . 'public' . str_replace('/', DS, $attachment->url);
// if ($filePath) {
// @unlink($filePath);
// }
//}
}
return true;
}
/**
* 渲染命名空间配置信息
*/
public function appInit()
{
if (!class_exists('\Obs\ObsClient')) {
Loader::addNamespace('Obs', $this->addons_path . str_replace('/', DS, 'library/Obs/'));
}
}
}