refactor(handler): improve autoindex code quality

Use fmt.Fprintf directly on buffer instead of buf.WriteString(fmt.Sprintf(...)),
handle dir.Close error in defer, use blank identifier for unused parameter,
use range-over-int, and remove trailing blank line.

💘 Generated with Crush

Assisted-by: GLM 5.1 via Crush <crush@charm.land>
This commit is contained in:
xfy 2026-04-30 16:23:34 +08:00
parent 5f470993ff
commit 26d62c9fcd
2 changed files with 7 additions and 8 deletions

View File

@ -80,7 +80,7 @@ func readDirectory(dirPath string) ([]dirEntry, error) {
if err != nil {
return nil, err
}
defer dir.Close()
defer func() { _ = dir.Close() }()
infos, err := dir.Readdir(-1)
if err != nil {
@ -117,7 +117,7 @@ func generateHTMLIndex(ctx *fasthttp.RequestCtx, reqPath string, entries []dirEn
// HTML 头部
buf.WriteString("<!DOCTYPE html>\n")
buf.WriteString("<html>\n<head>\n")
buf.WriteString(fmt.Sprintf("<title>Index of %s</title>\n", html.EscapeString(reqPath)))
fmt.Fprintf(&buf, "<title>Index of %s</title>\n", html.EscapeString(reqPath))
buf.WriteString("<style>\n")
buf.WriteString("body { font-family: monospace; margin: 20px; }\n")
buf.WriteString("h1 { border-bottom: 1px solid #ccc; padding-bottom: 10px; }\n")
@ -128,7 +128,7 @@ func generateHTMLIndex(ctx *fasthttp.RequestCtx, reqPath string, entries []dirEn
buf.WriteString("a:hover { text-decoration: underline; }\n")
buf.WriteString("</style>\n")
buf.WriteString("</head>\n<body>\n")
buf.WriteString(fmt.Sprintf("<h1>Index of %s</h1>\n", html.EscapeString(reqPath)))
fmt.Fprintf(&buf, "<h1>Index of %s</h1>\n", html.EscapeString(reqPath))
buf.WriteString("<hr>\n<table>\n")
buf.WriteString("<thead><tr><th>Name</th><th>Modified</th><th>Size</th></tr></thead>\n")
buf.WriteString("<tbody>\n")
@ -167,8 +167,8 @@ func generateHTMLIndex(ctx *fasthttp.RequestCtx, reqPath string, entries []dirEn
sizeStr = formatSize(entry.Size)
}
buf.WriteString(fmt.Sprintf("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td class=\"size\">%s</td></tr>\n",
href, html.EscapeString(displayName), timeStr, sizeStr))
fmt.Fprintf(&buf, "<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td class=\"size\">%s</td></tr>\n",
href, html.EscapeString(displayName), timeStr, sizeStr)
}
buf.WriteString("</tbody>\n</table>\n<hr>\n</body>\n</html>\n")
@ -179,7 +179,7 @@ func generateHTMLIndex(ctx *fasthttp.RequestCtx, reqPath string, entries []dirEn
}
// generateJSONIndex 生成 JSON 格式的目录列表。
func generateJSONIndex(ctx *fasthttp.RequestCtx, reqPath string, entries []dirEntry) {
func generateJSONIndex(ctx *fasthttp.RequestCtx, _ string, entries []dirEntry) {
type jsonEntry struct {
Name string `json:"name"`
Type string `json:"type"`
@ -277,4 +277,3 @@ func formatSize(size int64) string {
return fmt.Sprintf("%d", size)
}
}

View File

@ -404,7 +404,7 @@ func BenchmarkGenerateAutoIndex_HTML(b *testing.B) {
defer os.RemoveAll(tmpDir)
// 创建 100 个文件
for i := 0; i < 100; i++ {
for i := range 100 {
if err := os.WriteFile(filepath.Join(tmpDir, fmt.Sprintf("file%d.txt", i)), []byte("content"), 0o644); err != nil {
b.Fatal(err)
}