From 5da4ca1ee066a2bae29a3322863830a99833ed56 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Tue, 20 Aug 2024 14:20:52 +0200 Subject: [PATCH 1/6] Get mimetype from file extension rather than from file content --- go.mod | 2 +- server/filesystem/filesystem.go | 26 ++++++++------------------ server/filesystem/stat.go | 19 ++++++++++--------- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 1950bac4..7110c3d1 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,6 @@ require ( github.com/docker/go-connections v0.5.0 github.com/fatih/color v1.16.0 github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf - github.com/gabriel-vasile/mimetype v1.4.3 github.com/gammazero/workerpool v1.1.3 github.com/gbrlsnchs/jwt/v3 v3.0.1 github.com/gin-gonic/gin v1.9.1 @@ -67,6 +66,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/server/filesystem/filesystem.go b/server/filesystem/filesystem.go index 42c56f2d..b32b18d9 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" @@ -435,26 +434,17 @@ func (fs *Filesystem) ListDirectory(p string) ([]Stat, error) { } else { d = "application/octet-stream" } - 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 - } - m, err = mimetype.DetectReader(f) - if err != nil { - log.Error(err.Error()) - } - _ = f.Close() + // Get mimetype of file extension + splitted := strings.Split(e.Name(), ".") + fileExtension := splitted[len(splitted)-1] + m = mime.TypeByExtension("." + fileExtension) } 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 9d446beb..890f1713 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,13 @@ 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 of file extension + splitted := strings.Split(f.Name(), ".") + fileExtension := splitted[len(splitted)-1] + m = mime.TypeByExtension("." + fileExtension) + if _, err := f.Seek(0, io.SeekStart); err != nil { return Stat{}, err } @@ -62,8 +63,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 } From cd64f9982ed67b3e54cb4703cd8c3c3b75436e49 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Tue, 20 Aug 2024 14:23:20 +0200 Subject: [PATCH 2/6] change comment --- server/filesystem/filesystem.go | 2 +- server/filesystem/stat.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/filesystem/filesystem.go b/server/filesystem/filesystem.go index b32b18d9..15778c65 100644 --- a/server/filesystem/filesystem.go +++ b/server/filesystem/filesystem.go @@ -436,7 +436,7 @@ func (fs *Filesystem) ListDirectory(p string) ([]Stat, error) { } var m = "" if e.Type().IsRegular() { - // Get mimetype of file extension + // Get mimetype rom file extension splitted := strings.Split(e.Name(), ".") fileExtension := splitted[len(splitted)-1] m = mime.TypeByExtension("." + fileExtension) diff --git a/server/filesystem/stat.go b/server/filesystem/stat.go index 890f1713..208a5b41 100644 --- a/server/filesystem/stat.go +++ b/server/filesystem/stat.go @@ -50,7 +50,7 @@ func statFromFile(f ufs.File) (Stat, error) { } var m = "" if !s.IsDir() { - // Get mimetype of file extension + // Get mimetype from file extension splitted := strings.Split(f.Name(), ".") fileExtension := splitted[len(splitted)-1] m = mime.TypeByExtension("." + fileExtension) From 85f2b4ae3088f5249b140f582d30526e8df069f7 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Tue, 20 Aug 2024 14:27:00 +0200 Subject: [PATCH 3/6] typo --- server/filesystem/filesystem.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/filesystem/filesystem.go b/server/filesystem/filesystem.go index 15778c65..88e66416 100644 --- a/server/filesystem/filesystem.go +++ b/server/filesystem/filesystem.go @@ -436,7 +436,7 @@ func (fs *Filesystem) ListDirectory(p string) ([]Stat, error) { } var m = "" if e.Type().IsRegular() { - // Get mimetype rom file extension + // Get mimetype from file extension splitted := strings.Split(e.Name(), ".") fileExtension := splitted[len(splitted)-1] m = mime.TypeByExtension("." + fileExtension) From 1b79764a9080c11b65bc3c97ac00fe841dae0546 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Thu, 29 Aug 2024 15:06:44 +0200 Subject: [PATCH 4/6] fix deps --- go.sum | 2 ++ 1 file changed, 2 insertions(+) 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= From 9ce4212262b51a7a41c6db9961f3bb27988d82e7 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Mon, 2 Sep 2024 13:03:38 +0200 Subject: [PATCH 5/6] only get mimetype if there is actually a file extension --- server/filesystem/filesystem.go | 6 ++++-- server/filesystem/stat.go | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/server/filesystem/filesystem.go b/server/filesystem/filesystem.go index 88e66416..ac91045a 100644 --- a/server/filesystem/filesystem.go +++ b/server/filesystem/filesystem.go @@ -438,8 +438,10 @@ func (fs *Filesystem) ListDirectory(p string) ([]Stat, error) { if e.Type().IsRegular() { // Get mimetype from file extension splitted := strings.Split(e.Name(), ".") - fileExtension := splitted[len(splitted)-1] - m = mime.TypeByExtension("." + fileExtension) + if len(splitted) >= 2 { + fileExtension := splitted[len(splitted)-1] + m = mime.TypeByExtension("." + fileExtension) + } } st := Stat{FileInfo: info, Mimetype: d} diff --git a/server/filesystem/stat.go b/server/filesystem/stat.go index 08814565..f2e57d67 100644 --- a/server/filesystem/stat.go +++ b/server/filesystem/stat.go @@ -52,8 +52,10 @@ func statFromFile(f ufs.File) (Stat, error) { if !s.IsDir() { // Get mimetype from file extension splitted := strings.Split(f.Name(), ".") - fileExtension := splitted[len(splitted)-1] - m = mime.TypeByExtension("." + fileExtension) + 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 From a12f37ff63f5e4b383fdcb6c23e632f7c8a74587 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Fri, 13 Sep 2024 18:02:45 +0200 Subject: [PATCH 6/6] make file readable by default to match with the previous mimetype library --- server/filesystem/filesystem.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/filesystem/filesystem.go b/server/filesystem/filesystem.go index ac91045a..eb2ca0cd 100644 --- a/server/filesystem/filesystem.go +++ b/server/filesystem/filesystem.go @@ -432,7 +432,7 @@ 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 = "" if e.Type().IsRegular() {