neutered-filesystem/neutered-filesystem.go

37 lines
575 B
Go
Raw Permalink Normal View History

2021-12-24 01:14:18 +00:00
package neutered_filesystem
import (
2022-11-05 00:47:57 +00:00
"io/fs"
2021-12-24 01:46:59 +00:00
"path/filepath"
2021-12-24 01:14:18 +00:00
)
type NeuteredFileSystem struct {
2022-11-05 00:47:57 +00:00
fs fs.FS
2021-12-24 01:14:18 +00:00
}
2022-11-05 00:47:57 +00:00
func New(fs fs.FS) NeuteredFileSystem {
2021-12-24 01:17:44 +00:00
return NeuteredFileSystem{fs}
}
2022-11-05 00:47:57 +00:00
func (nfs NeuteredFileSystem) Open(name string) (fs.File, error) {
2021-12-24 01:46:59 +00:00
f, err := nfs.fs.Open(name)
2021-12-24 01:14:18 +00:00
if err != nil {
return nil, err
}
2021-12-24 01:46:59 +00:00
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
}
}
2021-12-24 01:14:18 +00:00
2021-12-24 01:46:59 +00:00
return f, nil
2021-12-24 01:14:18 +00:00
}