1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +00:00
mycorrhiza/hyphae/hypha.go
Timur Ismagilov eb9acb718e Break a lot of stuff
Starring:
* Broken error localization for now (got in the way)
* The title for error pages is the same for all errors (who cares anyway)
* New bugs
* The brand new /rename/ handler
2022-02-19 19:42:32 +03:00

47 lines
1.1 KiB
Go

package hyphae
import (
"regexp"
"strings"
"sync"
)
// hyphaNamePattern is a pattern which all hyphae names must match.
var hyphaNamePattern = regexp.MustCompile(`[^?!:#@><*|"'&%{}]+`)
// IsValidName checks for invalid characters and path traversals.
func IsValidName(hyphaName string) bool {
if !hyphaNamePattern.MatchString(hyphaName) {
return false
}
for _, segment := range strings.Split(hyphaName, "/") {
if segment == ".git" || segment == ".." {
return false
}
}
return true
}
// Hypha is the hypha you know and love.
type Hypha interface {
sync.Locker
// CanonicalName returns the canonical name of the hypha.
//
// util.CanonicalName(h.CanonicalName()) == h.CanonicalName()
CanonicalName() string
}
// ByName returns a hypha by name. It returns an *EmptyHypha if there is no such hypha. This function is the only source of empty hyphae.
func ByName(hyphaName string) (h Hypha) {
byNamesMutex.Lock()
defer byNamesMutex.Unlock()
h, recorded := byNames[hyphaName]
if recorded {
return h
}
return &EmptyHypha{
canonicalName: hyphaName,
}
}