2019-06-27 22:22:47 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log"
|
2019-06-27 23:09:17 +03:00
|
|
|
"net"
|
2019-06-27 22:22:47 +03:00
|
|
|
"net/http"
|
|
|
|
|
2019-06-29 01:20:52 +03:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2019-06-27 22:22:47 +03:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/wybiral/tube/pkg/media"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
2019-06-27 23:09:17 +03:00
|
|
|
Config *Config
|
2019-06-27 22:22:47 +03:00
|
|
|
Library *media.Library
|
2019-06-29 01:20:52 +03:00
|
|
|
Watcher *fsnotify.Watcher
|
2019-06-27 22:22:47 +03:00
|
|
|
Templates *template.Template
|
2019-06-27 23:09:17 +03:00
|
|
|
Listener net.Listener
|
2019-06-27 22:22:47 +03:00
|
|
|
Router *mux.Router
|
|
|
|
}
|
|
|
|
|
2019-06-27 23:09:17 +03:00
|
|
|
func NewApp(cfg *Config) (*App, error) {
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = DefaultConfig()
|
|
|
|
}
|
|
|
|
a := &App{
|
|
|
|
Config: cfg,
|
|
|
|
}
|
2019-06-29 01:20:52 +03:00
|
|
|
a.Library = media.NewLibrary()
|
|
|
|
w, err := fsnotify.NewWatcher()
|
2019-06-27 22:22:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-29 01:20:52 +03:00
|
|
|
a.Watcher = w
|
2019-06-27 23:09:17 +03:00
|
|
|
ln, err := newListener(cfg.Server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
a.Listener = ln
|
2019-06-27 22:22:47 +03:00
|
|
|
a.Templates = template.Must(template.ParseGlob("templates/*"))
|
|
|
|
r := mux.NewRouter().StrictSlash(true)
|
|
|
|
r.HandleFunc("/", a.indexHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/t/{id}", a.thumbHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/{id}", a.pageHandler).Methods("GET")
|
|
|
|
fsHandler := http.StripPrefix(
|
|
|
|
"/static/",
|
|
|
|
http.FileServer(http.Dir("./static/")),
|
|
|
|
)
|
|
|
|
r.PathPrefix("/static/").Handler(fsHandler).Methods("GET")
|
|
|
|
a.Router = r
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 23:09:17 +03:00
|
|
|
func (a *App) Run() error {
|
2019-06-29 01:20:52 +03:00
|
|
|
path := a.Config.LibraryPath
|
|
|
|
err := a.Library.Import(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.Watcher.Add(path)
|
|
|
|
go a.watch()
|
2019-06-27 23:09:17 +03:00
|
|
|
return http.Serve(a.Listener, a.Router)
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
2019-06-29 01:20:52 +03:00
|
|
|
func (a *App) watch() {
|
|
|
|
for {
|
|
|
|
e, ok := <-a.Watcher.Events
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e.Op&fsnotify.Create > 0 {
|
|
|
|
// add new files to library
|
|
|
|
a.Library.Add(e.Name)
|
|
|
|
} else if e.Op&(fsnotify.Write|fsnotify.Chmod) > 0 {
|
|
|
|
// writes and chmods should remove old file before adding again
|
|
|
|
a.Library.Remove(e.Name)
|
|
|
|
a.Library.Add(e.Name)
|
|
|
|
} else if e.Op&(fsnotify.Remove|fsnotify.Rename) > 0 {
|
|
|
|
// remove and rename just remove file
|
|
|
|
// fsnotify will signal a Create event with the new file name
|
|
|
|
a.Library.Remove(e.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:22:47 +03:00
|
|
|
func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("/")
|
2019-06-29 01:20:52 +03:00
|
|
|
pl := a.Library.Playlist()
|
2019-06-29 01:51:38 +03:00
|
|
|
if len(pl) > 0 {
|
|
|
|
http.Redirect(w, r, "/"+pl[0].ID, 302)
|
|
|
|
} else {
|
|
|
|
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: &media.Video{ID: ""},
|
|
|
|
Playlist: a.Library.Playlist(),
|
|
|
|
})
|
|
|
|
}
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
log.Printf("/%s", id)
|
|
|
|
playing, ok := a.Library.Videos[id]
|
|
|
|
if !ok {
|
2019-06-29 01:51:38 +03:00
|
|
|
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: &media.Video{ID: ""},
|
|
|
|
Playlist: a.Library.Playlist(),
|
|
|
|
})
|
2019-06-27 22:22:47 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: playing,
|
2019-06-29 01:20:52 +03:00
|
|
|
Playlist: a.Library.Playlist(),
|
2019-06-27 22:22:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
log.Printf("/v/%s", id)
|
|
|
|
m, ok := a.Library.Videos[id]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
title := m.Title
|
|
|
|
disposition := "attachment; filename=\"" + title + ".mp4\""
|
|
|
|
w.Header().Set("Content-Disposition", disposition)
|
|
|
|
w.Header().Set("Content-Type", "video/mp4")
|
2019-06-27 23:09:17 +03:00
|
|
|
path := a.Config.LibraryPath + "/" + id + ".mp4"
|
|
|
|
http.ServeFile(w, r, path)
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
log.Printf("/t/%s", id)
|
|
|
|
m, ok := a.Library.Videos[id]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=7776000")
|
|
|
|
if m.ThumbType == "" {
|
|
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
|
|
http.ServeFile(w, r, "static/defaulticon.jpg")
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", m.ThumbType)
|
|
|
|
w.Write(m.Thumb)
|
|
|
|
}
|
|
|
|
}
|