This commit is contained in:
toom1996
2026-09-29 19:59:30 +08:00
parent d15d2a4701
commit 361ffc01af
28 changed files with 896 additions and 47 deletions

View File

@ -19,6 +19,7 @@ import (
"bytes"
"context"
"embed"
"fmt"
"html/template"
"io/fs"
"net/http"
@ -41,6 +42,25 @@ func SetImgComposer(c *imgurl.Composer) {
}
}
// formatBytes 把字节数格式化为人类可读串(B/KB/MB/GB/TB);0 或负数返回「—」。
func formatBytes(n int64) string {
if n <= 0 {
return "—"
}
const unit = 1024
if n < unit {
return fmt.Sprintf("%d B", n)
}
val := float64(n)
for _, u := range []string{"KB", "MB", "GB", "TB"} {
val /= unit
if val < unit {
return fmt.Sprintf("%.1f %s", val, u)
}
}
return fmt.Sprintf("%.1f PB", val)
}
//go:embed templates/layout.html templates/partials.html templates/pages/*.html assets/*
var backstageFS embed.FS
@ -60,6 +80,7 @@ func init() {
// imgThumb 同但走列表缩略图样式(小尺寸场景省流量)。两者兜底逻辑见 imgurl.Composer。
"imgURL": func(stored string) string { return imgComposer.Compose(stored) },
"imgThumb": func(stored string) string { return imgComposer.ComposeThumb(stored) },
"formatBytes": formatBytes,
}
base, err := template.New("layout.html").Funcs(funcs).