Fix url in UI

This commit is contained in:
Mukhtar Akere
2025-04-08 21:00:40 +01:00
parent 9011420ac3
commit 92177b150b
3 changed files with 48 additions and 5 deletions
+43
View File
@@ -14,6 +14,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
path "path/filepath"
"slices"
@@ -464,6 +465,48 @@ func (h *Handler) serveDirectory(w http.ResponseWriter, r *http.Request, file we
"add": func(a, b int) int {
return a + b
},
"urlpath": func(p string) string {
segments := strings.Split(p, "/")
for i, segment := range segments {
segments[i] = url.PathEscape(segment)
}
return strings.Join(segments, "/")
},
"formatSize": func(bytes int64) string {
const (
KB = 1024
MB = 1024 * KB
GB = 1024 * MB
TB = 1024 * GB
)
var size float64
var unit string
switch {
case bytes >= TB:
size = float64(bytes) / TB
unit = "TB"
case bytes >= GB:
size = float64(bytes) / GB
unit = "GB"
case bytes >= MB:
size = float64(bytes) / MB
unit = "MB"
case bytes >= KB:
size = float64(bytes) / KB
unit = "KB"
default:
size = float64(bytes)
unit = "bytes"
}
// Format to 2 decimal places for larger units, no decimals for bytes
if unit == "bytes" {
return fmt.Sprintf("%.0f %s", size, unit)
}
return fmt.Sprintf("%.2f %s", size, unit)
},
}
tmpl, err := template.New("directory").Funcs(funcMap).Parse(directoryTemplate)
if err != nil {
+3 -3
View File
@@ -109,16 +109,16 @@ const directoryTemplate = `
<h1>Index of {{.Path}}</h1>
<ul>
{{if .ShowParent}}
<li><a href="{{.ParentPath}}" class="parent-dir"><span class="file-number"></span>Parent Directory</a></li>
<li><a href="{{urlpath .ParentPath}}" class="parent-dir"><span class="file-number"></span>Parent Directory</a></li>
{{end}}
{{range $index, $file := .Children}}
<li>
<a href="{{$.Path}}/{{$file.Name}}">
<a href="{{urlpath (printf "%s/%s" $.Path $file.Name)}}">
<span class="file-number">{{add $index 1}}.</span>
<span class="file-name">{{$file.Name}}{{if $file.IsDir}}/{{end}}</span>
<span class="file-info">
{{if not $file.IsDir}}
{{$file.Size}} bytes
{{formatSize $file.Size}}
{{end}}
{{$file.ModTime.Format "2006-01-02 15:04:05"}}
</span>