1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

Switch from snowflake to random string ID system

This commit is contained in:
Lukas SP
2020-08-24 20:22:53 +02:00
parent a1cd915759
commit 5585a26ab0
13 changed files with 97 additions and 65 deletions

View File

@@ -2,13 +2,10 @@ package pastes
import (
"github.com/Lukaesebrot/pasty/internal/env"
"math/rand"
"github.com/Lukaesebrot/pasty/internal/utils"
"strconv"
)
// deletionTokenContents represents the characters a deletion token may contain
const deletionTokenContents = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#+-.,"
// generateDeletionToken generates a new deletion token
func generateDeletionToken() (string, error) {
// Read the deletion token length
@@ -19,9 +16,5 @@ func generateDeletionToken() (string, error) {
}
// Generate the deletion token
bytes := make([]byte, length)
for i := range bytes {
bytes[i] = deletionTokenContents[rand.Int63()%int64(len(deletionTokenContents))]
}
return string(bytes), nil
return utils.RandomString(length), nil
}

View File

@@ -2,26 +2,18 @@ package pastes
import (
"github.com/alexedwards/argon2id"
"github.com/bwmarrin/snowflake"
)
func init() {
snowflakeNode, _ = snowflake.NewNode(1)
}
// snowflakeNode holds the current snowflake node
var snowflakeNode *snowflake.Node
// Paste represents a saved paste
type Paste struct {
ID snowflake.ID `json:"id" bson:"_id"`
Content string `json:"content" bson:"content"`
SuggestedSyntaxType string `json:"suggestedSyntaxType" bson:"suggestedSyntaxType"`
DeletionToken string `json:"deletionToken" bson:"deletionToken"`
ID string `json:"id" bson:"_id"`
Content string `json:"content" bson:"content"`
SuggestedSyntaxType string `json:"suggestedSyntaxType" bson:"suggestedSyntaxType"`
DeletionToken string `json:"deletionToken" bson:"deletionToken"`
}
// Create creates a new paste object using the given content
func Create(content string) (*Paste, error) {
func Create(id, content string) (*Paste, error) {
// TODO: Generate the suggested syntax type
suggestedSyntaxType := ""
@@ -33,7 +25,7 @@ func Create(content string) (*Paste, error) {
// Return the paste object
return &Paste{
ID: snowflakeNode.Generate(),
ID: id,
Content: content,
SuggestedSyntaxType: suggestedSyntaxType,
DeletionToken: deletionToken,