storage: 对齐图片对象 key 到生产口径

生产真实 key 为 `YYYY/MM/<UnixNano>.<ext>`(无前缀、无 day 层),
与 route/question/page.go:uploadImage 一致;internal/storage 里三个
上传函数仍用旧的 `picture/ 前缀 + YYYY/MM/DD + 15hex + 无扩展名`,从未上线。

- PictureKeyPrefix 置空,统一由 uploadKey 生成 key
- 扩展名跟随最终 ContentType(webp 转成功用 .webp,回退原图用原格式)
- OSS 原图直传通道取原文件名扩展名
- 新增 picture_test.go 锁定 key 格式,防回归
This commit is contained in:
tamakyi
2026-10-29 20:04:31 +08:00
parent c6966138b4
commit 08bbd17751
4 changed files with 97 additions and 19 deletions
+4 -9
View File
@@ -16,7 +16,6 @@ import (
"mime/multipart"
"os"
"os/exec"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
@@ -24,7 +23,6 @@ import (
"github.com/disintegration/imaging"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/thanhpk/randstr"
"github.com/wuhan005/gadget"
"github.com/tamakyi/TamaBox/internal/conf"
@@ -181,12 +179,9 @@ func uploadPictureToS3(file multipart.File, fileHeader *multipart.FileHeader, as
}
}
// 5. 生成存储路径(无后缀名)
now := time.Now()
year := now.Year()
month := int(now.Month())
day := now.Day()
key := fmt.Sprintf("%s%d/%02d/%02d/%s", PictureKeyPrefix, year, month, day, randstr.Hex(15))
// 5. 生成存储路径:`YYYY/MM/<UnixNano><ext>`(生产口径,无前缀、无 day 层)。
// 扩展名跟随最终落盘内容类型,保证 key 扩展名与 ContentType 严格一致。
key := uploadKey(extFromContentType(contentType))
// 6. 获取共享 S3 客户端
client, err := GetS3Client()
@@ -202,7 +197,7 @@ func uploadPictureToS3(file multipart.File, fileHeader *multipart.FileHeader, as
Body: bytes.NewReader(uploadData),
ContentLength: aws.Int64(int64(len(uploadData))),
ContentType: aws.String(contentType),
// key 为 picture/日期/随机hex,内容不可变 → 可安全启用 immutable 强缓存,
// key 含纳秒时间戳且内容不可变 → 可安全启用 immutable 强缓存,
// 避免每次页面加载都回源校验(曾导致头像/二维码 logo 每次重新下载)。
CacheControl: aws.String("public, max-age=31536000, immutable"),
})