package lib import ( "archive/tar" "bytes" "compress/gzip" "errors" "fmt" "io" "strings" "time" "github.com/gofiber/fiber/v2" ) func RenderError(c *fiber.Ctx, code int) error{ var msg string = "Server Error" c.Status(code) switch code { case 404: msg = "Not Found" } return c.Render("error", fiber.Map{ "msg": msg, }) } func SizeFromBytes(size int) 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) } func ExtractFile(raw []byte, file string) (string, error){ stream := bytes.NewReader(raw) ustream, err := gzip.NewReader(stream) if err != nil { return "", err } reader := tar.NewReader(ustream) var header *tar.Header for header, err = reader.Next(); err == nil; header, err = reader.Next() { if header.Typeflag != tar.TypeReg { return "", errors.New("Found invalid entry in archive") } if header.Name != file { continue } buf := new(strings.Builder) _, err := io.Copy(buf, reader) if err != nil { return "", errors.New("Failed to extract file to memory") } return buf.String(), nil } return "", errors.New("File not found") } func GetTimePassed(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 } func CleanString(s string) string{ var badchars []string = []string{"~", "'", "\"", "/", "<", ">", "?", "=", "#", "(", ")", "{", "}", "*", "!", "`", "[", "]"} for _, c := range badchars { s = strings.ReplaceAll(s, c, "") } return s }