neutered-filesystem/http/http-filesystem.go

37 lines
601 B
Go
Raw Permalink Normal View History

2022-11-05 00:47:57 +00:00
package http
import (
"net/http"
"path/filepath"
)
type NeuteredHttpFileSystem struct {
fs http.FileSystem
}
func New(fs http.FileSystem) NeuteredHttpFileSystem {
return NeuteredHttpFileSystem{fs}
}
func (nfs NeuteredHttpFileSystem) 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
}