forked from Mirrors/gomuks
server/media: use fallback avatar for unsupported mime types
This commit is contained in:
parent
f0e3ec650d
commit
68356a9ef1
1 changed files with 19 additions and 11 deletions
|
@ -72,6 +72,9 @@ func (gmx *Gomuks) downloadMediaFromCache(ctx context.Context, w http.ResponseWr
|
||||||
} else if r.Header.Get("If-None-Match") == entry.ETag() {
|
} else if r.Header.Get("If-None-Match") == entry.ETag() {
|
||||||
w.WriteHeader(http.StatusNotModified)
|
w.WriteHeader(http.StatusNotModified)
|
||||||
return true
|
return true
|
||||||
|
} else if entry.MimeType != "" && r.URL.Query().Has("fallback") && !isAllowedAvatarMime(entry.MimeType) {
|
||||||
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
log := zerolog.Ctx(ctx)
|
log := zerolog.Ctx(ctx)
|
||||||
cacheFile, err := os.Open(gmx.cacheEntryToPath(entry.Hash[:]))
|
cacheFile, err := os.Open(gmx.cacheEntryToPath(entry.Hash[:]))
|
||||||
|
@ -130,30 +133,35 @@ type avatarResponseWriter struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
bgColor string
|
bgColor string
|
||||||
character string
|
character string
|
||||||
data []byte
|
|
||||||
errored bool
|
errored bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isAllowedAvatarMime(mime string) bool {
|
||||||
|
switch mime {
|
||||||
|
case "image/png", "image/jpeg", "image/gif", "image/webp":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (w *avatarResponseWriter) WriteHeader(statusCode int) {
|
func (w *avatarResponseWriter) WriteHeader(statusCode int) {
|
||||||
if statusCode != http.StatusOK {
|
if statusCode != http.StatusOK && statusCode != http.StatusNotModified {
|
||||||
w.data = []byte(fmt.Sprintf(fallbackAvatarTemplate, w.bgColor, w.character))
|
data := []byte(fmt.Sprintf(fallbackAvatarTemplate, w.bgColor, w.character))
|
||||||
w.Header().Set("Content-Type", "image/svg+xml")
|
w.Header().Set("Content-Type", "image/svg+xml")
|
||||||
w.Header().Set("Content-Length", strconv.Itoa(len(w.data)))
|
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
||||||
w.Header().Del("Content-Disposition")
|
w.Header().Del("Content-Disposition")
|
||||||
|
w.ResponseWriter.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.ResponseWriter.Write(data)
|
||||||
w.errored = true
|
w.errored = true
|
||||||
statusCode = http.StatusOK
|
return
|
||||||
}
|
}
|
||||||
w.ResponseWriter.WriteHeader(statusCode)
|
w.ResponseWriter.WriteHeader(statusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *avatarResponseWriter) Write(p []byte) (n int, err error) {
|
func (w *avatarResponseWriter) Write(p []byte) (n int, err error) {
|
||||||
if w.errored {
|
if w.errored {
|
||||||
if w.data != nil {
|
return len(p), nil
|
||||||
_, err = w.ResponseWriter.Write(w.data)
|
|
||||||
w.data = nil
|
|
||||||
}
|
|
||||||
n = len(p)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
return w.ResponseWriter.Write(p)
|
return w.ResponseWriter.Write(p)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue