mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-09 18:33:36 +02:00
refactor: clean up code and enhance utilities with new functions
This commit is contained in:
@@ -106,12 +106,6 @@ func (p BidirectionalPipe) Start() gperr.Error {
|
||||
return b.Error()
|
||||
}
|
||||
|
||||
var copyBufPool = sync.Pool{
|
||||
New: func() any {
|
||||
return make([]byte, copyBufSize)
|
||||
},
|
||||
}
|
||||
|
||||
type httpFlusher interface {
|
||||
Flush() error
|
||||
}
|
||||
|
||||
@@ -529,10 +529,10 @@ func SaveJSON[T any](path string, src *T, perm os.FileMode) error {
|
||||
return os.WriteFile(path, data, perm)
|
||||
}
|
||||
|
||||
func LoadJSONIfExist[T any](path string, dst *T) (exists bool, err error) {
|
||||
err = loadSerialized(path, dst, json.Unmarshal)
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
return false, nil
|
||||
func LoadJSONIfExist[T any](path string, dst *T) error {
|
||||
err := loadSerialized(path, dst, json.Unmarshal)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return true, err
|
||||
return err
|
||||
}
|
||||
|
||||
11
internal/utils/strutils/filepath.go
Normal file
11
internal/utils/strutils/filepath.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package strutils
|
||||
|
||||
import "strings"
|
||||
|
||||
// IsValidFilename checks if a filename is safe and doesn't contain path traversal attempts
|
||||
// Returns true if the filename is valid, false otherwise
|
||||
func IsValidFilename(filename string) bool {
|
||||
return !strings.Contains(filename, "/") &&
|
||||
!strings.Contains(filename, "\\") &&
|
||||
!strings.Contains(filename, "..")
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package strutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -65,6 +66,44 @@ func ParseBool(s string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func formatFloat(f float64) string {
|
||||
f = math.Round(f*100) / 100
|
||||
if f == 0 {
|
||||
return "0"
|
||||
}
|
||||
return strconv.FormatFloat(f, 'f', -1, 64)
|
||||
}
|
||||
|
||||
func FormatByteSize[T ~int64 | ~uint64 | ~float64](size T) (value, unit string) {
|
||||
const (
|
||||
_ = (1 << (10 * iota))
|
||||
kb
|
||||
mb
|
||||
gb
|
||||
tb
|
||||
pb
|
||||
)
|
||||
switch {
|
||||
case size < kb:
|
||||
return fmt.Sprintf("%v", size), "B"
|
||||
case size < mb:
|
||||
return formatFloat(float64(size) / kb), "KiB"
|
||||
case size < gb:
|
||||
return formatFloat(float64(size) / mb), "MiB"
|
||||
case size < tb:
|
||||
return formatFloat(float64(size) / gb), "GiB"
|
||||
case size < pb:
|
||||
return formatFloat(float64(size/gb) / kb), "TiB" // prevent overflow
|
||||
default:
|
||||
return formatFloat(float64(size/tb) / kb), "PiB" // prevent overflow
|
||||
}
|
||||
}
|
||||
|
||||
func FormatByteSizeWithUnit[T ~int64 | ~uint64 | ~float64](size T) string {
|
||||
value, unit := FormatByteSize(size)
|
||||
return value + " " + unit
|
||||
}
|
||||
|
||||
func PortString(port uint16) string {
|
||||
return strconv.FormatUint(uint64(port), 10)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
@@ -102,6 +103,14 @@ func ExpectDeepEqual[T any](t *testing.T, got T, want T) {
|
||||
}
|
||||
}
|
||||
|
||||
func ExpectBytesEqual(t *testing.T, got []byte, want []byte) {
|
||||
t.Helper()
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Errorf("expected:\n%v, got\n%v", want, got)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func ExpectTrue(t *testing.T, got bool) {
|
||||
t.Helper()
|
||||
if !got {
|
||||
|
||||
Reference in New Issue
Block a user