tracker/lib/util.go

77 lines
1.3 KiB
Go
Raw Normal View History

2024-01-17 17:06:26 +00:00
package lib
import (
"fmt"
"strings"
"time"
"github.com/gofiber/fiber/v2"
)
2024-03-21 18:52:14 +00:00
func ListToStr(l []string) string {
res := ""
for _, e := range l {
res += e + " "
}
return res
2024-03-21 18:52:14 +00:00
}
func RenderError(c *fiber.Ctx, code int) error {
var msg string = "Server Error"
c.Status(code)
2024-01-17 17:06:26 +00:00
switch code {
case 404:
msg = "Not Found"
}
2024-01-17 17:06:26 +00:00
return c.Render("error", fiber.Map{
"msg": msg,
})
2024-01-17 17:06:26 +00:00
}
func SizeFromBytes(size int64) string {
if size > 1024*1024*1024 {
return fmt.Sprintf("%dGB", (size / 1024 / 1024 / 1024))
} else if size > 1024*1024 {
return fmt.Sprintf("%dMB", (size / 1024 / 1024))
} else if size > 1024 {
return fmt.Sprintf("%dKB", (size / 1024))
}
return fmt.Sprintf("%dB", size)
2024-01-17 17:06:26 +00:00
}
func TimePassed(t time.Time) string {
diff := time.Since(t)
res := fmt.Sprintf(
"%ds ago",
int(diff.Seconds()),
)
if diff.Minutes() > 1 {
res = fmt.Sprintf(
"%dm and %ds ago",
int(diff.Minutes()), int(diff.Seconds())-(int(diff.Minutes())*60),
)
}
if diff.Hours() > 1 {
res = fmt.Sprintf("%dh and %dm ago",
int(diff.Hours()),
int(diff.Minutes())-(int(diff.Hours())*60),
)
}
return res
2024-01-17 17:06:26 +00:00
}
func SanitizeXSS(s string) string {
var bad []string = []string{"~", "'", "\"", "/", "<", ">", "?", "=", "#", "(", ")", "{", "}", "*", "!", "`", "[", "]"}
2024-01-17 17:06:26 +00:00
for _, c := range bad {
s = strings.ReplaceAll(s, c, "")
}
2024-01-17 17:06:26 +00:00
return s
2024-01-17 17:06:26 +00:00
}