From 3f941de9c88585cc299a9201ac99f75776134860 Mon Sep 17 00:00:00 2001 From: Stephen Searles Date: Wed, 6 Sep 2017 09:11:06 -0500 Subject: [PATCH] fixing comment keying, fixing comment storage location --- comments/comments.go | 23 +++++++++++++++++------ setup.go | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/comments/comments.go b/comments/comments.go index 3f4bc44..990a711 100644 --- a/comments/comments.go +++ b/comments/comments.go @@ -8,6 +8,7 @@ import ( "html/template" "io" "net/http" + "strconv" "sync" "time" @@ -175,17 +176,27 @@ func (d *diskvStorage) nextKey(post string) (string, error) { d.keyMtx.Lock() defer d.keyMtx.Unlock() - var i uint64 - prefix := post + "-" + lastKey := prefix + keys := d.KeysPrefix(prefix, nil) - for range keys { - i++ + for key := range keys { + lastKey = key + } + + if lastKey == prefix { + return prefix + "1", nil + } + + minusPrefix := lastKey[len(prefix):] + lastIdx, err := strconv.Atoi(minusPrefix) + if err != nil { + return "", fmt.Errorf("comment key %q seems malformed", lastKey) } - key := prefix + fmt.Sprint(i) - err := d.WriteStream(key, &bytes.Buffer{}, true) + key := prefix + fmt.Sprint(lastIdx+1) + err = d.WriteStream(key, &bytes.Buffer{}, true) return key, err } diff --git a/setup.go b/setup.go index 317f056..17994cc 100644 --- a/setup.go +++ b/setup.go @@ -38,7 +38,7 @@ func (ch *CaddyHugo) commentsSetting(c *caddy.Controller) { if c.Val() == "hugo" { for c.NextBlock() { if c.Val() == "comments" { - ch.Comments = comments.Default() + ch.Comments = comments.WithStorage(comments.NewDiskv(path.Join(ch.Site.Root, "comments"))) if c.NextArg() { ch.Comments.Password = c.Val() }