use gabriel-vasile/mimetype for mime type detection

This commit is contained in:
2026-11-10 18:33:01 -05:00
parent 75a346c5f9
commit 3cddec9aff
3 changed files with 14 additions and 8 deletions
+1
View File
@@ -3,6 +3,7 @@ module git.whoswhip.dev/whoswhip/minibin
go 1.27.1 go 1.27.1
require ( require (
github.com/gabriel-vasile/mimetype v1.4.16
github.com/glebarez/sqlite v1.11.0 github.com/glebarez/sqlite v1.11.0
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
gorm.io/gorm v1.31.2 gorm.io/gorm v1.31.2
+2
View File
@@ -1,5 +1,7 @@
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gabriel-vasile/mimetype v1.4.16 h1:5izNFuvA37st1dDRAcA7TtmngBmRgiIPDmTtiEvjx1E=
github.com/gabriel-vasile/mimetype v1.4.16/go.mod h1:3oFFwxX8kTklZwzIl2aQEDZT0lKY8xH92ST88bAwba4=
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
+11 -8
View File
@@ -17,6 +17,7 @@ import (
"time" "time"
"unicode/utf8" "unicode/utf8"
"github.com/gabriel-vasile/mimetype"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -271,17 +272,19 @@ func contentHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
} }
func detectMimeType(file io.ReadSeeker) (string, error) { func detectMimeType(file io.ReadSeeker) (string, error) {
var buffer [512]byte
n, readErr := io.ReadFull(file, buffer[:])
if _, err := file.Seek(0, io.SeekStart); err != nil { if _, err := file.Seek(0, io.SeekStart); err != nil {
return "", fmt.Errorf("rewind file: %w", err) return "", fmt.Errorf("rewind file: %w", err)
} }
if readErr != nil && readErr != io.EOF && readErr != io.ErrUnexpectedEOF {
return "", fmt.Errorf("read file header: %w", readErr) mtype, dtcErr := mimetype.DetectReader(file)
if _, err := file.Seek(0, io.SeekStart); err != nil {
return "", fmt.Errorf("rewind file: %w", err)
} }
// will replace later, http.DetectContentType doesnt detect as much as i want it to if dtcErr != nil {
// github.com/google/magika - ML based, working on bindings for go? requires cgo & onnxruntime return "", fmt.Errorf("detect MIME type: %w", dtcErr)
// github.com/gabriel-vasile/mimetype - native go, similar amount of labels to magika }
return http.DetectContentType(buffer[:n]), nil
return mtype.String(), nil
} }