Add normal neutered filesystem variant
This commit is contained in:
parent
18ad3f810f
commit
b186b353a7
36
http/http-filesystem.go
Normal file
36
http/http-filesystem.go
Normal 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
|
||||
}
|
@ -1,19 +1,19 @@
|
||||
package neutered_filesystem
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type NeuteredFileSystem struct {
|
||||
fs http.FileSystem
|
||||
fs fs.FS
|
||||
}
|
||||
|
||||
func New(fs http.FileSystem) NeuteredFileSystem {
|
||||
func New(fs fs.FS) NeuteredFileSystem {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Loading…
Reference in New Issue
Block a user