Multipath (#10)

* added multiple paths

* prevent path duplicates
This commit is contained in:
davy wybiral 2019-07-03 13:25:42 -05:00 committed by GitHub
parent 4fc389edcb
commit deb6808c75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 126 additions and 38 deletions

View file

@ -1,5 +1,10 @@
{ {
"library": "videos", "library": [
{
"path": "videos",
"prefix": ""
}
],
"server": { "server": {
"host": "127.0.0.1", "host": "127.0.0.1",
"port": 0 "port": 0

View file

@ -56,8 +56,11 @@ func NewApp(cfg *Config) (*App, error) {
r := mux.NewRouter().StrictSlash(true) r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/", a.indexHandler).Methods("GET") r.HandleFunc("/", a.indexHandler).Methods("GET")
r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET") r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET")
r.HandleFunc("/v/{prefix}/{id}.mp4", a.videoHandler).Methods("GET")
r.HandleFunc("/t/{id}", a.thumbHandler).Methods("GET") r.HandleFunc("/t/{id}", a.thumbHandler).Methods("GET")
r.HandleFunc("/t/{prefix}/{id}", a.thumbHandler).Methods("GET")
r.HandleFunc("/v/{id}", a.pageHandler).Methods("GET") r.HandleFunc("/v/{id}", a.pageHandler).Methods("GET")
r.HandleFunc("/v/{prefix}/{id}", a.pageHandler).Methods("GET")
r.HandleFunc("/feed.xml", a.rssHandler).Methods("GET") r.HandleFunc("/feed.xml", a.rssHandler).Methods("GET")
// Static file handler // Static file handler
fsHandler := http.StripPrefix( fsHandler := http.StripPrefix(
@ -71,12 +74,21 @@ func NewApp(cfg *Config) (*App, error) {
// Run imports the library and starts server. // Run imports the library and starts server.
func (a *App) Run() error { func (a *App) Run() error {
path := a.Config.LibraryPath for _, pc := range a.Config.Library {
err := a.Library.Import(path) p := &media.Path{
if err != nil { Path: pc.Path,
return err Prefix: pc.Prefix,
}
err := a.Library.AddPath(p)
if err != nil {
return err
}
err = a.Library.Import(p)
if err != nil {
return err
}
a.Watcher.Add(p.Path)
} }
a.Watcher.Add(path)
go a.watch() go a.watch()
return http.Serve(a.Listener, a.Router) return http.Serve(a.Listener, a.Router)
} }
@ -124,6 +136,10 @@ func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) { func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
prefix, ok := vars["prefix"]
if ok {
id = path.Join(prefix, id)
}
log.Printf("/v/%s", id) log.Printf("/v/%s", id)
playing, ok := a.Library.Videos[id] playing, ok := a.Library.Videos[id]
if !ok { if !ok {
@ -150,6 +166,10 @@ func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) { func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
prefix, ok := vars["prefix"]
if ok {
id = path.Join(prefix, id)
}
log.Printf("/v/%s", id) log.Printf("/v/%s", id)
m, ok := a.Library.Videos[id] m, ok := a.Library.Videos[id]
if !ok { if !ok {
@ -159,14 +179,17 @@ func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) {
disposition := "attachment; filename=\"" + title + ".mp4\"" disposition := "attachment; filename=\"" + title + ".mp4\""
w.Header().Set("Content-Disposition", disposition) w.Header().Set("Content-Disposition", disposition)
w.Header().Set("Content-Type", "video/mp4") w.Header().Set("Content-Type", "video/mp4")
path := a.Config.LibraryPath + "/" + id + ".mp4" http.ServeFile(w, r, m.Path)
http.ServeFile(w, r, path)
} }
// HTTP handler for /t/id // HTTP handler for /t/id
func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) { func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
prefix, ok := vars["prefix"]
if ok {
id = path.Join(prefix, id)
}
log.Printf("/t/%s", id) log.Printf("/t/%s", id)
m, ok := a.Library.Videos[id] m, ok := a.Library.Videos[id]
if !ok { if !ok {

View file

@ -7,9 +7,15 @@ import (
// Config settings for main App. // Config settings for main App.
type Config struct { type Config struct {
LibraryPath string `json:"library"` Library []*PathConfig `json:"library"`
Server *ServerConfig `json:"server"` Server *ServerConfig `json:"server"`
Feed *FeedConfig `json:"feed"` Feed *FeedConfig `json:"feed"`
}
// PathConfig settings for media library path.
type PathConfig struct {
Path string `json:"path"`
Prefix string `json:"prefix"`
} }
// ServerConfig settings for App Server. // ServerConfig settings for App Server.
@ -34,7 +40,12 @@ type FeedConfig struct {
// DefaultConfig returns Config initialized with default values. // DefaultConfig returns Config initialized with default values.
func DefaultConfig() *Config { func DefaultConfig() *Config {
return &Config{ return &Config{
LibraryPath: "videos", Library: []*PathConfig{
&PathConfig{
Path: "videos",
Prefix: "",
},
},
Server: &ServerConfig{ Server: &ServerConfig{
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 0, Port: 0,

View file

@ -1,9 +1,10 @@
package media package media
import ( import (
"errors"
"io/ioutil" "io/ioutil"
"log" "log"
"path/filepath" "path"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -12,25 +13,44 @@ import (
// Library manages importing and retrieving video data. // Library manages importing and retrieving video data.
type Library struct { type Library struct {
mu sync.RWMutex mu sync.RWMutex
Paths map[string]*Path
Videos map[string]*Video Videos map[string]*Video
} }
// NewLibrary returns new instance of Library. // NewLibrary returns new instance of Library.
func NewLibrary() *Library { func NewLibrary() *Library {
lib := &Library{ lib := &Library{
Paths: make(map[string]*Path),
Videos: make(map[string]*Video), Videos: make(map[string]*Video),
} }
return lib 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. // Import adds all valid videos from a given path.
func (lib *Library) Import(path string) error { func (lib *Library) Import(p *Path) error {
files, err := ioutil.ReadDir(path) files, err := ioutil.ReadDir(p.Path)
if err != nil { if err != nil {
return err return err
} }
for _, info := range files { for _, info := range files {
err = lib.Add(path + "/" + info.Name()) err = lib.Add(path.Join(p.Path, info.Name()))
if err != nil { if err != nil {
// Ignore files that can't be parsed // Ignore files that can't be parsed
continue continue
@ -40,33 +60,48 @@ func (lib *Library) Import(path string) error {
} }
// Add adds a single video from a given file path. // Add adds a single video from a given file path.
func (lib *Library) Add(path string) error { func (lib *Library) Add(filepath string) error {
v, err := ParseVideo(path) 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 { if err != nil {
return err return err
} }
lib.mu.Lock()
defer lib.mu.Unlock()
lib.Videos[v.ID] = v lib.Videos[v.ID] = v
log.Println("Added:", path) log.Println("Added:", v.Path)
return nil return nil
} }
// Remove removes a single video from a given file path. // Remove removes a single video from a given file path.
func (lib *Library) Remove(path string) { func (lib *Library) Remove(filepath 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() lib.mu.Lock()
defer lib.mu.Unlock() defer lib.mu.Unlock()
_, ok := lib.Videos[id] d := path.Dir(filepath)
p, ok := lib.Paths[d]
if !ok {
return
}
n := path.Base(filepath)
// ID is name without extension
idx := strings.LastIndex(n, ".")
if idx == -1 {
idx = len(n)
}
id := n[:idx]
if len(p.Prefix) > 0 {
id = path.Join(p.Prefix, id)
}
v, ok := lib.Videos[id]
if ok { if ok {
delete(lib.Videos, id) delete(lib.Videos, id)
log.Println("Removed:", path) log.Println("Removed:", v.Path)
} }
} }

7
pkg/media/path.go Normal file
View file

@ -0,0 +1,7 @@
package media
// Path represents a media library path.
type Path struct {
Path string
Prefix string
}

View file

@ -2,6 +2,7 @@ package media
import ( import (
"os" "os"
"path"
"strings" "strings"
"time" "time"
@ -18,12 +19,14 @@ type Video struct {
ThumbType string ThumbType string
Modified string Modified string
Size int64 Size int64
Path string
Timestamp time.Time Timestamp time.Time
} }
// ParseVideo parses a video file's metadata and returns a Video. // ParseVideo parses a video file's metadata and returns a Video.
func ParseVideo(path string) (*Video, error) { func ParseVideo(p *Path, name string) (*Video, error) {
f, err := os.Open(path) pth := path.Join(p.Path, name)
f, err := os.Open(pth)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -34,13 +37,16 @@ func ParseVideo(path string) (*Video, error) {
size := info.Size() size := info.Size()
timestamp := info.ModTime() timestamp := info.ModTime()
modified := timestamp.Format("2006-01-02 03:04 PM") modified := timestamp.Format("2006-01-02 03:04 PM")
name := info.Name()
// ID is name without extension // ID is name without extension
idx := strings.LastIndex(name, ".") idx := strings.LastIndex(name, ".")
if idx == -1 { if idx == -1 {
idx = len(name) idx = len(name)
} }
id := name[:idx] 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) m, err := tag.ReadFrom(f)
if err != nil { if err != nil {
return nil, err return nil, err
@ -57,13 +63,14 @@ func ParseVideo(path string) (*Video, error) {
Description: m.Comment(), Description: m.Comment(),
Modified: modified, Modified: modified,
Size: size, Size: size,
Path: pth,
Timestamp: timestamp, Timestamp: timestamp,
} }
// Add thumbnail (if exists) // Add thumbnail (if exists)
p := m.Picture() pic := m.Picture()
if p != nil { if pic != nil {
v.Thumb = p.Data v.Thumb = pic.Data
v.ThumbType = p.MIMEType v.ThumbType = pic.MIMEType
} }
return v, nil return v, nil
} }