更新若干功能

This commit is contained in:
2026-08-19 08:16:49 +08:00
parent 4979bb83d2
commit 72bc6502eb
56 changed files with 3627 additions and 343 deletions

View File

@@ -5,14 +5,26 @@ namespace App\Service\common\upload;
/**
* 对象键规整
*
* 五个驱动在列举 / 删除时对 key 的处理完全一致(补 path_prefix、压斜杠、拼 domain
* 抽出来免得同一段逻辑在五个文件里各写一遍、各改一半
* 上传用 normalizeObjectKey可补 path_prefix
* 列举 / 删除用 normalizeListedKey只压斜杠用服务端真实 key
* 使用方需要有 $this->config OssRuntimeConfigService 注入,含 path_prefix / domain
*/
trait ObjectKeyNormalizeTrait
{
/**
* path_prefix 并压平重复斜杠
* 压平重复斜杠,不 path_prefix
*
* 列举结果必须走这里OSS 回的 key 就是对象真实路径。
* 若再套一层 path_prefix根目录的历史文件 b_xxx.jpg会被改写成
* uploads/b_xxx.jpg素材库入库地址全 404
*/
protected function normalizeListedKey(string $key): string
{
return ltrim((string) preg_replace('#/{2,}#', '/', trim($key)), '/');
}
/**
* 上传 / 删除用:缺 path_prefix 时补上,并压平重复斜杠
*
* 为什么必须压平uploads//spa/a.jpg 与 uploads/spa/a.jpg 在 OSS 上是两个对象,
* 但拼出来的访问地址会被 CDN 归一成同一个。素材库按 path 建索引,
@@ -20,7 +32,7 @@ trait ObjectKeyNormalizeTrait
*/
protected function normalizeObjectKey(string $key): string
{
$key = ltrim((string) preg_replace('#/{2,}#', '/', trim($key)), '/');
$key = $this->normalizeListedKey($key);
if ($key === '') {
return '';
}
@@ -32,19 +44,16 @@ trait ObjectKeyNormalizeTrait
}
/**
* 列举前缀:调用方没给就退回 path_prefix
* 列举前缀:只认调用方传入的值,留空 = 扫整个 Bucket
*
* 同一个 bucket 常常被多个项目共用,不加这层兜底会把别人的对象也拉进素材库
* 之后走回收流程就等于跨项目删文件
* 以前空前缀会偷偷套配置里的 path_prefix。种子数据把七牛写成 uploads
* 而本站历史文件都在根目录b_*.jpg一拉就是空列表
* 素材库文案也是「留空表示整个 Bucket」这里必须跟文案一致
* 要收窄范围请在同步抽屉里显式填前缀。
*/
protected function scopedListPrefix(string $prefix): string
{
$prefix = trim($prefix);
if ($prefix !== '') {
return $this->normalizeObjectKey($prefix);
}
$base = trim((string) ($this->config['path_prefix'] ?? ''), '/');
return $base === '' ? '' : $base . '/';
return $this->normalizeListedKey($prefix);
}
/**