diff --git a/store/random.go b/store/random.go new file mode 100644 index 0000000..01a9cd7 --- /dev/null +++ b/store/random.go @@ -0,0 +1,22 @@ +package store + +import ( + "crypto/rand" +) + +func Generate62RandomString(l int) string { + return GenerateRandomString("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", l) +} + +func GenerateFriendlyRandomString(l int) string { + return GenerateRandomString("0123456789abcdefghijklmnopqrstuvwxyz", l) +} + +func GenerateRandomString(dictionary string, l int) string { + var bytes = make([]byte, l) + rand.Read(bytes) + for k, v := range bytes { + bytes[k] = dictionary[v%byte(len(dictionary))] + } + return string(bytes) +} diff --git a/store/store.go b/store/store.go index df76508..ca95a13 100644 --- a/store/store.go +++ b/store/store.go @@ -4,11 +4,10 @@ import ( "os" "io" "bytes" - "crypto/rand" ) const ( - nameLen = 12 + FriendlyIdLen = 12 ) func SavePost(outDir string, post []byte) (string, error) { @@ -33,12 +32,5 @@ func SavePost(outDir string, post []byte) (string, error) { } func generateFileName() string { - c := nameLen - var dictionary string = "0123456789abcdefghijklmnopqrstuvwxyz" - var bytes = make([]byte, c) - rand.Read(bytes) - for k, v := range bytes { - bytes[k] = dictionary[v%byte(len(dictionary))] - } - return string(bytes) + return GenerateFriendlyRandomString(FriendlyIdLen) }