2019-06-26 22:02:31 +03:00
|
|
|
package media
|
|
|
|
|
|
|
|
import (
|
2019-07-03 21:25:42 +03:00
|
|
|
"errors"
|
2020-03-29 12:02:52 +03:00
|
|
|
"fmt"
|
2019-06-26 22:02:31 +03:00
|
|
|
"io/ioutil"
|
2019-06-29 01:20:52 +03:00
|
|
|
"log"
|
2020-03-29 12:02:52 +03:00
|
|
|
"os"
|
2019-07-03 21:25:42 +03:00
|
|
|
"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
|
2019-07-03 21:25:42 +03:00
|
|
|
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{
|
2019-07-03 21:25:42 +03:00
|
|
|
Paths: make(map[string]*Path),
|
2019-06-26 22:02:31 +03:00
|
|
|
Videos: make(map[string]*Video),
|
|
|
|
}
|
|
|
|
return lib
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:25:42 +03:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 06:22:17 +03:00
|
|
|
if err := os.MkdirAll(p.Path, 0755); err != nil {
|
2020-03-29 12:02:52 +03:00
|
|
|
return fmt.Errorf("error creating library path %s: %w", p.Path, err)
|
|
|
|
}
|
2019-07-03 21:25:42 +03:00
|
|
|
lib.Paths[p.Path] = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// Import adds all valid videos from a given path.
|
2019-07-03 21:25:42 +03:00
|
|
|
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 {
|
2020-03-28 08:59:16 +03:00
|
|
|
if strings.ContainsAny(info.Name(), "#") {
|
|
|
|
// ignore resized videos e.g: #240p.mp4
|
|
|
|
continue
|
|
|
|
}
|
2019-07-03 21:25:42 +03:00
|
|
|
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 {
|
2019-07-03 21:25:42 +03:00
|
|
|
lib.mu.Lock()
|
|
|
|
defer lib.mu.Unlock()
|
2019-08-09 00:13:55 +03:00
|
|
|
fp = filepath.ToSlash(fp)
|
|
|
|
d := path.Dir(fp)
|
2019-07-03 21:25:42 +03:00
|
|
|
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)
|
2019-07-03 21:25:42 +03:00
|
|
|
v, err := ParseVideo(p, n)
|
2019-06-29 01:20:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lib.Videos[v.ID] = v
|
2019-07-03 21:25:42 +03:00
|
|
|
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) {
|
2019-07-03 21:25:42 +03:00
|
|
|
lib.mu.Lock()
|
|
|
|
defer lib.mu.Unlock()
|
2019-08-09 00:13:55 +03:00
|
|
|
fp = filepath.ToSlash(fp)
|
|
|
|
d := path.Dir(fp)
|
2019-07-03 21:25:42 +03:00
|
|
|
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
|
2019-07-03 21:25:42 +03:00
|
|
|
idx := strings.LastIndex(n, ".")
|
2019-06-29 01:20:52 +03:00
|
|
|
if idx == -1 {
|
2019-07-03 21:25:42 +03:00
|
|
|
idx = len(n)
|
2019-06-29 01:20:52 +03:00
|
|
|
}
|
2019-07-03 21:25:42 +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)
|
2019-07-03 21:25:42 +03:00
|
|
|
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
|
|
|
|
}
|