Skip to content

Commit 860b1a3

Browse files
committed
update img header
1 parent a8c2982 commit 860b1a3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

internal/base/middleware/avatar.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/url"
66
"os"
7+
"path"
78
"path/filepath"
89
"strings"
910

@@ -53,13 +54,26 @@ func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc {
5354
ctx.Next()
5455
return
5556
}
57+
ext := strings.ToLower(path.Ext(filePath)[1:])
58+
ctx.Header("content-type", fmt.Sprintf("image/%s", ext))
5659
_, err = ctx.Writer.WriteString(string(avatarfile))
5760
if err != nil {
5861
log.Error(err)
5962
}
6063
ctx.Abort()
6164
return
6265

66+
} else {
67+
uUrl, err := url.Parse(u)
68+
if err != nil {
69+
ctx.Next()
70+
return
71+
}
72+
_, urlfileName := filepath.Split(uUrl.Path)
73+
uploadPath := am.serviceConfig.UploadPath
74+
filePath := fmt.Sprintf("%s/%s", uploadPath, urlfileName)
75+
ext := strings.ToLower(path.Ext(filePath)[1:])
76+
ctx.Header("content-type", fmt.Sprintf("image/%s", ext))
6377
}
6478
ctx.Next()
6579
}

0 commit comments

Comments
 (0)