mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
35 lines
561 B
Go
35 lines
561 B
Go
package fs
|
|
|
|
import (
|
|
"io/fs"
|
|
"path/filepath"
|
|
)
|
|
|
|
// https://www.alexedwards.net/blog/disable-http-fileserver-directory-listings
|
|
|
|
type NeuteredFileSystem struct {
|
|
fs.FS
|
|
}
|
|
|
|
func (nfs NeuteredFileSystem) Open(path string) (fs.File, error) {
|
|
f, err := nfs.FS.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s, err := f.Stat()
|
|
if s.IsDir() {
|
|
index := filepath.Join(path, "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
|
|
}
|