diff --git a/go.mod b/go.mod index fcf84146..466bc799 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,6 @@ require ( github.com/docker/go-connections v0.5.0 github.com/fatih/color v1.17.0 github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf - github.com/gabriel-vasile/mimetype v1.4.5 github.com/gammazero/workerpool v1.1.3 github.com/gbrlsnchs/jwt/v3 v3.0.1 github.com/gin-gonic/gin v1.10.0 @@ -69,6 +68,7 @@ require ( github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gammazero/deque v0.2.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/glebarez/go-sqlite v1.22.0 // indirect diff --git a/go.sum b/go.sum index 6b8d2a1d..e45aec77 100644 --- a/go.sum +++ b/go.sum @@ -107,6 +107,8 @@ github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSw github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf h1:NrF81UtW8gG2LBGkXFQFqlfNnvMt9WdB46sfdJY4oqc= github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf/go.mod h1:VzmDKDJVZI3aJmnRI9VjAn9nJ8qPPsN1fqzr9dqInIo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= github.com/gammazero/deque v0.2.1 h1:qSdsbG6pgp6nL7A0+K/B7s12mcCY/5l5SIUpMOl+dC0= diff --git a/server/filesystem/filesystem.go b/server/filesystem/filesystem.go index 42c56f2d..eb2ca0cd 100644 --- a/server/filesystem/filesystem.go +++ b/server/filesystem/filesystem.go @@ -3,6 +3,7 @@ package filesystem import ( "fmt" "io" + "mime" "os" "path/filepath" "slices" @@ -13,8 +14,6 @@ import ( "time" "emperror.dev/errors" - "github.com/apex/log" - "github.com/gabriel-vasile/mimetype" ignore "github.com/sabhiram/go-gitignore" "github.com/pterodactyl/wings/config" @@ -433,28 +432,21 @@ func (fs *Filesystem) ListDirectory(p string) ([]Stat, error) { if e.Type().IsDir() { d = "inode/directory" } else { - d = "application/octet-stream" + d = "text/plain" } - var m *mimetype.MIME + var m = "" if e.Type().IsRegular() { - // TODO: I should probably find a better way to do this. - eO := e.(interface { - Open() (ufs.File, error) - }) - f, err := eO.Open() - if err != nil { - return Stat{}, err + // Get mimetype from file extension + splitted := strings.Split(e.Name(), ".") + if len(splitted) >= 2 { + fileExtension := splitted[len(splitted)-1] + m = mime.TypeByExtension("." + fileExtension) } - m, err = mimetype.DetectReader(f) - if err != nil { - log.Error(err.Error()) - } - _ = f.Close() } st := Stat{FileInfo: info, Mimetype: d} - if m != nil { - st.Mimetype = m.String() + if m != "" { + st.Mimetype = m } return st, nil }) diff --git a/server/filesystem/stat.go b/server/filesystem/stat.go index 94cab60b..f2e57d67 100644 --- a/server/filesystem/stat.go +++ b/server/filesystem/stat.go @@ -3,11 +3,11 @@ package filesystem import ( "encoding/json" "io" + "mime" "strconv" + "strings" "time" - "github.com/gabriel-vasile/mimetype" - "github.com/pterodactyl/wings/internal/ufs" ) @@ -48,12 +48,15 @@ func statFromFile(f ufs.File) (Stat, error) { if err != nil { return Stat{}, err } - var m *mimetype.MIME + var m = "" if !s.IsDir() { - m, err = mimetype.DetectReader(f) - if err != nil { - return Stat{}, err + // Get mimetype from file extension + splitted := strings.Split(f.Name(), ".") + if len(splitted) >= 2 { + fileExtension := splitted[len(splitted)-1] + m = mime.TypeByExtension("." + fileExtension) } + if _, err := f.Seek(0, io.SeekStart); err != nil { return Stat{}, err } @@ -62,8 +65,8 @@ func statFromFile(f ufs.File) (Stat, error) { FileInfo: s, Mimetype: "inode/directory", } - if m != nil { - st.Mimetype = m.String() + if m != "" { + st.Mimetype = m } return st, nil }