From 860b1a3bd8cfaa8827e6e6f50ab1d98fa4c2c816 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Tue, 7 Feb 2023 17:55:31 +0800 Subject: [PATCH] update img header --- internal/base/middleware/avatar.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/base/middleware/avatar.go b/internal/base/middleware/avatar.go index 66a72f68e..d38e76c9b 100644 --- a/internal/base/middleware/avatar.go +++ b/internal/base/middleware/avatar.go @@ -4,6 +4,7 @@ import ( "fmt" "net/url" "os" + "path" "path/filepath" "strings" @@ -53,6 +54,8 @@ func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc { ctx.Next() return } + ext := strings.ToLower(path.Ext(filePath)[1:]) + ctx.Header("content-type", fmt.Sprintf("image/%s", ext)) _, err = ctx.Writer.WriteString(string(avatarfile)) if err != nil { log.Error(err) @@ -60,6 +63,17 @@ func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc { ctx.Abort() return + } else { + uUrl, err := url.Parse(u) + if err != nil { + ctx.Next() + return + } + _, urlfileName := filepath.Split(uUrl.Path) + uploadPath := am.serviceConfig.UploadPath + filePath := fmt.Sprintf("%s/%s", uploadPath, urlfileName) + ext := strings.ToLower(path.Ext(filePath)[1:]) + ctx.Header("content-type", fmt.Sprintf("image/%s", ext)) } ctx.Next() }