add more extensions to mimetype database. (#1417)

fallback to text/plain for 0 byte files
This commit is contained in:
Mike Sawka 2024-12-06 09:36:26 -08:00 committed by GitHub
parent 74bb5c7317
commit 9f10be5629
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View File

@ -572,6 +572,8 @@ var StaticMimeTypeMap = map[string]string{
".oeb": "application/vnd.openeye.oeb", ".oeb": "application/vnd.openeye.oeb",
".oxt": "application/vnd.openofficeorg.extension", ".oxt": "application/vnd.openofficeorg.extension",
".osm": "application/vnd.openstreetmap.data+xml", ".osm": "application/vnd.openstreetmap.data+xml",
".exe": "application/vnd.microsoft.portable-executable",
".dll": "application/vnd.microsoft.portable-executable",
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
".sldx": "application/vnd.openxmlformats-officedocument.presentationml.slide", ".sldx": "application/vnd.openxmlformats-officedocument.presentationml.slide",
".ppsx": "application/vnd.openxmlformats-officedocument.presentationml.slideshow", ".ppsx": "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
@ -1108,14 +1110,18 @@ var StaticMimeTypeMap = map[string]string{
".jsx": "text/jsx", ".jsx": "text/jsx",
".less": "text/less", ".less": "text/less",
".md": "text/markdown", ".md": "text/markdown",
".mdx": "text/mdx",
".m": "text/mips", ".m": "text/mips",
".miz": "text/mizar", ".miz": "text/mizar",
".n3": "text/n3", ".n3": "text/n3",
".txt": "text/plain", ".txt": "text/plain",
".conf": "text/plain",
".awk": "text/x-awk",
".provn": "text/provenance-notation", ".provn": "text/provenance-notation",
".rst": "text/prs.fallenstein.rst", ".rst": "text/prs.fallenstein.rst",
".tag": "text/prs.lines.tag", ".tag": "text/prs.lines.tag",
".rs": "text/x-rust", ".rs": "text/x-rust",
".ini": "text/x-ini",
".sass": "text/scss", ".sass": "text/scss",
".scss": "text/scss", ".scss": "text/scss",
".sgml": "text/SGML", ".sgml": "text/SGML",

View File

@ -620,6 +620,7 @@ func CopyToChannel(outputCh chan<- []byte, reader io.Reader) error {
// on error just returns "" // on error just returns ""
// does not return "application/octet-stream" as this is considered a detection failure // does not return "application/octet-stream" as this is considered a detection failure
// can pass an existing fileInfo to avoid re-statting the file // can pass an existing fileInfo to avoid re-statting the file
// falls back to text/plain for 0 byte files
func DetectMimeType(path string, fileInfo fs.FileInfo, extended bool) string { func DetectMimeType(path string, fileInfo fs.FileInfo, extended bool) string {
if fileInfo == nil { if fileInfo == nil {
statRtn, err := os.Stat(path) statRtn, err := os.Stat(path)
@ -648,6 +649,9 @@ func DetectMimeType(path string, fileInfo fs.FileInfo, extended bool) string {
if mimeType := mime.TypeByExtension(ext); mimeType != "" { if mimeType := mime.TypeByExtension(ext); mimeType != "" {
return mimeType return mimeType
} }
if fileInfo.Size() == 0 {
return "text/plain"
}
if !extended { if !extended {
return "" return ""
} }