From bbcbf180adf5f5e0e6d89d4f5630936128cf508b Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 19 Feb 2024 12:19:56 -0700 Subject: templates: format last modified and size values, CSS --- pipkin.go | 32 ++++++++++++++++++++++++++++++-- templates/index.html | 42 +++++++++++++++++++++++++----------------- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/pipkin.go b/pipkin.go index 7c787c0..d1a494f 100644 --- a/pipkin.go +++ b/pipkin.go @@ -11,11 +11,13 @@ import ( "flag" "fmt" "html/template" + "math" "io" "log" "net/http" "os" "path" + "strconv" "strings" "syscall" "time" @@ -254,8 +256,16 @@ func (pk *Pipkin) fetch(w http.ResponseWriter, r *http.Request) { index.Contents[i].Key = prefixTrim } - t, _ := template.ParseFiles(path.Join(pk.templates, - "index.html")) + t, err := template.New("index.html").Funcs(template.FuncMap{ + "prettyByteSize": prettyByteSize, + "formatLastModified": formatLastModified, + }).ParseFiles(path.Join(pk.templates, "index.html")) + if err != nil { + pk.log.Errorf("Failed to render template: %s %s %s: %s\n", + hostStr, host.Bucket, key, err.Error()) + http.Error(w, http.StatusText(http.StatusNotFound), + http.StatusNotFound) + } if err := t.Execute(w, index); err != nil { pk.log.Errorf("Failed to render template: %s %s %s: %s\n", hostStr, host.Bucket, key, err.Error()) @@ -409,6 +419,24 @@ func (pk *Pipkin) presignQuery(method string, host Host, key string) (string, er return signedURL.String(), nil } +func prettyByteSize(b string) string { + + bf, _ := strconv.ParseFloat(b, 0) + for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} { + if math.Abs(bf) < 1024.0 { + return fmt.Sprintf("%3.1f%sB", bf, unit) + } + bf /= 1024.0 + } + return fmt.Sprintf("%.1fYiB", bf) +} + +func formatLastModified(value string) string { + + t, _ := time.Parse("2006-01-02T15:04:05.000Z", value) + return t.Format(time.DateTime) +} + func main() { // We should re-use socket connections between requests. diff --git a/templates/index.html b/templates/index.html index 691de8e..409368b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,42 +1,50 @@ - -

Index of {{ .Path }}

- - +
+
+ {{- range $f := .Contents -}} - - + + {{- end -}} {{- range $f := .CommonPrefixes -}} + + {{- end -}}
Name Last Modified Size
Parent Directory--
{{- $f.Key -}}{{- $f.LastModified -}}{{- $f.Size -}}{{- formatLastModified $f.LastModified -}}{{- prettyByteSize $f.Size -}}
{{- $f.Prefix -}}--
+ + -- cgit v1.2.3-54-g00ecf