diff options
| author | davy <davy.wybiral@gmail.com> | 2019-06-28 17:20:52 -0500 |
|---|---|---|
| committer | davy <davy.wybiral@gmail.com> | 2019-06-28 17:20:52 -0500 |
| commit | b46830c8806fc1293bc8cfdf5496cd024fcde6c4 (patch) | |
| tree | 409ac133758cd0e745fe930e0d30e5821e8e4190 /pkg/media | |
| parent | ef01f99df7dafc61c11203e879e997c15dea98ca (diff) | |
added app.Watcher
Diffstat (limited to 'pkg/media')
| -rw-r--r-- | pkg/media/library.go | 51 | ||||
| -rw-r--r-- | pkg/media/video.go | 22 |
2 files changed, 57 insertions, 16 deletions
diff --git a/pkg/media/library.go b/pkg/media/library.go index b18fe3a..337907d 100644 --- a/pkg/media/library.go +++ b/pkg/media/library.go @@ -2,11 +2,15 @@ package media import ( "io/ioutil" + "log" + "path/filepath" "sort" "strings" + "sync" ) type Library struct { + mu sync.RWMutex Videos map[string]*Video } @@ -23,30 +27,47 @@ func (lib *Library) Import(path string) error { return err } for _, info := range files { - name := info.Name() - v, err := ParseVideo(path + "/" + name) + err = lib.Add(path + "/" + info.Name()) if err != nil { // Ignore files that can't be parsed continue } - // Set modified date property - v.Modified = info.ModTime().Format("2006-01-02") - // Default title is filename - if v.Title == "" { - v.Title = name - } - // ID is name without extension - idx := strings.LastIndex(name, ".") - if idx == -1 { - idx = len(name) - } - v.ID = name[:idx] - lib.Videos[v.ID] = v } return nil } +func (lib *Library) Add(path string) error { + v, err := ParseVideo(path) + if err != nil { + return err + } + lib.mu.Lock() + defer lib.mu.Unlock() + lib.Videos[v.ID] = v + log.Println("Added:", path) + return nil +} + +func (lib *Library) Remove(path string) { + name := filepath.Base(path) + // ID is name without extension + idx := strings.LastIndex(name, ".") + if idx == -1 { + idx = len(name) + } + id := name[:idx] + lib.mu.Lock() + defer lib.mu.Unlock() + _, ok := lib.Videos[id] + if ok { + delete(lib.Videos, id) + log.Println("Removed:", path) + } +} + func (lib *Library) Playlist() Playlist { + lib.mu.RLock() + defer lib.mu.RUnlock() pl := make(Playlist, 0) for _, v := range lib.Videos { pl = append(pl, v) diff --git a/pkg/media/video.go b/pkg/media/video.go index bd0def8..f79ab44 100644 --- a/pkg/media/video.go +++ b/pkg/media/video.go @@ -2,6 +2,7 @@ package media import ( "os" + "strings" "github.com/dhowden/tag" ) @@ -21,14 +22,33 @@ func ParseVideo(path string) (*Video, error) { if err != nil { return nil, err } + info, err := f.Stat() + if err != nil { + return nil, err + } + modified := info.ModTime().Format("2006-01-02") + name := info.Name() + // ID is name without extension + idx := strings.LastIndex(name, ".") + if idx == -1 { + idx = len(name) + } + id := name[:idx] m, err := tag.ReadFrom(f) if err != nil { return nil, err } + title := m.Title() + // Default title is filename + if title == "" { + title = name + } v := &Video{ - Title: m.Title(), + ID: id, + Title: title, Album: m.Album(), Description: m.Comment(), + Modified: modified, } // Add thumbnail (if exists) p := m.Picture() |
