refactor: if no db mimetype, manually use mimetype.DetectReader, and if none found fall back to application/octet-stream
This commit is contained in:
parent
8e9d2d0398
commit
09c9ab8614
21
api/s5/s5.go
21
api/s5/s5.go
|
@ -22,6 +22,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
|
|
||||||
_import "git.lumeweb.com/LumeWeb/portal/import"
|
_import "git.lumeweb.com/LumeWeb/portal/import"
|
||||||
|
|
||||||
"git.lumeweb.com/LumeWeb/portal/api/router"
|
"git.lumeweb.com/LumeWeb/portal/api/router"
|
||||||
|
@ -1991,10 +1993,27 @@ func (s *S5API) downloadFile(jc jape.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var mimeType string
|
||||||
|
|
||||||
if len(file.Mime()) > 0 {
|
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)
|
http.ServeContent(jc.ResponseWriter, jc.Request, file.Name(), file.Modtime(), file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue