From 0d1295096180fb09d54948621a41f96d2970d032 Mon Sep 17 00:00:00 2001 From: yml2213 Date: Wed, 4 Nov 2026 19:31:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(file):=20=E4=BF=AE=E5=A4=8D=E7=A7=AF?= =?UTF-8?q?=E5=88=86=E5=95=86=E5=9F=8E=E5=9B=BE=E7=89=87=E5=85=AC=E5=BC=80?= =?UTF-8?q?=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/modules/file/handler.go | 35 +++++++++++++------ backend/internal/modules/file/storage_test.go | 3 ++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/backend/internal/modules/file/handler.go b/backend/internal/modules/file/handler.go index f82586c..d59a136 100644 --- a/backend/internal/modules/file/handler.go +++ b/backend/internal/modules/file/handler.go @@ -94,15 +94,7 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) { response.BadRequest(c, "文件 key 不正确") return } - if publicOnly && - !strings.HasPrefix(key, "home-banner/") && - !strings.HasPrefix(key, "avatar/") && - !strings.HasPrefix(key, "announcement/") && - !strings.HasPrefix(key, "mohong/") && - !strings.HasPrefix(key, "crash/") && - !strings.HasPrefix(key, "aw-recycle/") && - !strings.HasPrefix(key, "cooperation-feedback/") && - !strings.HasPrefix(key, "support-qrcode/") { + if publicOnly && !isPublicObjectKey(key) { response.Error(c, http.StatusNotFound, "not_found", "文件不存在或暂不可访问") return } @@ -142,10 +134,33 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) { contentType = "application/octet-stream" } c.Header("Content-Type", contentType) - c.Header("Cache-Control", "private, max-age=300") + if publicOnly { + c.Header("Cache-Control", "public, max-age=86400") + } else { + c.Header("Cache-Control", "private, max-age=300") + } c.DataFromReader(http.StatusOK, object.Size, contentType, object.Reader, nil) } +func isPublicObjectKey(key string) bool { + for _, prefix := range []string{ + "home-banner/", + "avatar/", + "announcement/", + "mohong/", + "crash/", + "aw-recycle/", + "cooperation-feedback/", + "support-qrcode/", + "points-mall/", + } { + if strings.HasPrefix(key, prefix) { + return true + } + } + return false +} + func writeFileError(c *gin.Context, err error) { response.RecordError(c, err) switch { diff --git a/backend/internal/modules/file/storage_test.go b/backend/internal/modules/file/storage_test.go index e59fda2..1500344 100644 --- a/backend/internal/modules/file/storage_test.go +++ b/backend/internal/modules/file/storage_test.go @@ -95,6 +95,9 @@ func TestPointsMallSceneUsesPublicFileURL(t *testing.T) { if !strings.HasPrefix(got, "/api/public/files/object?") { t.Fatalf("points-mall file url = %q, want public file endpoint", got) } + if !isPublicObjectKey("points-mall/2026/11/04/example.webp") { + t.Fatal("points-mall object should be publicly readable") + } } func TestNormalizeSceneListingScreenshotAndOperatingExpense(t *testing.T) {