From deb6808c75b54ac23f3332ac32525b475dd267f3 Mon Sep 17 00:00:00 2001 From: davy wybiral Date: Wed, 3 Jul 2019 13:25:42 -0500 Subject: Multipath (#10) * added multiple paths * prevent path duplicates --- pkg/app/app.go | 37 ++++++++++++++++++++++++++++++------- pkg/app/config.go | 19 +++++++++++++++---- 2 files changed, 45 insertions(+), 11 deletions(-) (limited to 'pkg/app') diff --git a/pkg/app/app.go b/pkg/app/app.go index d5a909a..baf3ccb 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -56,8 +56,11 @@ func NewApp(cfg *Config) (*App, error) { r := mux.NewRouter().StrictSlash(true) r.HandleFunc("/", a.indexHandler).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/{prefix}/{id}", a.thumbHandler).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") // Static file handler fsHandler := http.StripPrefix( @@ -71,12 +74,21 @@ func NewApp(cfg *Config) (*App, error) { // Run imports the library and starts server. func (a *App) Run() error { - path := a.Config.LibraryPath - err := a.Library.Import(path) - if err != nil { - return err + for _, pc := range a.Config.Library { + p := &media.Path{ + Path: pc.Path, + 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() 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) { vars := mux.Vars(r) id := vars["id"] + prefix, ok := vars["prefix"] + if ok { + id = path.Join(prefix, id) + } log.Printf("/v/%s", id) playing, ok := a.Library.Videos[id] 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) { vars := mux.Vars(r) id := vars["id"] + prefix, ok := vars["prefix"] + if ok { + id = path.Join(prefix, id) + } log.Printf("/v/%s", id) m, ok := a.Library.Videos[id] if !ok { @@ -159,14 +179,17 @@ func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) { disposition := "attachment; filename=\"" + title + ".mp4\"" w.Header().Set("Content-Disposition", disposition) w.Header().Set("Content-Type", "video/mp4") - path := a.Config.LibraryPath + "/" + id + ".mp4" - http.ServeFile(w, r, path) + http.ServeFile(w, r, m.Path) } // HTTP handler for /t/id func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] + prefix, ok := vars["prefix"] + if ok { + id = path.Join(prefix, id) + } log.Printf("/t/%s", id) m, ok := a.Library.Videos[id] if !ok { diff --git a/pkg/app/config.go b/pkg/app/config.go index f8ca2e8..c2ecb45 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -7,9 +7,15 @@ import ( // Config settings for main App. type Config struct { - LibraryPath string `json:"library"` - Server *ServerConfig `json:"server"` - Feed *FeedConfig `json:"feed"` + Library []*PathConfig `json:"library"` + Server *ServerConfig `json:"server"` + 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. @@ -34,7 +40,12 @@ type FeedConfig struct { // DefaultConfig returns Config initialized with default values. func DefaultConfig() *Config { return &Config{ - LibraryPath: "videos", + Library: []*PathConfig{ + &PathConfig{ + Path: "videos", + Prefix: "", + }, + }, Server: &ServerConfig{ Host: "127.0.0.1", Port: 0, -- cgit v1.2.3