Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get mimetype from file extension, rather than from file content #200

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 8 additions & 18 deletions server/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filesystem
import (
"fmt"
"io"
"mime"
"os"
"path/filepath"
"slices"
Expand All @@ -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"
Expand Down Expand Up @@ -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 from 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
})
Expand Down
19 changes: 10 additions & 9 deletions server/filesystem/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 from 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
}
Expand All @@ -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
}
Expand Down
Loading