perf(image): 图片列表默认加载缩略图
This commit is contained in:
@@ -247,7 +247,14 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) {
|
||||
}
|
||||
|
||||
storageStart := time.Now()
|
||||
object, err := h.storage.Get(c.Request.Context(), key)
|
||||
var object *Object
|
||||
var err error
|
||||
for _, readKey := range objectReadKeys(key) {
|
||||
object, err = h.storage.Get(c.Request.Context(), readKey)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
storageMS := float64(time.Since(storageStart).Microseconds()) / 1000
|
||||
if err != nil {
|
||||
fields := []zap.Field{
|
||||
@@ -308,6 +315,16 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// objectReadKeys 为图片变体提供原图回退,兼容启用缩略图之前上传的历史文件。
|
||||
func objectReadKeys(key string) []string {
|
||||
keys := []string{key}
|
||||
canonical := filekey.Canonical(key)
|
||||
if canonical != "" && canonical != key {
|
||||
keys = append(keys, canonical)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func isPublicObjectKey(key string) bool {
|
||||
for _, prefix := range []string{
|
||||
"home-banner/",
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestObjectReadKeysFallsBackToOriginalImage(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "original key",
|
||||
key: "listing/account.webp",
|
||||
want: []string{"listing/account.webp"},
|
||||
},
|
||||
{
|
||||
name: "thumbnail fallback",
|
||||
key: "listing/account.webp.thumb.jpg",
|
||||
want: []string{"listing/account.webp.thumb.jpg", "listing/account.webp"},
|
||||
},
|
||||
{
|
||||
name: "medium fallback",
|
||||
key: "listing/account.webp.medium.jpg",
|
||||
want: []string{"listing/account.webp.medium.jpg", "listing/account.webp"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := objectReadKeys(tt.key); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Fatalf("objectReadKeys(%q) = %#v, want %#v", tt.key, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user