-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
90 lines (76 loc) · 1.49 KB
/
sync.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
package sync
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
func listAACFiles(dir string) ([]string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
var aacFiles []string
for _, file := range files {
if !file.IsDir() {
ext := filepath.Ext(file.Name())
if ext == ".aac" {
aacFiles = append(aacFiles, file.Name())
}
}
}
return aacFiles, nil
}
func listenForChangesToNotes(watchDir string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("Error:", err)
return
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
fmt.Println(event.Name)
if event.Op&fsnotify.Create == fsnotify.Create {
ext := filepath.Ext(event.Name)
if ext == ".aac" && filepath.Dir(event.Name) == "assets" {
processAudio(event.Name)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
fmt.Println("Error:", err)
}
}
}()
err = watcher.Add(watchDir)
if err != nil {
fmt.Println("Error:", err)
return
}
<-done
}
func processAudio(filename string) {
fmt.Println("Processing audio:", filename)
// do something with the audio file
}
func WatchForAudioFiles(notesDir string) error {
dir := os.Args[1]
files, err := listAACFiles(dir)
if err != nil {
return err
}
for _, file := range files {
println("getting transcript for file", file)
}
return nil
}