2024-10-07 22:44:53 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-11-21 00:15:30 +00:00
|
|
|
"crypto/rand"
|
2025-01-21 14:02:43 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2024-11-21 00:15:30 +00:00
|
|
|
"fmt"
|
|
|
|
math "math/rand"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2025-01-21 14:02:43 +00:00
|
|
|
|
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/logger"
|
2024-10-07 22:44:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CheckErr(source string, err error) {
|
|
|
|
if err != nil {
|
2025-01-21 14:02:43 +00:00
|
|
|
logger.Fatal(fmt.Sprintf("%s: %v", source, err))
|
2024-10-07 22:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateKey() []byte {
|
2024-11-21 00:15:30 +00:00
|
|
|
b := make([]byte, 64)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// CapitalizeFirst returns the input string with the first letter capitalized.
|
|
|
|
func CapitalizeFirst(s string) string {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return s // Return the empty string as is
|
|
|
|
}
|
|
|
|
return strings.ToUpper(string(s[0])) + s[1:]
|
|
|
|
}
|
|
|
|
|
2025-01-21 14:02:43 +00:00
|
|
|
func InsecureRandomIdentifier(length int) string {
|
2024-11-21 00:15:30 +00:00
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
math.New(math.NewSource(time.Now().UnixNano()))
|
|
|
|
result := make([]byte, length)
|
|
|
|
for i := range result {
|
|
|
|
result[i] = charset[math.Intn(len(charset))]
|
|
|
|
}
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintStructFields(v interface{}) {
|
|
|
|
val := reflect.ValueOf(v)
|
|
|
|
typ := reflect.TypeOf(v)
|
|
|
|
|
|
|
|
// Ensure the input is a struct
|
|
|
|
if val.Kind() != reflect.Struct {
|
2025-01-21 14:02:43 +00:00
|
|
|
logger.Debug("Provided value is not a struct")
|
2024-11-21 00:15:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the fields of the struct
|
|
|
|
for i := 0; i < val.NumField(); i++ {
|
|
|
|
field := val.Field(i)
|
|
|
|
fieldType := typ.Field(i)
|
|
|
|
|
|
|
|
// Convert field value to string, if possible
|
|
|
|
fieldValue := fmt.Sprintf("%v", field.Interface())
|
|
|
|
|
|
|
|
// Limit to 50 characters
|
|
|
|
if len(fieldValue) > 100 {
|
|
|
|
fieldValue = fieldValue[:100] + "..."
|
|
|
|
}
|
|
|
|
|
2025-01-21 14:02:43 +00:00
|
|
|
logger.Debug(fmt.Sprintf("Field: %s, %s\n", fieldType.Name, fieldValue))
|
2024-11-21 00:15:30 +00:00
|
|
|
}
|
2024-10-07 22:44:53 +00:00
|
|
|
}
|
2024-11-26 17:21:41 +00:00
|
|
|
|
|
|
|
func GetParentDirectoryPath(path string) string {
|
|
|
|
if path == "/" || path == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
path = strings.TrimSuffix(path, "/") // Remove trailing slash if any
|
|
|
|
lastSlash := strings.LastIndex(path, "/")
|
|
|
|
if lastSlash == -1 {
|
|
|
|
return "" // No parent directory for a relative path without slashes
|
|
|
|
}
|
|
|
|
if lastSlash == 0 {
|
|
|
|
return "/" // If the last slash is the first character, return root
|
|
|
|
}
|
|
|
|
return path[:lastSlash]
|
|
|
|
}
|
2025-01-21 14:02:43 +00:00
|
|
|
|
|
|
|
func HashSHA256(data string) string {
|
|
|
|
bytes := sha256.Sum256([]byte(data))
|
|
|
|
return hex.EncodeToString(bytes[:])
|
|
|
|
}
|