Fix: cache-control header in thumb redirect request

pull/247/head
HFO4 5 years ago
parent 4f0c2bbb29
commit 9fe451ad4b

@ -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.ThumbSizeCtx, [2]uint{w, h})
ctx = context.WithValue(ctx, fsctx.FileModelCtx, fs.FileTarget[0]) ctx = context.WithValue(ctx, fsctx.FileModelCtx, fs.FileTarget[0])
res, err := fs.Handler.Thumb(ctx, fs.FileTarget[0].SourceName) res, err := fs.Handler.Thumb(ctx, fs.FileTarget[0].SourceName)
if err == nil {
res.MaxAge = model.GetIntSetting("preview_timeout", 60)
}
// TODO 出错时重新生成缩略图 // TODO 出错时重新生成缩略图
return res, err return res, err

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models" model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/filesystem/response" "github.com/HFO4/cloudreve/pkg/filesystem/response"
"github.com/HFO4/cloudreve/pkg/util" "github.com/HFO4/cloudreve/pkg/util"
@ -32,6 +33,7 @@ func CreateTestImage() *os.File {
func TestFileSystem_GetThumb(t *testing.T) { func TestFileSystem_GetThumb(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
ctx := context.Background() ctx := context.Background()
cache.Set("setting_preview_timeout", "60", 0)
// 正常 // 正常
{ {

@ -9,6 +9,7 @@ type ContentResponse struct {
Redirect bool Redirect bool
Content RSCloser Content RSCloser
URL string URL string
MaxAge int
} }
// RSCloser 存储策略适配器返回的文件流有些策略需要带有Closer // RSCloser 存储策略适配器返回的文件流有些策略需要带有Closer

@ -3,6 +3,7 @@ package controllers
import "C" import "C"
import ( import (
"context" "context"
"fmt"
model "github.com/HFO4/cloudreve/models" model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem" "github.com/HFO4/cloudreve/pkg/filesystem"
"github.com/HFO4/cloudreve/pkg/filesystem/driver/local" "github.com/HFO4/cloudreve/pkg/filesystem/driver/local"
@ -126,6 +127,7 @@ func Thumb(c *gin.Context) {
} }
if resp.Redirect { if resp.Redirect {
c.Header("Cache-Control", fmt.Sprintf("max-age=%d", resp.MaxAge))
c.Redirect(http.StatusMovedPermanently, resp.URL) c.Redirect(http.StatusMovedPermanently, resp.URL)
return return
} }

Loading…
Cancel
Save