diff options
Diffstat (limited to 'storage.go')
-rw-r--r-- | storage.go | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -3,9 +3,11 @@ package main import ( "crypto/sha1" "encoding/hex" + "errors" "io/ioutil" "net/http" "os" + "regexp" "sort" ) @@ -13,6 +15,10 @@ type Storage struct { Path string } +var ( + tokenPattern = regexp.MustCompile(`[a-fA-F0-9]{10,}`) +) + func (s *Storage) MustInit() error { os.Mkdir(s.Path, 0755) os.Mkdir(s.Path+"/battles", 0755) @@ -20,6 +26,14 @@ func (s *Storage) MustInit() error { return nil } +func (s *Storage) CanPost(token string) (bool, error) { + if !tokenPattern.MatchString(token) { + return false, errors.New("invalid token") + } + _, err := os.Stat(s.Path + "/tokens/" + token) + return err == nil, nil +} + func (s *Storage) AddBattle(data []byte) (string, error) { hash := sha1.Sum(data) id := hex.EncodeToString(hash[:]) |