neutered-filesystem/neutered-filesystem.go
2021-12-24 01:46:59 +00:00

37 lines
600 B
Go

package neutered_filesystem
import (
"net/http"
"path/filepath"
)
type NeuteredFileSystem struct {
fs http.FileSystem
}
func New(fs http.FileSystem) NeuteredFileSystem {
return NeuteredFileSystem{fs}
}
func (nfs NeuteredFileSystem) Open(name string) (http.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
}