package storage 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) }