X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/c94fef11f43279165f39680fa0b0922c86702687..HEAD:/server/files.go diff --git a/server/files.go b/server/files.go index 4d6e293..963be99 100644 --- a/server/files.go +++ b/server/files.go @@ -7,6 +7,7 @@ import ( "log" "os" "path/filepath" + "sync" ) type FileSpec struct { @@ -18,6 +19,7 @@ type AudioFiles struct { path string list []FileSpec changeWait chan bool + filesMutex sync.Mutex } var files AudioFiles @@ -25,22 +27,22 @@ var files AudioFiles func InitAudioFiles(path string) { files.changeWait = make(chan bool) files.path = path - log.Println("initing audio files") files.Refresh() - log.Println("done") } func (r *AudioFiles) Refresh() { entries, err := os.ReadDir(r.path) if err != nil { - log.Println("couldn't read dir", r.path) + log.Println("Couldn't read dir", r.path) return } + r.filesMutex.Lock() + defer r.filesMutex.Unlock() r.list = nil for _, file := range entries { f, err := os.Open(filepath.Join(r.path, file.Name())) if err != nil { - log.Println("couldn't open", file.Name()) + log.Println("Couldn't open", file.Name()) return } hash := sha256.New() @@ -57,6 +59,8 @@ func (r *AudioFiles) Path() string { } func (r *AudioFiles) Files() []FileSpec { + r.filesMutex.Lock() + defer r.filesMutex.Unlock() return r.list } @@ -68,6 +72,8 @@ func (r *AudioFiles) Delete(filename string) { } } -func (r *AudioFiles) ChangeChannel() chan bool { - return r.changeWait +func (r *AudioFiles) WatchForChanges() ([]FileSpec, chan bool) { + r.filesMutex.Lock() + defer r.filesMutex.Unlock() + return r.list, r.changeWait }