105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
|
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 possible_bad_char_list_that_i_made_up_which_may_contain_bad_xss_chars_look_im_not_gonna_search_for_a_xss_charlist_just_to_prevent_self_xss []string = []string{"'", "\"", "/", "<", ">", "?", "=", "#", "(", ")", "{", "}", "*", "!", "`", "[", "]"}
|
||
|
|
||
|
for _, c := range possible_bad_char_list_that_i_made_up_which_may_contain_bad_xss_chars_look_im_not_gonna_search_for_a_xss_charlist_just_to_prevent_self_xss {
|
||
|
s = strings.ReplaceAll(s, c, "")
|
||
|
}
|
||
|
|
||
|
return s
|
||
|
}
|