diff --git a/pkg/filesystem/image.go b/pkg/filesystem/image.go index 558f7d4..d32335b 100644 --- a/pkg/filesystem/image.go +++ b/pkg/filesystem/image.go @@ -33,6 +33,9 @@ func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentR ctx = context.WithValue(ctx, fsctx.ThumbSizeCtx, [2]uint{w, h}) ctx = context.WithValue(ctx, fsctx.FileModelCtx, fs.FileTarget[0]) res, err := fs.Handler.Thumb(ctx, fs.FileTarget[0].SourceName) + if err == nil { + res.MaxAge = model.GetIntSetting("preview_timeout", 60) + } // TODO 出错时重新生成缩略图 return res, err diff --git a/pkg/filesystem/image_test.go b/pkg/filesystem/image_test.go index 0381530..23bdf15 100644 --- a/pkg/filesystem/image_test.go +++ b/pkg/filesystem/image_test.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/DATA-DOG/go-sqlmock" model "github.com/HFO4/cloudreve/models" + "github.com/HFO4/cloudreve/pkg/cache" "github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/filesystem/response" "github.com/HFO4/cloudreve/pkg/util" @@ -32,6 +33,7 @@ func CreateTestImage() *os.File { func TestFileSystem_GetThumb(t *testing.T) { asserts := assert.New(t) ctx := context.Background() + cache.Set("setting_preview_timeout", "60", 0) // 正常 { diff --git a/pkg/filesystem/response/common.go b/pkg/filesystem/response/common.go index 02a93bc..d0b74fa 100644 --- a/pkg/filesystem/response/common.go +++ b/pkg/filesystem/response/common.go @@ -9,6 +9,7 @@ type ContentResponse struct { Redirect bool Content RSCloser URL string + MaxAge int } // RSCloser 存储策略适配器返回的文件流,有些策略需要带有Closer diff --git a/routers/controllers/file.go b/routers/controllers/file.go index 87a22e1..224f57b 100644 --- a/routers/controllers/file.go +++ b/routers/controllers/file.go @@ -3,6 +3,7 @@ package controllers import "C" import ( "context" + "fmt" model "github.com/HFO4/cloudreve/models" "github.com/HFO4/cloudreve/pkg/filesystem" "github.com/HFO4/cloudreve/pkg/filesystem/driver/local" @@ -126,6 +127,7 @@ func Thumb(c *gin.Context) { } if resp.Redirect { + c.Header("Cache-Control", fmt.Sprintf("max-age=%d", resp.MaxAge)) c.Redirect(http.StatusMovedPermanently, resp.URL) return }