Refactored StaticFS()

- different approach to disable directory listing.
This commit is contained in:
Manu Mtz-Almeida
2015-05-24 16:29:55 +02:00
parent 0a9030f9d7
commit fcfe65685a
3 changed files with 43 additions and 13 deletions
+37
View File
@@ -0,0 +1,37 @@
package gin
import (
"net/http"
"os"
)
type (
onlyfilesFS struct {
fs http.FileSystem
}
neuteredReaddirFile struct {
http.File
}
)
func Dir(root string, listDirectory bool) http.FileSystem {
fs := http.Dir(root)
if listDirectory {
return fs
} else {
return &onlyfilesFS{fs}
}
}
func (fs onlyfilesFS) Open(name string) (http.File, error) {
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
return neuteredReaddirFile{f}, nil
}
func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
// this disables directory listing
return nil, nil
}