// Package util holds small helpers shared by the service and handler layers: // output formatting and external-command plumbing. package util import ( "fmt" "net/url" "strings" ) // FormatClock renders a duration in whole seconds as H:MM:SS, or M:SS when // under an hour. func FormatClock(seconds int) string { if seconds < 0 { seconds = 0 } m := seconds / 60 s := seconds % 60 h := m / 60 m = m % 60 if h > 0 { return fmt.Sprintf("%d:%02d:%02d", h, m, s) } return fmt.Sprintf("%d:%02d", m, s) } // FormatBytes renders a byte count as a compact human-readable size using // 1024-based (binary) units with the matching iB suffix. func FormatBytes(n int64) string { const unit = 1024 if n < unit { return fmt.Sprintf("%d B", n) } div, exp := int64(unit), 0 for m := n / unit; m >= unit; m /= unit { div *= unit exp++ } return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp]) } // URLEncodePath escapes each segment of a slash-separated path, preserving the // slashes themselves. func URLEncodePath(p string) string { parts := strings.Split(strings.Trim(p, "/"), "/") for i, part := range parts { parts[i] = url.PathEscape(part) } return strings.Join(parts, "/") }