tube/media/library.go

131 lines
2.6 KiB
Go
Raw Normal View History

2019-06-26 22:02:31 +03:00
package media
import (
"errors"
"fmt"
2019-06-26 22:02:31 +03:00
"io/ioutil"
2019-06-29 01:20:52 +03:00
"log"
"os"
"path"
2019-08-09 00:13:55 +03:00
"path/filepath"
2019-06-26 22:02:31 +03:00
"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")
}
}
if err := os.MkdirAll(p.Path, 07550); err != nil {
return fmt.Errorf("error creating library path %s: %w", p.Path, err)
}
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 {
if strings.ContainsAny(info.Name(), "#") {
// ignore resized videos e.g: #240p.mp4
continue
}
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.
2019-08-09 00:13:55 +03:00
func (lib *Library) Add(fp string) error {
lib.mu.Lock()
defer lib.mu.Unlock()
2019-08-09 00:13:55 +03:00
fp = filepath.ToSlash(fp)
d := path.Dir(fp)
p, ok := lib.Paths[d]
if !ok {
return errors.New("media: path not found")
}
2019-08-09 00:13:55 +03:00
n := path.Base(fp)
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.
2019-08-09 00:13:55 +03:00
func (lib *Library) Remove(fp string) {
lib.mu.Lock()
defer lib.mu.Unlock()
2019-08-09 00:13:55 +03:00
fp = filepath.ToSlash(fp)
d := path.Dir(fp)
p, ok := lib.Paths[d]
if !ok {
return
}
2019-08-09 00:13:55 +03:00
n := path.Base(fp)
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
}
2020-03-25 09:26:30 +03:00
By(SortByTimestamp).Sort(pl)
2019-06-26 22:02:31 +03:00
return pl
}