Skip to content

Commit 981f0c0

Browse files
committed
fix: mime type is actually used by the panel.
1 parent 3a76ba3 commit 981f0c0

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/server/filesystem/filesystem.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"time"
1515

1616
"emperror.dev/errors"
17+
"github.com/gabriel-vasile/mimetype"
18+
1719
// "github.com/apex/log" // Was only used for the listDirectory function for Mimetype which has now been removed for performance.
1820
ignore "github.com/sabhiram/go-gitignore"
1921

@@ -432,6 +434,17 @@ func (fs *Filesystem) ListDirectory(p string) ([]Stat, error) {
432434
mt := "application/octet-stream"
433435
if e.Type().IsDir() {
434436
mt = "inode/directory"
437+
} else {
438+
// Detect MIME type for files by opening and reading first 512 bytes
439+
// NOTE: This is used by the frontend for determining if a file can be edited and
440+
// should not be removed! - ellie
441+
filePath := filepath.Join(p, e.Name())
442+
if f, err := fs.unixFS.Open(filePath); err == nil {
443+
if detected, err := mimetype.DetectReader(f); err == nil {
444+
mt = detected.String()
445+
}
446+
f.Close()
447+
}
435448
}
436449
return Stat{FileInfo: info, Mimetype: mt}, nil
437450
})
@@ -460,7 +473,7 @@ func (fs *Filesystem) ListDirectoryPaged(ctx context.Context, p string, offset,
460473
if limit <= 0 {
461474
return []Stat{}, 0, nil
462475
}
463-
476+
464477
entries, err := fs.unixFS.ReadDir(p)
465478
if err != nil {
466479
return nil, 0, err
@@ -500,6 +513,17 @@ func (fs *Filesystem) ListDirectoryPaged(ctx context.Context, p string, offset,
500513
mt := "application/octet-stream"
501514
if e.Type().IsDir() {
502515
mt = "inode/directory"
516+
} else {
517+
// Detect MIME type for files by opening and reading first 512 bytes
518+
// NOTE: This is used by the frontend for determining if a file can be edited and
519+
// should not be removed! - ellie
520+
filePath := filepath.Join(p, e.Name())
521+
if f, err := fs.unixFS.Open(filePath); err == nil {
522+
if detected, err := mimetype.DetectReader(f); err == nil {
523+
mt = detected.String()
524+
}
525+
f.Close()
526+
}
503527
}
504528
out = append(out, Stat{FileInfo: info, Mimetype: mt})
505529
}

0 commit comments

Comments
 (0)