-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
65 lines (56 loc) · 1.42 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
)
func generateRandomNumber(min, max int) int {
rand.Seed(time.Now().UnixNano())
diff := max - min
if diff <= 0 {
return rand.Intn(max)
}
return rand.Intn(diff) + min
}
func generateWidthAndHeight(mw, mh int) (width, height int) {
width = generateRandomNumber(0, mw)
height = generateRandomNumber(0, mh)
return
}
func generateRGB() (r, g, b float64) {
rand.Seed(time.Now().UnixNano())
min := 0
max := 255
r = float64(rand.Intn(max-min) + min)
g = float64(rand.Intn(max-min) + min)
b = float64(rand.Intn(max-min) + min)
return
}
func getFileDetails(path string) (filename, ext, directory string) {
f, err := os.Open(path)
if err != nil {
log.Fatalf("could not open %q\n", path)
}
defer f.Close()
ext = filepath.Ext(path)
filename = strings.TrimSuffix(filepath.Base(path), ext)
filename = strings.Replace(filename, " ", "_", -1)
directory = fmt.Sprintf("%s/%s", config.Path, filename)
return
}
func createFileName(file, ext string, index uint) string {
currentTime := time.Now()
date := currentTime.Format("2006-01-02")
unixTime := currentTime.Unix()
return fmt.Sprintf("%s_%s_%d_%d%s", file, date, index+1, unixTime, ext)
}
func createGifName(file string) string {
currentTime := time.Now()
date := currentTime.Format("2006-01-02")
unixTime := currentTime.Unix()
return fmt.Sprintf("%s_%s_%d", file, date, unixTime)
}