-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
45 lines (36 loc) · 916 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
)
func randomString() string {
rand.Seed(time.Now().Unix())
var output strings.Builder
charSet := []rune("abcdedfghijklmnopqrstABCDEFGHIJKLMNOP01234567890")
length := 20
for i := 0; i < length; i++ {
random := rand.Intn(len(charSet))
randomChar := charSet[random]
output.WriteRune(randomChar)
}
return output.String()
}
func doesExist(filename string) bool {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}
func generateFilename(imgDir string) (string, string) {
filename := fmt.Sprintf("%s.%s", randomString(), "jpg")
filepath := fmt.Sprintf("%s/%s", imgDir, filename)
for doesExist(filepath) != false {
fmt.Print("Regenerating file name.")
filename = fmt.Sprintf("%s.%s", randomString(), "jpg")
filepath = fmt.Sprintf("%s/%s", imgDir, filename)
}
return filename, filepath
}