-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
52 lines (39 loc) · 887 Bytes
/
io.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
package main
import (
"errors"
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
)
func getJpegFilesInDirectory(directory string) []string {
var files []string
filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return nil
}
fileExt := filepath.Ext(path)
if !info.IsDir() && (fileExt == ".jpg" || fileExt == ".jpeg") {
files = append(files, path)
}
return nil
})
return files
}
func getRandIndexInArray(array []string) (int, error) {
if len(array) == 0 {
return -1, errors.New("Empty list")
}
rand.Seed(time.Now().Unix())
return rand.Intn(len(array)), nil
}
func GetJpegPathInDirectory(directory string) (string, error) {
files := getJpegFilesInDirectory(directory)
rInd, err := getRandIndexInArray(files)
if err != nil {
return "", errEmptyDir
}
return files[rInd], nil
}