gin/internal/fs/fs.go
sergioperezcheco 5752de9235
fix: support HTMLFS globs with Readdir-only files
Co-Authored-By: gpt-6.1-sol <gpt-6.1-sol@users.noreply.github.com>
Signed-off-by: sergioperezcheco <checo520@outlook.com>
2026-10-03 15:29:02 +08:00

51 lines
1.0 KiB
Go

package fs
import (
"io/fs"
"net/http"
"slices"
"strings"
)
// FileSystem implements an [fs.FS].
type FileSystem struct {
http.FileSystem
}
// Open passes `Open` to the upstream implementation and return an [fs.File].
func (o FileSystem) Open(name string) (fs.File, error) {
f, err := o.FileSystem.Open(name)
if err != nil {
return nil, err
}
return fs.File(f), nil
}
// ReadDir adapts http.File.Readdir for globbing through io/fs.
func (o FileSystem) ReadDir(name string) ([]fs.DirEntry, error) {
f, err := o.FileSystem.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
var entries []fs.DirEntry
if dir, ok := f.(fs.ReadDirFile); ok {
entries, err = dir.ReadDir(-1)
} else {
var infos []fs.FileInfo
infos, err = f.Readdir(-1)
if infos != nil {
entries = make([]fs.DirEntry, len(infos))
}
for i, info := range infos {
entries[i] = fs.FileInfoToDirEntry(info)
}
}
slices.SortFunc(entries, func(a, b fs.DirEntry) int {
return strings.Compare(a.Name(), b.Name())
})
return entries, err
}