summaryrefslogtreecommitdiff
path: root/pkg/media
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/media')
-rw-r--r--pkg/media/library.go71
-rw-r--r--pkg/media/path.go7
-rw-r--r--pkg/media/video.go21
3 files changed, 74 insertions, 25 deletions
diff --git a/pkg/media/library.go b/pkg/media/library.go
index 3d58aab..e22f8ef 100644
--- a/pkg/media/library.go
+++ b/pkg/media/library.go
@@ -1,9 +1,10 @@
package media
import (
+ "errors"
"io/ioutil"
"log"
- "path/filepath"
+ "path"
"sort"
"strings"
"sync"
@@ -12,25 +13,44 @@ import (
// Library manages importing and retrieving video data.
type Library struct {
mu sync.RWMutex
+ Paths map[string]*Path
Videos map[string]*Video
}
// NewLibrary returns new instance of Library.
func NewLibrary() *Library {
lib := &Library{
+ Paths: make(map[string]*Path),
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
+}
+
// Import adds all valid videos from a given path.
-func (lib *Library) Import(path string) error {
- files, err := ioutil.ReadDir(path)
+func (lib *Library) Import(p *Path) error {
+ files, err := ioutil.ReadDir(p.Path)
if err != nil {
return err
}
for _, info := range files {
- err = lib.Add(path + "/" + info.Name())
+ err = lib.Add(path.Join(p.Path, info.Name()))
if err != nil {
// Ignore files that can't be parsed
continue
@@ -40,33 +60,48 @@ func (lib *Library) Import(path string) error {
}
// Add adds a single video from a given file path.
-func (lib *Library) Add(path string) error {
- v, err := ParseVideo(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)
if err != nil {
return err
}
- lib.mu.Lock()
- defer lib.mu.Unlock()
lib.Videos[v.ID] = v
- log.Println("Added:", path)
+ log.Println("Added:", v.Path)
return nil
}
// Remove removes a single video from a given file path.
-func (lib *Library) Remove(path string) {
- name := filepath.Base(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)
// ID is name without extension
- idx := strings.LastIndex(name, ".")
+ idx := strings.LastIndex(n, ".")
if idx == -1 {
- idx = len(name)
+ idx = len(n)
}
- id := name[:idx]
- lib.mu.Lock()
- defer lib.mu.Unlock()
- _, ok := lib.Videos[id]
+ id := n[:idx]
+ if len(p.Prefix) > 0 {
+ id = path.Join(p.Prefix, id)
+ }
+ v, ok := lib.Videos[id]
if ok {
delete(lib.Videos, id)
- log.Println("Removed:", path)
+ log.Println("Removed:", v.Path)
}
}
diff --git a/pkg/media/path.go b/pkg/media/path.go
new file mode 100644
index 0000000..1248ed5
--- /dev/null
+++ b/pkg/media/path.go
@@ -0,0 +1,7 @@
+package media
+
+// Path represents a media library path.
+type Path struct {
+ Path string
+ Prefix string
+}
diff --git a/pkg/media/video.go b/pkg/media/video.go
index 90ec595..24f89b7 100644
--- a/pkg/media/video.go
+++ b/pkg/media/video.go
@@ -2,6 +2,7 @@ package media
import (
"os"
+ "path"
"strings"
"time"
@@ -18,12 +19,14 @@ type Video struct {
ThumbType string
Modified string
Size int64
+ Path string
Timestamp time.Time
}
// ParseVideo parses a video file's metadata and returns a Video.
-func ParseVideo(path string) (*Video, error) {
- f, err := os.Open(path)
+func ParseVideo(p *Path, name string) (*Video, error) {
+ pth := path.Join(p.Path, name)
+ f, err := os.Open(pth)
if err != nil {
return nil, err
}
@@ -34,13 +37,16 @@ func ParseVideo(path string) (*Video, error) {
size := info.Size()
timestamp := info.ModTime()
modified := timestamp.Format("2006-01-02 03:04 PM")
- name := info.Name()
// ID is name without extension
idx := strings.LastIndex(name, ".")
if idx == -1 {
idx = len(name)
}
id := name[:idx]
+ if len(p.Prefix) > 0 {
+ // if there's a prefix prepend it to the ID
+ id = path.Join(p.Prefix, name[:idx])
+ }
m, err := tag.ReadFrom(f)
if err != nil {
return nil, err
@@ -57,13 +63,14 @@ func ParseVideo(path string) (*Video, error) {
Description: m.Comment(),
Modified: modified,
Size: size,
+ Path: pth,
Timestamp: timestamp,
}
// Add thumbnail (if exists)
- p := m.Picture()
- if p != nil {
- v.Thumb = p.Data
- v.ThumbType = p.MIMEType
+ pic := m.Picture()
+ if pic != nil {
+ v.Thumb = pic.Data
+ v.ThumbType = pic.MIMEType
}
return v, nil
}