neutered-filesystem/neutered-filesystem.go

37 lines
575 B
Go

package neutered_filesystem
import (
"io/fs"
"path/filepath"
)
type NeuteredFileSystem struct {
fs fs.FS
}
func New(fs fs.FS) NeuteredFileSystem {
return NeuteredFileSystem{fs}
}
func (nfs NeuteredFileSystem) Open(name string) (fs.File, error) {
f, err := nfs.fs.Open(name)
if err != nil {
return nil, err
}
s, err := f.Stat()
if s.IsDir() {
index := filepath.Join(name, "index.html")
if _, err := nfs.fs.Open(index); err != nil {
closeErr := f.Close()
if closeErr != nil {
return nil, closeErr
}
return nil, err
}
}
return f, nil
}