tube/pkg/media/library.go

121 lines
2.3 KiB
Go
Raw Normal View History

2019-06-26 22:02:31 +03:00
package media
import (
"errors"
2019-06-26 22:02:31 +03:00
"io/ioutil"
2019-06-29 01:20:52 +03:00
"log"
"path"
2019-06-26 22:02:31 +03:00
"sort"
"strings"
2019-06-29 01:20:52 +03:00
"sync"
2019-06-26 22:02:31 +03:00
)
2019-06-30 01:02:05 +03:00
// Library manages importing and retrieving video data.
2019-06-26 22:02:31 +03:00
type Library struct {
2019-06-29 01:20:52 +03:00
mu sync.RWMutex
Paths map[string]*Path
2019-06-26 22:02:31 +03:00
Videos map[string]*Video
}
2019-06-30 01:02:05 +03:00
// NewLibrary returns new instance of Library.
2019-06-26 22:02:31 +03:00
func NewLibrary() *Library {
lib := &Library{
Paths: make(map[string]*Path),
2019-06-26 22:02:31 +03:00
Videos: make(map[string]*Video),
}
return lib
}
// AddPath adds a media path to the library.
func (lib *Library) AddPath(p *Path) error {
lib.mu.Lock()
defer lib.mu.Unlock()
// make sure new path doesn't collide with existing ones
for _, p2 := range lib.Paths {
if p.Path == p2.Path {
return errors.New("media: duplicate library path")
}
if p.Prefix == p2.Prefix {
return errors.New("media: duplicate library prefix")
}
}
lib.Paths[p.Path] = p
return nil
}
2019-06-30 01:02:05 +03:00
// Import adds all valid videos from a given path.
func (lib *Library) Import(p *Path) error {
files, err := ioutil.ReadDir(p.Path)
2019-06-26 22:02:31 +03:00
if err != nil {
return err
}
for _, info := range files {
err = lib.Add(path.Join(p.Path, info.Name()))
2019-06-26 22:02:31 +03:00
if err != nil {
// Ignore files that can't be parsed
continue
}
}
return nil
}
2019-06-30 01:02:05 +03:00
// Add adds a single video from a given file path.
func (lib *Library) Add(filepath string) error {
lib.mu.Lock()
defer lib.mu.Unlock()
d := path.Dir(filepath)
p, ok := lib.Paths[d]
if !ok {
log.Println(d)
return errors.New("media: path not found")
}
n := path.Base(filepath)
v, err := ParseVideo(p, n)
2019-06-29 01:20:52 +03:00
if err != nil {
return err
}
lib.Videos[v.ID] = v
log.Println("Added:", v.Path)
2019-06-29 01:20:52 +03:00
return nil
}
2019-06-30 01:02:05 +03:00
// Remove removes a single video from a given file path.
func (lib *Library) Remove(filepath string) {
lib.mu.Lock()
defer lib.mu.Unlock()
d := path.Dir(filepath)
p, ok := lib.Paths[d]
if !ok {
return
}
n := path.Base(filepath)
2019-06-29 01:20:52 +03:00
// ID is name without extension
idx := strings.LastIndex(n, ".")
2019-06-29 01:20:52 +03:00
if idx == -1 {
idx = len(n)
2019-06-29 01:20:52 +03:00
}
id := n[:idx]
if len(p.Prefix) > 0 {
id = path.Join(p.Prefix, id)
}
v, ok := lib.Videos[id]
2019-06-29 01:20:52 +03:00
if ok {
delete(lib.Videos, id)
log.Println("Removed:", v.Path)
2019-06-29 01:20:52 +03:00
}
}
2019-06-30 01:02:05 +03:00
// Playlist returns a sorted Playlist of all videos.
2019-06-26 22:02:31 +03:00
func (lib *Library) Playlist() Playlist {
2019-06-29 01:20:52 +03:00
lib.mu.RLock()
defer lib.mu.RUnlock()
2019-07-03 15:54:22 +03:00
pl := make(Playlist, len(lib.Videos))
i := 0
2019-06-26 22:02:31 +03:00
for _, v := range lib.Videos {
2019-07-03 15:54:22 +03:00
pl[i] = v
i++
2019-06-26 22:02:31 +03:00
}
sort.Sort(pl)
return pl
}