neutered-filesystem/http/http-filesystem.go

37 lines
601 B
Go

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
}