switched from h2non/filetype to garbiel-vasile/mimetype for determining file type

This commit is contained in:
Johannes Bülow 2025-06-13 16:19:19 +02:00
parent fca36e9b02
commit 2081998ca9
Signed by: jmb
GPG key ID: B56971CF7B8F83A6
4 changed files with 24 additions and 21 deletions

2
go.mod
View file

@ -17,6 +17,7 @@ require (
require (
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.9 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
@ -29,6 +30,7 @@ require (
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect

4
go.sum
View file

@ -9,6 +9,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY=
github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
@ -66,6 +68,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=

View file

@ -46,6 +46,7 @@ func setDefaults() {
viper.SetDefault("db.debug", false)
// Others
viper.SetDefault("processing.oleurl", "http://localhost:5000")
viper.SetDefault("processing.maxmimesize", "0")
viper.SetDefault("store.path", "./storage/files/")
viper.SetDefault("debug", false)
// UI Interface info

View file

@ -1,41 +1,37 @@
package store
import (
"github.com/h2non/filetype"
"log/slog"
"os"
"time"
"github.com/gabriel-vasile/mimetype"
"github.com/spf13/viper"
)
// Returns the MIME type of a file
func GetFileType(fileId string) (string, error) {
start := time.Now()
path, err := AbsPath(fileId)
if err != nil {
return "application/octet-stream", nil
}
file, err := os.Open(path)
mimetype.SetLimit(uint32(viper.GetSizeInBytes("processing.maxmimesize")))
mtype, err := mimetype.DetectFile(path)
if err != nil {
slog.Error("Storage not accessible", "error", err, "fileid", fileId)
slog.Error("Storage not accessible", "error", err, "file-uuid", fileId, "mimetype", mtype.String())
return "application/octet-stream", err
}
// We only have to pass the file header = first 261 bytes
head := make([]byte, 261)
file.Read(head)
return GetBytesFileType(head)
slog.Debug("store.GetFileType", "file-uuid", fileId, "mimetype", mtype.String(), "extension", mtype.Extension(), "duration", time.Now().Sub(start).String())
return mtype.String(), nil
}
// Returns the MimeType for a []byte
// We only have to pass the file header = first 261 bytes
func GetBytesFileType(data []byte) (string, error) {
kind, err := filetype.Match(data)
slog.Debug("GetBytesFileType", "data", data, "file-mime", kind.MIME.Value)
if err != nil {
slog.Error("Could not determine file type", "error", err)
return "application/octet-stream", err
}
if kind == filetype.Unknown {
return "application/octet-stream", nil
}
// Only pass the necessary data!
func GetBytesFileType(data []byte) (string) {
mimetype.SetLimit(uint32(viper.GetSizeInBytes("processing.maxmimesize")))
mtype := mimetype.Detect(data)
slog.Debug("GetBytesFileType", "data", data, "file-mime", mtype.String())
return kind.MIME.Value, nil
return mtype.String()
}