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

View File

@@ -29,7 +29,7 @@ func (c *Cache) refreshTorrentsWorker() {
} }
func (c *Cache) resetInvalidLinksWorker() { func (c *Cache) resetInvalidLinksWorker() {
// Calculate time until next 12:00 CET // Calculate time until next 00:00 CET
now := time.Now() now := time.Now()
loc, err := time.LoadLocation("CET") loc, err := time.LoadLocation("CET")
if err != nil { if err != nil {
@@ -43,7 +43,7 @@ func (c *Cache) resetInvalidLinksWorker() {
nowInCET.Year(), nowInCET.Year(),
nowInCET.Month(), nowInCET.Month(),
nowInCET.Day(), nowInCET.Day(),
12, 0, 0, 0, 0, 0, 0, 0,
loc, loc,
) )

View File

@@ -14,6 +14,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url"
"os" "os"
path "path/filepath" path "path/filepath"
"slices" "slices"
@@ -464,6 +465,48 @@ func (h *Handler) serveDirectory(w http.ResponseWriter, r *http.Request, file we
"add": func(a, b int) int { "add": func(a, b int) int {
return a + b 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) tmpl, err := template.New("directory").Funcs(funcMap).Parse(directoryTemplate)
if err != nil { if err != nil {

View File

@@ -109,16 +109,16 @@ const directoryTemplate = `
<h1>Index of {{.Path}}</h1> <h1>Index of {{.Path}}</h1>
<ul> <ul>
{{if .ShowParent}} {{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}} {{end}}
{{range $index, $file := .Children}} {{range $index, $file := .Children}}
<li> <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-number">{{add $index 1}}.</span>
<span class="file-name">{{$file.Name}}{{if $file.IsDir}}/{{end}}</span> <span class="file-name">{{$file.Name}}{{if $file.IsDir}}/{{end}}</span>
<span class="file-info"> <span class="file-info">
{{if not $file.IsDir}} {{if not $file.IsDir}}
{{$file.Size}} bytes {{formatSize $file.Size}}
{{end}} {{end}}
{{$file.ModTime.Format "2006-01-02 15:04:05"}} {{$file.ModTime.Format "2006-01-02 15:04:05"}}
</span> </span>