新增单元测试验证图片重定向逻辑,优化前端预览链接样式,调整依赖配置

This commit is contained in:
2026-01-26 23:32:00 +08:00
parent 4795960fad
commit 97df85d4f8
4 changed files with 98 additions and 7 deletions

View File

@@ -172,9 +172,11 @@ func handleImageResponse(c *gin.Context, img *model.Image) {
if mode == "redirect" {
if selected.PublicURL != "" {
c.Redirect(http.StatusFound, selected.PublicURL)
} else if img.URLBase != "" {
// 兜底重定向到原始 Bing
bingURL := fmt.Sprintf("https://www.bing.com%s_%s.jpg", img.URLBase, selected.Variant)
c.Redirect(http.StatusFound, bingURL)
} else {
// 兜底重定向到原始 Bing (如果可能,但由于 URLBase 只有一部分,这里可能不工作)
// 这里我们更倾向于 local 转发,如果 PublicURL 为空
serveLocal(c, selected.StorageKey)
}
} else {
@@ -201,7 +203,9 @@ func formatMeta(img *model.Image) gin.H {
variants := []gin.H{}
for _, v := range img.Variants {
url := v.PublicURL
if cfg.API.Mode == "local" || url == "" {
if url == "" && cfg.API.Mode == "redirect" && img.URLBase != "" {
url = fmt.Sprintf("https://www.bing.com%s_%s.jpg", img.URLBase, v.Variant)
} else if cfg.API.Mode == "local" || url == "" {
url = fmt.Sprintf("%s/api/v1/image/date/%s?variant=%s&format=%s", cfg.Server.BaseURL, img.Date, v.Variant, v.Format)
}
variants = append(variants, gin.H{