AI代码生成工具
This commit is contained in:
97
app/Service/common/oss/OssRuntimeConfigService.php
Normal file
97
app/Service/common/oss/OssRuntimeConfigService.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\common\oss;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use App\Models\oss\OssConfigModel;
|
||||
use App\Service\common\FieldEncryptService;
|
||||
use App\Service\SystemConfigService;
|
||||
|
||||
/**
|
||||
* OSS 运行时配置:解析当前启用的存储配置并解密密钥,供上传工厂使用
|
||||
*/
|
||||
class OssRuntimeConfigService extends BaseService
|
||||
{
|
||||
public const CFG_ACTIVE_ID = 'oss_active_config_id';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// 上传链路可能在无登录上下文触发,不强制鉴权
|
||||
$this->isAuth = false;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前启用的明文配置
|
||||
* 为什么要解密:上传客户端需要明文 access/secret;库内仅存 AES 密文
|
||||
*
|
||||
* @return array{
|
||||
* id:int,driver:string,name:string,access_key:string,secret_key:string,
|
||||
* endpoint:string,region:string,bucket:string,domain:string,
|
||||
* path_prefix:string,extra_json:mixed
|
||||
* }
|
||||
*/
|
||||
public function getActiveConfig(): array
|
||||
{
|
||||
$sys = SystemConfigService::getInstance();
|
||||
$activeId = (int) $sys->getValue(self::CFG_ACTIVE_ID, '0');
|
||||
$row = null;
|
||||
if ($activeId > 0) {
|
||||
$row = OssConfigModel::where('id', $activeId)
|
||||
->where('deleted_at', 0)
|
||||
->where('status', 1)
|
||||
->first();
|
||||
}
|
||||
if (!$row) {
|
||||
// 回落 is_active 标记,避免仅写了表未写 system_config 时上传失败
|
||||
$row = OssConfigModel::where('is_active', 1)
|
||||
->where('deleted_at', 0)
|
||||
->where('status', 1)
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
}
|
||||
if (!$row) {
|
||||
// 最终回落本地默认行或内存默认
|
||||
$row = OssConfigModel::where('driver', 'local')
|
||||
->where('deleted_at', 0)
|
||||
->where('status', 1)
|
||||
->orderByDesc('is_active')
|
||||
->orderBy('id')
|
||||
->first();
|
||||
}
|
||||
if (!$row) {
|
||||
return [
|
||||
'id' => 0,
|
||||
'driver' => 'local',
|
||||
'name' => '本地存储',
|
||||
'access_key' => '',
|
||||
'secret_key' => '',
|
||||
'endpoint' => '',
|
||||
'region' => '',
|
||||
'bucket' => '',
|
||||
'domain' => '',
|
||||
'path_prefix' => 'uploads',
|
||||
'extra_json' => null,
|
||||
];
|
||||
}
|
||||
$enc = FieldEncryptService::getInstance();
|
||||
$extra = $row->extra_json;
|
||||
if (is_string($extra) && $extra !== '') {
|
||||
$decoded = json_decode($extra, true);
|
||||
$extra = is_array($decoded) ? $decoded : $extra;
|
||||
}
|
||||
return [
|
||||
'id' => (int) $row->id,
|
||||
'driver' => (string) $row->driver,
|
||||
'name' => (string) $row->name,
|
||||
'access_key' => $enc->decryptFromStorage((string) ($row->access_key ?? ''), true),
|
||||
'secret_key' => $enc->decryptFromStorage((string) ($row->secret_key ?? ''), true),
|
||||
'endpoint' => (string) ($row->endpoint ?? ''),
|
||||
'region' => (string) ($row->region ?? ''),
|
||||
'bucket' => (string) ($row->bucket ?? ''),
|
||||
'domain' => (string) ($row->domain ?? ''),
|
||||
'path_prefix' => (string) ($row->path_prefix ?? ''),
|
||||
'extra_json' => $extra,
|
||||
];
|
||||
}
|
||||
}
|
||||
47
app/Service/common/oss/OssStorageFactory.php
Normal file
47
app/Service/common/oss/OssStorageFactory.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\common\oss;
|
||||
|
||||
use App\BaseApp\BaseNotAuthService;
|
||||
use App\Enum\OssDriverEnum;
|
||||
use App\Service\common\upload\AliyunStorageService;
|
||||
use App\Service\common\upload\LocalhostStorageService;
|
||||
use App\Service\common\upload\QcloudStorageService;
|
||||
use App\Service\common\upload\QiniuStorageService;
|
||||
use App\Service\common\upload\S3CompatibleStorageService;
|
||||
use App\Service\common\UtilsService;
|
||||
|
||||
/**
|
||||
* OSS 存储工厂:按 OssDriverEnum 分发到对应实现(工厂 + 策略)
|
||||
* 覆盖:local / aliyun / qcloud / qiniu / huawei / aws / minio / baidu
|
||||
*/
|
||||
class OssStorageFactory extends BaseNotAuthService
|
||||
{
|
||||
/**
|
||||
* 根据明文运行时配置构建上传客户端
|
||||
* 为什么集中在工厂:UploadService 不关心具体 SDK/签名差异,只拿接口用
|
||||
*
|
||||
* @param array $config getActiveConfig() 返回值(含 driver)
|
||||
*/
|
||||
public function make(array $config): OssStorageInterface
|
||||
{
|
||||
$driverCode = strtolower(trim((string) ($config['driver'] ?? OssDriverEnum::Local->value)));
|
||||
$driver = OssDriverEnum::tryFrom($driverCode);
|
||||
if ($driver === null) {
|
||||
UtilsService::getInstance()->errorThrow(
|
||||
'不支持的存储驱动:' . $driverCode . ',请改用本地存储或完善配置'
|
||||
);
|
||||
}
|
||||
// 按驱动枚举创建策略实现;S3 兼容族共用一套 SigV4 客户端
|
||||
return match ($driver) {
|
||||
OssDriverEnum::Local => LocalhostStorageService::getInstance()->withConfig($config),
|
||||
OssDriverEnum::Aliyun => AliyunStorageService::getInstance()->withConfig($config),
|
||||
OssDriverEnum::Qcloud => QcloudStorageService::getInstance()->withConfig($config),
|
||||
OssDriverEnum::Qiniu => QiniuStorageService::getInstance()->withConfig($config),
|
||||
OssDriverEnum::Huawei,
|
||||
OssDriverEnum::Aws,
|
||||
OssDriverEnum::Minio,
|
||||
OssDriverEnum::Baidu => S3CompatibleStorageService::getInstance()->withConfig($config),
|
||||
};
|
||||
}
|
||||
}
|
||||
34
app/Service/common/oss/OssStorageInterface.php
Normal file
34
app/Service/common/oss/OssStorageInterface.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\common\oss;
|
||||
|
||||
/**
|
||||
* OSS 存储驱动统一契约:工厂按 driver 返回实现类,UploadService 只依赖本接口
|
||||
*/
|
||||
interface OssStorageInterface
|
||||
{
|
||||
/**
|
||||
* 注入本请求的运行时配置(密钥已解密)
|
||||
*
|
||||
* @param array $config OssRuntimeConfigService::getActiveConfig()
|
||||
*/
|
||||
public function withConfig(array $config): static;
|
||||
|
||||
/**
|
||||
* 上传图片对象
|
||||
*
|
||||
* @param mixed $filePath 本地路径或 UploadedFile
|
||||
* @param string $key 对象键
|
||||
* @return array{key:string,url:string}|false
|
||||
*/
|
||||
public function uploadImage($filePath, string $key): bool|array;
|
||||
|
||||
/**
|
||||
* 上传视频/通用文件对象
|
||||
*
|
||||
* @param mixed $filePath 本地路径或 UploadedFile
|
||||
* @param string $key 对象键
|
||||
* @return array{key:string,url:string}|false
|
||||
*/
|
||||
public function uploadVideo($filePath, string $key): bool|array;
|
||||
}
|
||||
Reference in New Issue
Block a user