database, config, home page, start of upload endpoint

This commit is contained in:
2026-11-08 19:25:52 -05:00
parent 729d332a61
commit 1c99c5dd11
8 changed files with 400 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
package main
import (
"crypto/rand"
"math/big"
"path/filepath"
"strings"
)
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
// generates a secure random id,
// length must be either 10 or 16
func generateId(length int) string {
if length != 10 && length != 16 {
panic("length must be 10 or 16")
}
bytes := make([]byte, length)
max := big.NewInt(int64(len(alphabet)))
for i := range bytes {
rr, err := rand.Int(rand.Reader, max)
if err != nil {
panic(err)
}
bytes[i] = alphabet[rr.Int64()]
}
return string(bytes)
}
func validPasteId(id string) bool {
if len(id) != 10 && len(id) != 16 {
return false
}
// check if it uses our alphabet
for _, r := range id {
if !strings.ContainsRune(alphabet, r) {
return false
}
}
return true
}
func getStoragePath(uploadDir, id string) string {
return filepath.Join(uploadDir, id[:2], id[2:4], id)
}