Ada
2d1768d0c5
Some checks failed
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/release Pipeline was successful
ci/woodpecker/pr/lint Pipeline failed
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint Pipeline failed
108 lines
2.8 KiB
Go
108 lines
2.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"log"
|
|
mathrand "math/rand/v2"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
|
)
|
|
|
|
// GenerateURL generate random string for plak url.
|
|
func GenerateURL(length uint8) string {
|
|
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
b := make([]rune, length)
|
|
for i := range b {
|
|
b[i] = listChars[mathrand.IntN(len(listChars))]
|
|
}
|
|
|
|
return string(b)
|
|
}
|
|
|
|
// CheckCharRedundant verify is a character is redundant in a string.
|
|
func CheckCharRedundant(source string, char string) bool { // Verify if a char is redundant
|
|
return strings.Count(source, char) > 1
|
|
}
|
|
|
|
func parseIntBeforeSeparator(source *string, sep string) (int, error) { // return 0 & error if error, only accept positive number
|
|
if CheckCharRedundant(*source, sep) {
|
|
return 0, &parseIntBeforeSeparatorError{message: *source + ": cannot parse value as int"}
|
|
}
|
|
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)
|
|
|
|
return 0, &parseIntBeforeSeparatorError{message: *source + ": cannot parse value as int"}
|
|
}
|
|
if value < 0 { // Only positive value is correct
|
|
return 0, &parseIntBeforeSeparatorError{message: *source + ": format only take positive value"}
|
|
}
|
|
|
|
if value > 99 { //nolint:gomnd
|
|
return 0, &parseIntBeforeSeparatorError{message: *source + ": Format only take two number"}
|
|
}
|
|
|
|
*source = strings.Join(strings.Split(*source, sep)[1:], "")
|
|
}
|
|
|
|
return value, nil
|
|
}
|
|
|
|
// ParseExpiration Parse "1d1h1m1s" duration format. Return 0 & error if error.
|
|
func ParseExpiration(source string) (int, error) {
|
|
var expiration int
|
|
var tempOutput int
|
|
var err error
|
|
if source == "0" {
|
|
return 0, nil
|
|
}
|
|
|
|
source = strings.ToLower(source)
|
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "d")
|
|
expiration = tempOutput * constant.SecondsInDay
|
|
if err != nil {
|
|
log.Println(err)
|
|
|
|
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
|
}
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "h")
|
|
expiration += tempOutput * constant.SecondsInHour
|
|
if err != nil {
|
|
log.Println(err)
|
|
|
|
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
|
}
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "m")
|
|
expiration += tempOutput * constant.SecondsInMinute
|
|
if err != nil {
|
|
log.Println(err)
|
|
|
|
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
|
}
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "s")
|
|
expiration += tempOutput * 1
|
|
if err != nil {
|
|
log.Println(err)
|
|
|
|
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
|
}
|
|
|
|
return expiration, nil
|
|
}
|
|
|
|
// ValidKey Verify if a key is valid (only letter, number, - and _).
|
|
func ValidKey(key string) bool {
|
|
result, err := regexp.MatchString("^[a-zA-Z0-9_.-]*$", key)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
log.Println(key, result)
|
|
|
|
return result
|
|
}
|