From 09c9ab8614d499cfcc6ba6453cdb90bd4498def5 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 29 Mar 2024 11:56:30 -0400 Subject: [PATCH] refactor: if no db mimetype, manually use mimetype.DetectReader, and if none found fall back to application/octet-stream --- api/s5/s5.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/api/s5/s5.go b/api/s5/s5.go index 030a20a..ece7e5e 100644 --- a/api/s5/s5.go +++ b/api/s5/s5.go @@ -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) }