upload: return error if the short IDs collide for whatever reason

This commit is contained in:
mappu 2023-05-17 18:54:30 +12:00
parent 4294738337
commit e26a3b58b0

View File

@ -3,6 +3,7 @@ package contented
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -62,9 +63,17 @@ func (this *Server) AddMetadata(m Metadata) (string, error) {
err = this.db.Update(func(tx *bolt.Tx) error { err = this.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(this.metadataBucket) b := tx.Bucket(this.metadataBucket)
seq, _ := b.NextSequence() // cannot fail seq, err := b.NextSequence()
if err != nil {
return fmt.Errorf("NextSequence: %w", err)
}
shortRef = idToString(seq) shortRef = idToString(seq)
return tx.Bucket(this.metadataBucket).Put([]byte(shortRef), jb)
if b.Get([]byte(shortRef)) != nil {
return fmt.Errorf("Next bucket sequence %d creates colliding ID %s", seq, shortRef)
}
return b.Put([]byte(shortRef), jb)
}) })
if err != nil { if err != nil {
return "", err return "", err