This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
100 lines (72 loc) · 1.83 KB
/
watcher.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
90
91
92
93
94
95
96
97
98
99
100
package file_write_folder_watcher
import (
"github.com/fsnotify/fsnotify"
"github.com/initialed85/eds-cctv-system/internal/file_watcher"
"log"
"path/filepath"
"strings"
"time"
)
type Watcher struct {
path string
callback func(time.Time, string)
fileWatcher file_watcher.Watcher
last map[string]string
}
func New(path string, callback func(time.Time, string)) (Watcher, error) {
last := make([]byte, 256)
for i := 0; i < 256; i++ {
last[i] = byte(i)
}
w := Watcher{
path: path,
callback: callback,
last: make(map[string]string),
}
fileWatcher, err := file_watcher.New(path, w.handle)
if err != nil {
return Watcher{}, err
}
w.fileWatcher = fileWatcher
return w, nil
}
func (w *Watcher) handle(event fsnotify.Event, timestamp time.Time) bool {
if event.Op != fsnotify.Create {
return true
}
_, fileName := filepath.Split(event.Name)
if !strings.HasPrefix(fileName, "Segment_") {
log.Printf("%v doesn't have Segment_ prefix", fileName)
return true
}
if !strings.HasSuffix(fileName, ".mp4") {
log.Printf("%v doesn't have .mp4 suffix", fileName)
return true
}
if strings.Contains(fileName, "-lowres") {
log.Printf("%v contains -lowres", fileName)
return true
}
path := event.Name
parts := strings.Split(path, "_") // path = Segment_2019-12-15_22-04-28_Driveway.mp4
suffix := parts[len(parts)-1]
lastPath, ok := w.last[suffix]
if !ok {
w.last[suffix] = path
return true
}
pathChanged := path != lastPath
log.Printf("handled path=%v, suffix=%v, lastPath=%v, pathChanged=%v", path, suffix, lastPath, pathChanged)
if pathChanged {
log.Printf("calling callback w/ %v", lastPath)
go w.callback(timestamp, lastPath)
}
w.last[suffix] = path
return true
}
func (w *Watcher) Watch() {
w.fileWatcher.Watch()
}
func (w *Watcher) Stop() {
w.fileWatcher.Stop()
}