2018-09-18 22:57:25 +02:00
|
|
|
package namer
|
2018-09-18 21:20:34 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2021-03-04 03:04:52 +01:00
|
|
|
var claimNameRegexp = regexp.MustCompile(`[=&#:$@%?;\\"/<>%{}|^~\x60[\]\s]`)
|
2018-09-18 21:20:34 +02:00
|
|
|
|
|
|
|
type Namer struct {
|
|
|
|
mu *sync.Mutex
|
|
|
|
names map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNamer() *Namer {
|
|
|
|
return &Namer{
|
|
|
|
mu: &sync.Mutex{},
|
|
|
|
names: make(map[string]bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:08:18 +02:00
|
|
|
func (n *Namer) SetNames(names map[string]bool) {
|
|
|
|
n.names = names
|
|
|
|
}
|
|
|
|
|
2018-09-18 21:20:34 +02:00
|
|
|
func (n *Namer) GetNextName(prefix string) string {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
|
|
|
|
attempt := 1
|
|
|
|
var name string
|
|
|
|
for {
|
|
|
|
name = getClaimNameFromTitle(prefix, attempt)
|
|
|
|
if _, exists := n.names[name]; !exists {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
attempt++
|
|
|
|
}
|
|
|
|
|
|
|
|
//if for some reasons the title can't be converted in a valid claim name (too short or not latin) then we use a hash
|
2019-09-10 11:43:20 +02:00
|
|
|
attempt = 1
|
2018-09-18 21:20:34 +02:00
|
|
|
if len(name) < 2 {
|
2018-09-18 22:57:25 +02:00
|
|
|
sum := md5.Sum([]byte(prefix))
|
2019-09-10 11:43:20 +02:00
|
|
|
for {
|
|
|
|
name = fmt.Sprintf("%s-%d", hex.EncodeToString(sum[:])[:15], attempt)
|
|
|
|
if _, exists := n.names[name]; !exists {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
attempt++
|
|
|
|
}
|
2018-09-18 21:20:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
n.names[name] = true
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: clean this up some
|
|
|
|
func getClaimNameFromTitle(title string, attempt int) string {
|
|
|
|
suffix := ""
|
|
|
|
if attempt > 1 {
|
|
|
|
suffix = "-" + strconv.Itoa(attempt)
|
|
|
|
}
|
|
|
|
maxLen := 40 - len(suffix)
|
|
|
|
|
2021-03-04 03:04:52 +01:00
|
|
|
chunks := strings.Split(strings.ToLower(strings.Trim(claimNameRegexp.ReplaceAllString(title, "-"), "-")), "-")
|
2018-09-18 21:20:34 +02:00
|
|
|
|
|
|
|
name := chunks[0]
|
|
|
|
if len(name) > maxLen {
|
|
|
|
return name[:maxLen]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chunk := range chunks[1:] {
|
2021-03-04 03:04:52 +01:00
|
|
|
if chunk == "" {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-18 21:20:34 +02:00
|
|
|
tmpName := name + "-" + chunk
|
|
|
|
if len(tmpName) > maxLen {
|
|
|
|
if len(name) < 20 {
|
|
|
|
name = tmpName[:maxLen]
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
name = tmpName
|
|
|
|
}
|
|
|
|
|
|
|
|
return name + suffix
|
|
|
|
}
|