diff --git a/http/http-filesystem.go b/http/http-filesystem.go new file mode 100644 index 0000000..d8f3536 --- /dev/null +++ b/http/http-filesystem.go @@ -0,0 +1,36 @@ +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 +} diff --git a/neutered-filesystem.go b/neutered-filesystem.go index d7e16d9..1b38c11 100644 --- a/neutered-filesystem.go +++ b/neutered-filesystem.go @@ -1,19 +1,19 @@ package neutered_filesystem import ( - "net/http" + "io/fs" "path/filepath" ) type NeuteredFileSystem struct { - fs http.FileSystem + fs fs.FS } -func New(fs http.FileSystem) NeuteredFileSystem { +func New(fs fs.FS) NeuteredFileSystem { return NeuteredFileSystem{fs} } -func (nfs NeuteredFileSystem) Open(name string) (http.File, error) { +func (nfs NeuteredFileSystem) Open(name string) (fs.File, error) { f, err := nfs.fs.Open(name) if err != nil { return nil, err