update: make things work with the new package system

This commit is contained in:
ngn
2024-08-13 03:01:00 +03:00
parent 528df304e9
commit a556ed08aa
14 changed files with 556 additions and 373 deletions

View File

@ -1,12 +1,7 @@
package lib
import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"strings"
"time"
@ -14,99 +9,68 @@ import (
)
func ListToStr(l []string) string {
res := ""
for _, e := range l {
res += e+" "
}
return res
res := ""
for _, e := range l {
res += e + " "
}
return res
}
func RenderError(c *fiber.Ctx, code int) error{
var msg string = "Server Error"
c.Status(code)
func RenderError(c *fiber.Ctx, code int) error {
var msg string = "Server Error"
c.Status(code)
switch code {
case 404:
msg = "Not Found"
}
switch code {
case 404:
msg = "Not Found"
}
return c.Render("error", fiber.Map{
"msg": msg,
})
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 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)
}
func ExtractFile(raw []byte, file string) (string, error){
stream := bytes.NewReader(raw)
ustream, err := gzip.NewReader(stream)
if err != nil {
return "", err
}
func TimePassed(t time.Time) string {
diff := time.Since(t)
res := fmt.Sprintf(
"%ds ago",
int(diff.Seconds()),
)
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 diff.Minutes() > 1 {
res = fmt.Sprintf(
"%dm and %ds ago",
int(diff.Minutes()), int(diff.Seconds())-(int(diff.Minutes())*60),
)
}
if header.Name != file {
continue
}
if diff.Hours() > 1 {
res = fmt.Sprintf("%dh and %dm ago",
int(diff.Hours()),
int(diff.Minutes())-(int(diff.Hours())*60),
)
}
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")
return res
}
func GetTimePassed(t time.Time) string {
diff := time.Since(t)
res := fmt.Sprintf(
"%ds ago",
int(diff.Seconds()),
)
func SanitizeXSS(s string) string {
var bad []string = []string{"~", "'", "\"", "/", "<", ">", "?", "=", "#", "(", ")", "{", "}", "*", "!", "`", "[", "]"}
if diff.Minutes() > 1 {
res = fmt.Sprintf(
"%dm and %ds ago",
int(diff.Minutes()), int(diff.Seconds())-(int(diff.Minutes())*60),
)
}
for _, c := range bad {
s = strings.ReplaceAll(s, c, "")
}
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
return s
}