-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
89 lines (74 loc) · 1.87 KB
/
files.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
func scanDir(dirName string) ([]string, []string, error) {
// check if dir exists
if _, err := os.Stat(dirName); os.IsNotExist(err) {
log.Errorf("Directory %s does not exist", dirName)
return []string{}, []string{}, err
}
// check if dirName ends with slash
if !strings.HasSuffix(dirName, "/") {
dirName += "/"
}
var files, dirs []string
dirFiles, err := ioutil.ReadDir(dirName)
if err != nil {
log.Errorf("Unable to scan dir %s: %v", dirName, err)
return []string{}, []string{}, err
}
for _, f := range dirFiles {
if f.IsDir() {
dirs = append(dirs, f.Name())
continue
}
files = append(files, f.Name())
}
log.Infof("Successfully scanned %s", dirName)
return files, dirs, nil
}
func getVideos(dirName string) []string {
videoExtensions := []string{".mp4", ".mkv", ".mov", ".avi", "flv", "wmv"}
videoFiles := getFilesByType(dirName, videoExtensions)
return videoFiles
}
func getFilesByType(dirName string, extensions []string) []string {
// check if dir exists
if _, err := os.Stat(dirName); os.IsNotExist(err) {
log.Errorf("Directory %s does not exist", dirName)
return []string{}
}
// check if dirName ends with slash
if !strings.HasSuffix(dirName, "/") {
dirName += "/"
}
var files []string
err := filepath.Walk(dirName, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if stringInSlice(filepath.Ext(path), extensions) {
files = append(files, strings.Replace(path, dirName, "", -1))
}
return nil
})
if err != nil {
log.Errorf("Unable to scan dir %s: %v", dirName, err)
return []string{}
}
log.Infof("Successfully scanned %s", dirName)
return files
}
func stringInSlice(str string, list []string) bool {
for _, x := range list {
if x == str {
return true
}
}
return false
}