2021-12-24 01:14:18 +00:00
|
|
|
package neutered_filesystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-12-24 01:46:59 +00:00
|
|
|
"path/filepath"
|
2021-12-24 01:14:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NeuteredFileSystem struct {
|
|
|
|
fs http.FileSystem
|
|
|
|
}
|
|
|
|
|
2021-12-24 01:17:44 +00:00
|
|
|
func New(fs http.FileSystem) NeuteredFileSystem {
|
|
|
|
return NeuteredFileSystem{fs}
|
|
|
|
}
|
|
|
|
|
2021-12-24 01:46:59 +00:00
|
|
|
func (nfs NeuteredFileSystem) Open(name string) (http.File, error) {
|
|
|
|
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
|
|
|
}
|