From b24467c37d60d4d42685fef2ce154577446b6c17 Mon Sep 17 00:00:00 2001 From: Thayol Date: Sun, 13 Sep 2026 09:10:46 +0200 Subject: [PATCH] Fix duration saying never when it's unlimited --- internal/config/config_test.go | 8 ++++++++ internal/config/parse.go | 10 ++++++++++ internal/server/admin.go | 2 +- internal/server/login.go | 2 +- internal/server/pages.go | 4 ++-- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index e62945c..a4430fb 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -78,6 +78,14 @@ func TestFormatRoundTrips(t *testing.T) { if got, err := ParseDuration(FormatDuration(d)); err != nil || got != d { t.Errorf("FormatDuration(%s) = %q, which parses back to %s (%v)", d, FormatDuration(d), got, err) } + // FormatLifetime reaches the upload form as the expiry placeholder, so + // what it prints has to be something a caller could type back. + if got, err := ParseDuration(FormatLifetime(d)); err != nil || got != d { + t.Errorf("FormatLifetime(%s) = %q, which parses back to %s (%v)", d, FormatLifetime(d), got, err) + } + } + if got := FormatLifetime(Unlimited); got != "unlimited" { + t.Errorf("FormatLifetime(Unlimited) = %q, want %q", got, "unlimited") } } diff --git a/internal/config/parse.go b/internal/config/parse.go index f1351bc..e24d384 100644 --- a/internal/config/parse.go +++ b/internal/config/parse.go @@ -103,6 +103,16 @@ func ParseDuration(s string) (time.Duration, error) { return d, nil } +// FormatLifetime renders a lifetime *limit*, where FormatDuration's "never" +// would describe the wrong thing: a ceiling of Unlimited is not a file that +// never expires, it is the absence of a ceiling. +func FormatLifetime(d time.Duration) string { + if d == Unlimited { + return "unlimited" + } + return FormatDuration(d) +} + // FormatDuration is the human-facing inverse of ParseDuration. func FormatDuration(d time.Duration) string { switch { diff --git a/internal/server/admin.go b/internal/server/admin.go index a4fd6b5..e65033c 100644 --- a/internal/server/admin.go +++ b/internal/server/admin.go @@ -159,7 +159,7 @@ func (s *Server) adminTokens() []adminToken { out = append(out, adminToken{ Name: t.Name, MaxSize: config.FormatSize(l.MaxSize), - MaxExpiry: config.FormatDuration(l.MaxExpiry), + MaxExpiry: config.FormatLifetime(l.MaxExpiry), Vanity: l.AllowVanity, Admin: l.Admin, }) diff --git a/internal/server/login.go b/internal/server/login.go index f500bf9..254782d 100644 --- a/internal/server/login.go +++ b/internal/server/login.go @@ -45,7 +45,7 @@ func (s *Server) handleLoginPage(w http.ResponseWriter, r *http.Request) { page: s.page(r, "Log in", false), Next: next, MaxSize: config.FormatSize(lim.MaxSize), - MaxExpiry: config.FormatDuration(lim.MaxExpiry), + MaxExpiry: config.FormatLifetime(lim.MaxExpiry), Vanity: lim.AllowVanity, }) return diff --git a/internal/server/pages.go b/internal/server/pages.go index 0e6136b..d8d5383 100644 --- a/internal/server/pages.go +++ b/internal/server/pages.go @@ -69,8 +69,8 @@ func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) { s.render(w, http.StatusOK, "index.html", indexPage{ page: s.page(r, "Upload", true), MaxSize: config.FormatSize(lim.MaxSize), - MaxExpiry: config.FormatDuration(lim.MaxExpiry), - DefaultExpiry: config.FormatDuration(lim.DefaultExpiry), + MaxExpiry: config.FormatLifetime(lim.MaxExpiry), + DefaultExpiry: config.FormatLifetime(lim.DefaultExpiry), AbsBase: s.absBase(r), AllowVanity: lim.AllowVanity, MaxSizeBytes: lim.MaxSize,