2024-01-25 17:58:55 +01:00
|
|
|
package utils
|
2023-10-02 20:33:13 +02:00
|
|
|
|
2023-10-03 20:41:03 +02:00
|
|
|
import (
|
2023-10-04 01:49:14 +02:00
|
|
|
"log"
|
2024-02-19 00:32:27 +01:00
|
|
|
mathrand "math/rand/v2"
|
2024-02-18 23:27:14 +01:00
|
|
|
"regexp"
|
2023-12-28 00:02:50 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-04-02 22:13:41 +02:00
|
|
|
|
|
|
|
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
2023-10-03 20:41:03 +02:00
|
|
|
)
|
2023-10-02 20:33:13 +02:00
|
|
|
|
2024-04-02 22:13:41 +02:00
|
|
|
// GenerateURL generate random string for plak url.
|
|
|
|
func GenerateURL(length uint8) string {
|
2023-10-02 20:33:13 +02:00
|
|
|
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
2024-01-25 17:58:55 +01:00
|
|
|
b := make([]rune, length)
|
2023-10-02 20:33:13 +02:00
|
|
|
for i := range b {
|
2024-02-19 00:32:27 +01:00
|
|
|
b[i] = listChars[mathrand.IntN(len(listChars))]
|
2023-10-02 20:33:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(b)
|
|
|
|
}
|
2023-10-03 20:41:03 +02:00
|
|
|
|
2024-04-02 22:13:41 +02:00
|
|
|
// CheckCharRedundant verify is a character is redundant in a string.
|
2024-01-25 17:58:55 +01:00
|
|
|
func CheckCharRedundant(source string, char string) bool { // Verify if a char is redundant
|
2024-02-17 00:32:55 +01:00
|
|
|
return strings.Count(source, char) > 1
|
2023-10-20 03:30:19 +02:00
|
|
|
}
|
2023-12-28 00:02:50 +01:00
|
|
|
|
2024-01-25 17:58:55 +01:00
|
|
|
func parseIntBeforeSeparator(source *string, sep string) (int, error) { // return 0 & error if error, only accept positive number
|
|
|
|
if CheckCharRedundant(*source, sep) {
|
2024-02-25 00:29:39 +01:00
|
|
|
return 0, &parseIntBeforeSeparatorError{message: *source + ": cannot parse value as int"}
|
2024-01-25 17:58:55 +01:00
|
|
|
}
|
2023-12-28 00:02:50 +01:00
|
|
|
var value int
|
|
|
|
var err error
|
|
|
|
if strings.Contains(*source, sep) {
|
|
|
|
value, err = strconv.Atoi(strings.Split(*source, sep)[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2024-04-02 22:13:41 +02:00
|
|
|
|
2024-02-25 00:29:39 +01:00
|
|
|
return 0, &parseIntBeforeSeparatorError{message: *source + ": cannot parse value as int"}
|
2023-12-28 00:02:50 +01:00
|
|
|
}
|
|
|
|
if value < 0 { // Only positive value is correct
|
2024-02-25 00:29:39 +01:00
|
|
|
return 0, &parseIntBeforeSeparatorError{message: *source + ": format only take positive value"}
|
2023-12-28 00:02:50 +01:00
|
|
|
}
|
2024-01-25 17:58:55 +01:00
|
|
|
|
2024-04-02 22:13:41 +02:00
|
|
|
if value > 99 { //nolint:gomnd
|
2024-02-25 00:29:39 +01:00
|
|
|
return 0, &parseIntBeforeSeparatorError{message: *source + ": Format only take two number"}
|
2024-01-25 17:58:55 +01:00
|
|
|
}
|
|
|
|
|
2023-12-28 00:02:50 +01:00
|
|
|
*source = strings.Join(strings.Split(*source, sep)[1:], "")
|
|
|
|
}
|
2024-04-02 22:13:41 +02:00
|
|
|
|
2023-12-28 00:02:50 +01:00
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2024-04-02 22:13:41 +02:00
|
|
|
// ParseExpiration Parse "1d1h1m1s" duration format. Return 0 & error if error.
|
2024-01-25 17:58:55 +01:00
|
|
|
func ParseExpiration(source string) (int, error) {
|
2023-12-28 00:02:50 +01:00
|
|
|
var expiration int
|
|
|
|
var tempOutput int
|
|
|
|
var err error
|
|
|
|
if source == "0" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2024-01-25 17:58:55 +01:00
|
|
|
|
|
|
|
source = strings.ToLower(source)
|
|
|
|
|
2023-12-28 00:02:50 +01:00
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "d")
|
2024-04-02 22:13:41 +02:00
|
|
|
expiration = tempOutput * constant.SecondsInDay
|
2023-12-28 00:02:50 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2024-04-02 22:13:41 +02:00
|
|
|
|
2024-02-25 00:29:39 +01:00
|
|
|
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
2023-12-28 00:02:50 +01:00
|
|
|
}
|
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "h")
|
2024-04-02 22:13:41 +02:00
|
|
|
expiration += tempOutput * constant.SecondsInHour
|
2023-12-28 00:02:50 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2024-04-02 22:13:41 +02:00
|
|
|
|
2024-02-25 00:29:39 +01:00
|
|
|
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
2023-12-28 00:02:50 +01:00
|
|
|
}
|
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "m")
|
2024-04-02 22:13:41 +02:00
|
|
|
expiration += tempOutput * constant.SecondsInMinute
|
2023-12-28 00:02:50 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2024-04-02 22:13:41 +02:00
|
|
|
|
2024-02-25 00:29:39 +01:00
|
|
|
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
2023-12-28 00:02:50 +01:00
|
|
|
}
|
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "s")
|
|
|
|
expiration += tempOutput * 1
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2024-04-02 22:13:41 +02:00
|
|
|
|
2024-02-25 00:29:39 +01:00
|
|
|
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
2023-12-28 00:02:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return expiration, nil
|
|
|
|
}
|
2024-02-18 23:27:14 +01:00
|
|
|
|
2024-04-02 22:13:41 +02:00
|
|
|
// ValidKey Verify if a key is valid (only letter, number, - and _).
|
2024-02-18 23:27:14 +01:00
|
|
|
func ValidKey(key string) bool {
|
|
|
|
result, err := regexp.MatchString("^[a-zA-Z0-9_.-]*$", key)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
log.Println(key, result)
|
2024-04-02 22:13:41 +02:00
|
|
|
|
2024-02-18 23:27:14 +01:00
|
|
|
return result
|
|
|
|
}
|