diff options
author | Jordan <me@jordan.im> | 2024-02-19 12:19:56 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2024-02-19 12:19:56 -0700 |
commit | bbcbf180adf5f5e0e6d89d4f5630936128cf508b (patch) | |
tree | 1639b24c561a210dd16fb59db10468adc2ed18cf | |
parent | f223f5f57054218027d694bce09f1657fa3326a9 (diff) | |
download | pipkin-bbcbf180adf5f5e0e6d89d4f5630936128cf508b.tar.gz pipkin-bbcbf180adf5f5e0e6d89d4f5630936128cf508b.zip |
templates: format last modified and size values, CSS
-rw-r--r-- | pipkin.go | 32 | ||||
-rw-r--r-- | templates/index.html | 42 |
2 files changed, 55 insertions, 19 deletions
@@ -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 @@ <!DOCTYPE html> <html> <head> -<style> -table { - font-family: monospace; - border-collapse: collapse; -} - -td, th { - border: 0px solid #dddddd; - text-align: left; - padding: 2px; - padding-right: 1em; -} +<title>Index of {{ .Path }}</title> +<style type="text/css"> + body { background-color: #F5F5F5; } + h2#title { margin-bottom: 12px; } + a, a:active { text-decoration: none; color: blue; } + a:visited { color: #48468F; } + a:hover, a:focus { text-decoration: underline; color: red; } + table { margin-left: 12px; } + th, td { font: 90% monospace; text-align: left; } + th { font-weight: bold; padding-right: 14px; padding-bottom: 3px; } + td { padding-right: 14px; } + td.size, th#size { text-align: right; } + #dirlist { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px; } + div#footer { font: 90% monospace; color: #787878; padding-top: 4px; } + a.sortheader { color: black; text-decoration: none; display: block; } + span.sortarrow { text-decoration: none; } </style> </head> <body> - <h2>Index of {{ .Path }}</h2> - -<table> +<div id="dirlist"> +<table summary="Directory Listing" cellpadding="0" cellspacing="0" class="sort"> <tr> <th>Name</th> <th>Last Modified</th> <th>Size</th> </tr> + <tr ><td><a href="../">Parent Directory</a></td><td>-</td><td>-</td></tr> {{- range $f := .Contents -}} <tr> <td><a href="{{- $f.Key -}}">{{- $f.Key -}}</a></td> - <td>{{- $f.LastModified -}}</td> - <td>{{- $f.Size -}}</td> + <td>{{- formatLastModified $f.LastModified -}}</td> + <td>{{- prettyByteSize $f.Size -}}</td> </tr> {{- end -}} {{- range $f := .CommonPrefixes -}} <tr> <td><a href="{{- $f.Prefix -}}">{{- $f.Prefix -}}</a></td> + <td>-</td> + <td>-</td> </tr> {{- end -}} </table> +</div> +<div id="footer"><a href="https://git.jordan.im/pipkin/">pipkin</a></div> </body> </html> |