refactor: if no db mimetype, manually use mimetype.DetectReader, and if none found fall back to application/octet-stream

This commit is contained in:
Derrick Hammer 2024-03-29 11:56:30 -04:00
parent 8e9d2d0398
commit 09c9ab8614
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 20 additions and 1 deletions

View File

@ -22,6 +22,8 @@ import (
"strconv"
"strings"
"github.com/gabriel-vasile/mimetype"
_import "git.lumeweb.com/LumeWeb/portal/import"
"git.lumeweb.com/LumeWeb/portal/api/router"
@ -1991,10 +1993,27 @@ func (s *S5API) downloadFile(jc jape.Context) {
return
}
var mimeType string
if len(file.Mime()) > 0 {
jc.ResponseWriter.Header().Set("Content-Type", file.Mime())
mimeType = file.Mime()
}
if len(mimeType) == 0 {
detectedType, err := mimetype.DetectReader(file)
if err != nil {
s.logger.Error("error detecting mime type", zap.Error(err))
}
_ = jc.Error(err, http.StatusInternalServerError)
mimeType = detectedType.String()
}
if len(mimeType) == 0 {
mimeType = "application/octet-stream"
}
jc.ResponseWriter.Header().Set("Content-Type", mimeType)
http.ServeContent(jc.ResponseWriter, jc.Request, file.Name(), file.Modtime(), file)
}