2019-06-30 01:08:00 +03:00
|
|
|
// Package app manages main application server.
|
2019-06-27 22:22:47 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2020-03-21 13:34:37 +03:00
|
|
|
"fmt"
|
2019-06-27 22:22:47 +03:00
|
|
|
"html/template"
|
2020-03-21 13:34:37 +03:00
|
|
|
"io"
|
2019-06-27 23:09:17 +03:00
|
|
|
"net"
|
2019-06-27 22:22:47 +03:00
|
|
|
"net/http"
|
2020-03-21 13:34:37 +03:00
|
|
|
"os"
|
2019-06-29 06:50:59 +03:00
|
|
|
"path"
|
2020-03-21 13:34:37 +03:00
|
|
|
"path/filepath"
|
2020-03-22 11:18:59 +03:00
|
|
|
"sort"
|
2020-03-22 11:01:27 +03:00
|
|
|
"strings"
|
2019-06-27 22:22:47 +03:00
|
|
|
|
2020-03-21 02:55:06 +03:00
|
|
|
rice "github.com/GeertJohan/go.rice"
|
2019-06-29 01:20:52 +03:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2019-06-27 22:22:47 +03:00
|
|
|
"github.com/gorilla/mux"
|
2020-03-21 15:58:14 +03:00
|
|
|
"github.com/renstrom/shortuuid"
|
2020-03-21 14:11:42 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-03-21 02:55:06 +03:00
|
|
|
"github.com/wybiral/tube/media"
|
2020-03-22 11:01:27 +03:00
|
|
|
"github.com/wybiral/tube/utils"
|
2019-06-27 22:22:47 +03:00
|
|
|
)
|
|
|
|
|
2020-03-21 02:55:06 +03:00
|
|
|
//go:generate rice embed-go
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// App represents main application.
|
2019-06-27 22:22:47 +03:00
|
|
|
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
|
2020-03-21 13:34:37 +03:00
|
|
|
Templates *templateStore
|
2019-08-08 14:04:39 +03:00
|
|
|
Feed []byte
|
2019-06-27 23:09:17 +03:00
|
|
|
Listener net.Listener
|
2019-06-27 22:22:47 +03:00
|
|
|
Router *mux.Router
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// NewApp returns a new instance of App from Config.
|
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-30 01:02:05 +03:00
|
|
|
// Setup Library
|
2019-06-29 01:20:52 +03:00
|
|
|
a.Library = media.NewLibrary()
|
2019-06-30 01:02:05 +03:00
|
|
|
// Setup Watcher
|
2019-06-29 01:20:52 +03:00
|
|
|
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-30 01:02:05 +03:00
|
|
|
// Setup Listener
|
2019-06-27 23:09:17 +03:00
|
|
|
ln, err := newListener(cfg.Server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
a.Listener = ln
|
2020-03-21 13:34:37 +03:00
|
|
|
|
|
|
|
// Templates
|
2020-03-21 02:55:06 +03:00
|
|
|
box := rice.MustFindBox("../templates")
|
2020-03-21 13:34:37 +03:00
|
|
|
|
|
|
|
a.Templates = newTemplateStore("base")
|
|
|
|
|
|
|
|
indexTemplate := template.New("index")
|
|
|
|
template.Must(indexTemplate.Parse(box.MustString("index.html")))
|
|
|
|
template.Must(indexTemplate.Parse(box.MustString("base.html")))
|
|
|
|
a.Templates.Add("index", indexTemplate)
|
|
|
|
|
|
|
|
uploadTemplate := template.New("upload")
|
|
|
|
template.Must(uploadTemplate.Parse(box.MustString("upload.html")))
|
|
|
|
template.Must(uploadTemplate.Parse(box.MustString("base.html")))
|
|
|
|
a.Templates.Add("upload", uploadTemplate)
|
2020-03-21 02:55:06 +03:00
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// Setup Router
|
2019-06-27 22:22:47 +03:00
|
|
|
r := mux.NewRouter().StrictSlash(true)
|
|
|
|
r.HandleFunc("/", a.indexHandler).Methods("GET")
|
2020-03-21 13:34:37 +03:00
|
|
|
r.HandleFunc("/upload", a.uploadHandler).Methods("GET", "POST")
|
2019-06-27 22:22:47 +03:00
|
|
|
r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET")
|
2019-07-03 21:25:42 +03:00
|
|
|
r.HandleFunc("/v/{prefix}/{id}.mp4", a.videoHandler).Methods("GET")
|
2019-06-27 22:22:47 +03:00
|
|
|
r.HandleFunc("/t/{id}", a.thumbHandler).Methods("GET")
|
2019-07-03 21:25:42 +03:00
|
|
|
r.HandleFunc("/t/{prefix}/{id}", a.thumbHandler).Methods("GET")
|
2019-06-29 04:44:56 +03:00
|
|
|
r.HandleFunc("/v/{id}", a.pageHandler).Methods("GET")
|
2019-07-03 21:25:42 +03:00
|
|
|
r.HandleFunc("/v/{prefix}/{id}", a.pageHandler).Methods("GET")
|
2019-06-29 06:50:59 +03:00
|
|
|
r.HandleFunc("/feed.xml", a.rssHandler).Methods("GET")
|
2019-06-30 01:02:05 +03:00
|
|
|
// Static file handler
|
2019-06-27 22:22:47 +03:00
|
|
|
fsHandler := http.StripPrefix(
|
2020-03-21 02:55:06 +03:00
|
|
|
"/static",
|
|
|
|
http.FileServer(rice.MustFindBox("../static").HTTPBox()),
|
2019-06-27 22:22:47 +03:00
|
|
|
)
|
|
|
|
r.PathPrefix("/static/").Handler(fsHandler).Methods("GET")
|
|
|
|
a.Router = r
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// Run imports the library and starts server.
|
2019-06-27 23:09:17 +03:00
|
|
|
func (a *App) Run() error {
|
2019-07-03 21:25:42 +03:00
|
|
|
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)
|
2019-06-29 01:20:52 +03:00
|
|
|
}
|
2019-08-08 14:04:39 +03:00
|
|
|
buildFeed(a)
|
2019-07-03 22:44:31 +03:00
|
|
|
go startWatcher(a)
|
2019-06-27 23:09:17 +03:00
|
|
|
return http.Serve(a.Listener, a.Router)
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
2020-03-21 13:34:37 +03:00
|
|
|
func (a *App) render(name string, w http.ResponseWriter, ctx interface{}) {
|
|
|
|
buf, err := a.Templates.Exec(name, ctx)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = buf.WriteTo(w)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /
|
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 {
|
2019-06-29 04:44:56 +03:00
|
|
|
http.Redirect(w, r, "/v/"+pl[0].ID, 302)
|
2019-06-29 01:51:38 +03:00
|
|
|
} else {
|
2020-03-21 13:34:37 +03:00
|
|
|
ctx := &struct {
|
2019-06-29 01:51:38 +03:00
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: &media.Video{ID: ""},
|
|
|
|
Playlist: a.Library.Playlist(),
|
2020-03-21 13:34:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
a.render("index", w, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP handler for /upload
|
|
|
|
func (a *App) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
ctx := &struct{}{}
|
|
|
|
a.render("upload", w, ctx)
|
|
|
|
} else if r.Method == "POST" {
|
|
|
|
// TODO: Move to a constant
|
|
|
|
r.ParseMultipartForm((10 << 20) * 10) // 100MB
|
|
|
|
|
|
|
|
file, handler, err := r.FormFile("video_file")
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error processing form: %w", err)
|
2020-03-21 14:11:42 +03:00
|
|
|
log.Error(err)
|
2020-03-21 13:34:37 +03:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2020-03-22 11:01:27 +03:00
|
|
|
// TODO: Make collection user selectable from drop-down in Form
|
2020-03-22 11:18:59 +03:00
|
|
|
// XXX: Assume we can put uploaded videos into the first collection (sorted) we find
|
|
|
|
keys := make([]string, 0, len(a.Library.Paths))
|
|
|
|
for k := range a.Library.Paths {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
collection := keys[0]
|
|
|
|
|
2020-03-22 11:01:27 +03:00
|
|
|
fn := filepath.Join(
|
|
|
|
a.Config.Server.UploadPath,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s%s",
|
|
|
|
shortuuid.New(),
|
|
|
|
filepath.Ext(handler.Filename),
|
|
|
|
),
|
|
|
|
)
|
2020-03-21 13:34:37 +03:00
|
|
|
|
2020-03-21 15:58:14 +03:00
|
|
|
f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE, 0644)
|
2020-03-21 13:34:37 +03:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error opening file for writing: %w", err)
|
2020-03-21 14:11:42 +03:00
|
|
|
log.Error(err)
|
2020-03-21 13:34:37 +03:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(f, file)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error writing file: %w", err)
|
2020-03-21 14:11:42 +03:00
|
|
|
log.Error(err)
|
2020-03-21 13:34:37 +03:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:01:27 +03:00
|
|
|
of := filepath.Join(
|
|
|
|
a.Library.Paths[collection].Path,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s.mp4",
|
|
|
|
strings.TrimSuffix(filepath.Base(fn), filepath.Ext(fn)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: Use a proper Job Queue and make this async
|
|
|
|
if err := utils.RunCmd(
|
|
|
|
// TODO: Make thie timeout configurable?
|
|
|
|
60,
|
|
|
|
"ffmpeg",
|
|
|
|
"-y",
|
|
|
|
"-i", fn,
|
|
|
|
"-vcodec", "h264",
|
|
|
|
"-acodec", "aac",
|
|
|
|
"-strict", "-2",
|
|
|
|
"-loglevel", "quiet",
|
2020-03-22 11:58:37 +03:00
|
|
|
of,
|
2020-03-22 11:01:27 +03:00
|
|
|
); err != nil {
|
|
|
|
err := fmt.Errorf("error transcoding video: %w", err)
|
|
|
|
log.Error(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:46:44 +03:00
|
|
|
if err := os.Remove(fn); err != nil {
|
2020-03-22 11:58:37 +03:00
|
|
|
err := fmt.Errorf("error removing uploaded file: %w", err)
|
2020-03-22 11:46:44 +03:00
|
|
|
log.Error(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2020-03-22 11:01:27 +03:00
|
|
|
|
2020-03-22 12:09:14 +03:00
|
|
|
if err := a.Library.Add(of); err != nil {
|
|
|
|
err := fmt.Errorf("error adding new video: %w", err)
|
|
|
|
log.Error(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:01:27 +03:00
|
|
|
fmt.Fprintf(w, "Video successfully uploaded!")
|
2020-03-21 13:34:37 +03:00
|
|
|
} else {
|
|
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
2019-06-29 01:51:38 +03:00
|
|
|
}
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /v/id
|
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"]
|
2019-07-03 21:25:42 +03:00
|
|
|
prefix, ok := vars["prefix"]
|
|
|
|
if ok {
|
|
|
|
id = path.Join(prefix, id)
|
|
|
|
}
|
2019-06-29 04:44:56 +03:00
|
|
|
log.Printf("/v/%s", id)
|
2019-06-27 22:22:47 +03:00
|
|
|
playing, ok := a.Library.Videos[id]
|
|
|
|
if !ok {
|
2020-03-21 13:34:37 +03:00
|
|
|
ctx := &struct {
|
2019-06-29 01:51:38 +03:00
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: &media.Video{ID: ""},
|
|
|
|
Playlist: a.Library.Playlist(),
|
2020-03-21 13:34:37 +03:00
|
|
|
}
|
|
|
|
a.render("upload", w, ctx)
|
2019-06-27 22:22:47 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2020-03-21 13:34:37 +03:00
|
|
|
ctx := &struct {
|
2019-06-27 22:22:47 +03:00
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: playing,
|
2019-06-29 01:20:52 +03:00
|
|
|
Playlist: a.Library.Playlist(),
|
2020-03-21 13:34:37 +03:00
|
|
|
}
|
|
|
|
a.render("index", w, ctx)
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /v/id.mp4
|
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"]
|
2019-07-03 21:25:42 +03:00
|
|
|
prefix, ok := vars["prefix"]
|
|
|
|
if ok {
|
|
|
|
id = path.Join(prefix, id)
|
|
|
|
}
|
2019-06-27 22:22:47 +03:00
|
|
|
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-07-03 21:25:42 +03:00
|
|
|
http.ServeFile(w, r, m.Path)
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /t/id
|
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"]
|
2019-07-03 21:25:42 +03:00
|
|
|
prefix, ok := vars["prefix"]
|
|
|
|
if ok {
|
|
|
|
id = path.Join(prefix, id)
|
|
|
|
}
|
2019-06-27 22:22:47 +03:00
|
|
|
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")
|
2020-03-21 02:55:06 +03:00
|
|
|
w.Write(rice.MustFindBox("../static").MustBytes("defaulticon.jpg"))
|
2019-06-27 22:22:47 +03:00
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", m.ThumbType)
|
|
|
|
w.Write(m.Thumb)
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 06:50:59 +03:00
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /feed.xml
|
2019-06-29 06:50:59 +03:00
|
|
|
func (a *App) rssHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=7776000")
|
|
|
|
w.Header().Set("Content-Type", "text/xml")
|
2019-08-08 14:04:39 +03:00
|
|
|
w.Write(a.Feed)
|
2019-06-29 06:50:59 +03:00
|
|
|
}
|