当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
17 Star 35 Fork 0

vz / ego
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
fs.go 984 Bytes
一键复制 编辑 原始数据 按行查看 历史
vz 提交于 2017-08-16 20:09 . update comment
package util
import (
"net/http"
"os"
)
type (
OnlyfilesFS struct {
fs http.FileSystem
}
neuteredReaddirFile struct {
http.File
}
)
// Dir returns a http.Filesystem that can be used by http.FileServer(). It is used internally
// in router.Static().
// in router.Static().
// if listDirectory == true, then it works the same as http.Dir() otherwise it returns
// a filesystem that prevents http.FileServer() to list the directory files.
func Dir(root string, listDirectory bool) http.FileSystem {
fs := http.Dir(root)
if listDirectory {
return fs
}
return &OnlyfilesFS{fs}
}
// Open conforms to http.Filesystem.
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
}
// Readdir overrides the http.File default implementation.
func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
// this disables directory listing
return nil, nil
}
Go
1
https://gitee.com/veni0/ego.git
git@gitee.com:veni0/ego.git
veni0
ego
ego
263eb77d34e2

搜索帮助

53164aa7 5694891 3bd8fe86 5694891