1 Star 2 Fork 0

阿债 / fiber-u8l

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
utils.go 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
阿债 提交于 2020-11-09 18:55 . 更新fiber到2.1.4
package filesystem
import (
"fmt"
"html"
"net/http"
"os"
"path"
"sort"
"strings"
"gitee.com/azhai/fiber-u8l"
)
func getFileExtension(path string) string {
n := strings.LastIndexByte(path, '.')
if n < 0 {
return ""
}
return path[n:]
}
func dirList(c *fiber.Ctx, f http.File) error {
fileinfos, err := f.Readdir(-1)
if err != nil {
return err
}
fm := make(map[string]os.FileInfo, len(fileinfos))
filenames := make([]string, 0, len(fileinfos))
for _, fi := range fileinfos {
name := fi.Name()
fm[name] = fi
filenames = append(filenames, name)
}
basePathEscaped := html.EscapeString(c.Path())
fmt.Fprintf(c, "<html><head><title>%s</title><style>.dir { font-weight: bold }</style></head><body>", basePathEscaped)
fmt.Fprintf(c, "<h1>%s</h1>", basePathEscaped)
fmt.Fprint(c, "<ul>")
if len(basePathEscaped) > 1 {
parentPathEscaped := html.EscapeString(c.Path() + "/..")
fmt.Fprintf(c, `<li><a href="%s" class="dir">..</a></li>`, parentPathEscaped)
}
sort.Strings(filenames)
for _, name := range filenames {
pathEscaped := html.EscapeString(path.Join(c.Path() + "/" + name))
fi := fm[name]
auxStr := "dir"
className := "dir"
if !fi.IsDir() {
auxStr = fmt.Sprintf("file, %d bytes", fi.Size())
className = "file"
}
fmt.Fprintf(c, `<li><a href="%s" class="%s">%s</a>, %s, last modified %s</li>`,
pathEscaped, className, html.EscapeString(name), auxStr, fi.ModTime())
}
fmt.Fprint(c, "</ul></body></html>")
c.Type("html")
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/azhai/fiber-u8l.git
git@gitee.com:azhai/fiber-u8l.git
azhai
fiber-u8l
fiber-u8l
v1.32.0

搜索帮助

344bd9b3 5694891 D2dac590 5694891