Add normal neutered filesystem variant

This commit is contained in:
Melon 2022-11-05 00:47:57 +00:00
parent 18ad3f810f
commit b186b353a7
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 40 additions and 4 deletions

36
http/http-filesystem.go Normal file
View File

@ -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
}

View File

@ -1,19 +1,19 @@
package neutered_filesystem package neutered_filesystem
import ( import (
"net/http" "io/fs"
"path/filepath" "path/filepath"
) )
type NeuteredFileSystem struct { type NeuteredFileSystem struct {
fs http.FileSystem fs fs.FS
} }
func New(fs http.FileSystem) NeuteredFileSystem { func New(fs fs.FS) NeuteredFileSystem {
return NeuteredFileSystem{fs} 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) f, err := nfs.fs.Open(name)
if err != nil { if err != nil {
return nil, err return nil, err