diff options
| author | davy wybiral <davy.wybiral@gmail.com> | 2019-07-03 13:25:42 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-03 13:25:42 -0500 |
| commit | deb6808c75b54ac23f3332ac32525b475dd267f3 (patch) | |
| tree | 7445f31f9079b3a794f6aaeed7d41db97f506938 /pkg/app | |
| parent | 4fc389edcbdb8275071050b316ff15ae3bdf8fda (diff) | |
Multipath (#10)
* added multiple paths
* prevent path duplicates
Diffstat (limited to 'pkg/app')
| -rw-r--r-- | pkg/app/app.go | 37 | ||||
| -rw-r--r-- | pkg/app/config.go | 19 |
2 files changed, 45 insertions, 11 deletions
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, |
