-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
62 lines (56 loc) · 1.14 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
package main
import (
"context"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
log "github.com/sirupsen/logrus"
)
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func checkErr(err error) {
if err != nil {
if err == context.DeadlineExceeded {
log.Errorln("Timeout exceeded")
} else {
log.Errorln("checkErr", err)
}
os.Exit(1)
}
}
func streamFiles(dir string) {
fs := http.FileServer(http.Dir(dir))
http.Handle("/", fs)
log.Println("Listening...")
http.ListenAndServe(":3099", nil)
}
func scanMedia(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil
}
medias := []string{}
for _, file := range files {
name := file.Name()
if strings.HasSuffix(name, ".mp3") || strings.HasSuffix(name, ".m4a") {
log.Println("Discovered", name)
medias = append(medias, name)
}
}
return medias
}