2021-12-20 16:51:05 +03:00
|
|
|
package fs
|
2021-02-07 14:28:42 +03:00
|
|
|
|
|
|
|
import (
|
2021-12-20 16:51:05 +03:00
|
|
|
"io/fs"
|
2021-02-07 14:28:42 +03:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// https://www.alexedwards.net/blog/disable-http-fileserver-directory-listings
|
|
|
|
|
|
|
|
type NeuteredFileSystem struct {
|
2021-12-20 18:41:50 +03:00
|
|
|
fs.FS
|
2021-02-07 14:28:42 +03:00
|
|
|
}
|
|
|
|
|
2021-12-20 16:51:05 +03:00
|
|
|
func (nfs NeuteredFileSystem) Open(path string) (fs.File, error) {
|
2021-12-20 18:41:50 +03:00
|
|
|
f, err := nfs.FS.Open(path)
|
2021-02-07 14:28:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := f.Stat()
|
|
|
|
if s.IsDir() {
|
|
|
|
index := filepath.Join(path, "index.html")
|
2021-12-20 18:41:50 +03:00
|
|
|
if _, err := nfs.FS.Open(index); err != nil {
|
2021-02-07 14:28:42 +03:00
|
|
|
closeErr := f.Close()
|
|
|
|
if closeErr != nil {
|
|
|
|
return nil, closeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|