From d00e356bc2d0b022b95d31888a878a1ee4abca5d Mon Sep 17 00:00:00 2001 From: MrMelon Date: Fri, 24 Dec 2021 01:46:59 +0000 Subject: [PATCH] Better NFS --- neutered-filesystem.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/neutered-filesystem.go b/neutered-filesystem.go index 2dd4001..d7e16d9 100644 --- a/neutered-filesystem.go +++ b/neutered-filesystem.go @@ -2,7 +2,7 @@ package neutered_filesystem import ( "net/http" - "os" + "path/filepath" ) type NeuteredFileSystem struct { @@ -13,18 +13,24 @@ func New(fs http.FileSystem) NeuteredFileSystem { return NeuteredFileSystem{fs} } -func (fs NeuteredFileSystem) Open(name string) (http.File, error) { - f, err := fs.fs.Open(name) +func (nfs NeuteredFileSystem) Open(name string) (http.File, error) { + f, err := nfs.fs.Open(name) if err != nil { return nil, err } - return neuteredReaddirFile{f}, nil -} -type neuteredReaddirFile struct { - http.File -} + 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 + } -func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) { - return nil, nil + return nil, err + } + } + + return f, nil }