mirror of
https://github.com/abhinavxd/libredesk.git
synced 2026-09-23 11:03:43 +00:00
ed314eb1a5
- feat: notification emails including - welcome email, assigned conversation email, reset password email. - feat: adds outer app component which does not have a navbar - feat: improve email templating, adds contact and conversations fields to email template - fix: evaluator - ensure "contains" checks evaluate individual tokens in the string rather than the entire string. - feat: logo URL for the app in general settings. - General fixes and refactors. - fix: hide trailing prose mirror line breaks.
102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
// Package stringutil provides string utility functions.
|
|
package stringutil
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"net/url"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"github.com/k3a/html2text"
|
|
)
|
|
|
|
const (
|
|
PasswordDummy = "•"
|
|
)
|
|
|
|
var (
|
|
regexpNonAlNum = regexp.MustCompile(`[^a-zA-Z0-9\-_\.]+`)
|
|
regexpSpaces = regexp.MustCompile(`[\s]+`)
|
|
)
|
|
|
|
// HTML2Text converts HTML to text.
|
|
func HTML2Text(html string) string {
|
|
return strings.TrimSpace(html2text.HTML2Text(html))
|
|
}
|
|
|
|
// SanitizeFilename sanitizes the provided filename.
|
|
func SanitizeFilename(fName string) string {
|
|
// Trim whitespace.
|
|
name := strings.TrimSpace(fName)
|
|
|
|
// Replace whitespace and "/" with "-"
|
|
name = regexpSpaces.ReplaceAllString(name, "-")
|
|
|
|
// Remove or replace any non-alphanumeric characters
|
|
name = regexpNonAlNum.ReplaceAllString(name, "")
|
|
|
|
// Convert to lowercase
|
|
name = strings.ToLower(name)
|
|
return filepath.Base(name)
|
|
}
|
|
|
|
// RandomAlNumString generates a random alphanumeric string of length n.
|
|
func RandomAlNumString(n int) (string, error) {
|
|
const dictionary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for k, v := range bytes {
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
}
|
|
|
|
return string(bytes), nil
|
|
}
|
|
|
|
// RandomNumericString generates a random numeric string of length n.
|
|
func RandomNumericString(n int) (string, error) {
|
|
const dictionary = "0123456789"
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for k, v := range bytes {
|
|
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
}
|
|
|
|
return string(bytes), nil
|
|
}
|
|
|
|
// GetPathFromURL extracts the path from a URL.
|
|
func GetPathFromURL(rawURL string) (string, error) {
|
|
parsedURL, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return parsedURL.Path, nil
|
|
}
|
|
|
|
// TokenizeString splits a string into tokens/words, removing punctuation
|
|
func TokenizeString(s string) []string {
|
|
// Remove common punctuation and split by whitespace
|
|
s = strings.Map(func(r rune) rune {
|
|
if unicode.IsPunct(r) {
|
|
return ' '
|
|
}
|
|
return r
|
|
}, s)
|
|
|
|
// Split by whitespace and filter out empty strings
|
|
words := strings.Fields(s)
|
|
return words
|
|
}
|