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:
@@ -0,0 +1,46 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 生产口径 key 结构:`YYYY/MM/<UnixNano><ext>`,无前缀、无 day 层。
|
||||
// 与 route/question/page.go:uploadImage 的落盘 key 保持一致。
|
||||
var prodKeyRe = regexp.MustCompile(`^\d{4}/\d{2}/\d{16,19}\.jpg$`)
|
||||
|
||||
func TestUploadKeyFormat(t *testing.T) {
|
||||
key := uploadKey(".jpg")
|
||||
if !prodKeyRe.MatchString(key) {
|
||||
t.Fatalf("uploadKey(.jpg) = %q, 不符合生产口径 YYYY/MM/<UnixNano><ext>", key)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtFromContentType(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"image/jpeg": ".jpg",
|
||||
"image/png": ".png",
|
||||
"image/gif": ".gif",
|
||||
"image/webp": ".webp",
|
||||
"image/bmp": "",
|
||||
}
|
||||
for ct, want := range cases {
|
||||
if got := extFromContentType(ct); got != want {
|
||||
t.Errorf("extFromContentType(%q) = %q, want %q", ct, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOriginalExt(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"photo.JPG": ".jpg",
|
||||
"a.b.png": ".png",
|
||||
"noext": "",
|
||||
"tricky.PnG": ".png",
|
||||
}
|
||||
for name, want := range cases {
|
||||
if got := originalExt(name); got != want {
|
||||
t.Errorf("originalExt(%q) = %q, want %q", name, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user