tube/app/app.go

346 lines
8.2 KiB
Go
Raw Normal View History

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"
2020-03-22 14:41:26 +03:00
"io/ioutil"
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"
2019-06-27 22:22:47 +03:00
rice "github.com/GeertJohan/go.rice"
2019-06-29 01:20:52 +03:00
"github.com/fsnotify/fsnotify"
2020-03-25 00:12:31 +03:00
"github.com/gorilla/handlers"
2019-06-27 22:22:47 +03:00
"github.com/gorilla/mux"
"github.com/renstrom/shortuuid"
2020-03-21 14:11:42 +03:00
log "github.com/sirupsen/logrus"
"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
)
//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
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)
2019-06-30 01:02:05 +03:00
// Setup Router
2019-06-27 22:22:47 +03:00
r := mux.NewRouter().StrictSlash(true)
2020-03-25 00:36:23 +03:00
r.HandleFunc("/", a.indexHandler).Methods("GET", "OPTIONS")
r.HandleFunc("/upload", a.uploadHandler).Methods("GET", "OPTIONS", "POST")
2019-06-27 22:22:47 +03:00
r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET")
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")
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")
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(
"/static",
http.FileServer(rice.MustFindBox("../static").HTTPBox()),
2019-06-27 22:22:47 +03:00
)
r.PathPrefix("/static/").Handler(fsHandler).Methods("GET")
2020-03-25 00:23:28 +03:00
cors := handlers.CORS(
2020-03-25 00:49:41 +03:00
handlers.AllowedHeaders([]string{
"X-Requested-With",
"Content-Type",
"Authorization",
}),
handlers.AllowedMethods([]string{
"GET",
"POST",
"PUT",
"HEAD",
"OPTIONS",
}),
2020-03-25 00:23:28 +03:00
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowCredentials(),
)
r.Use(cors)
2019-06-27 22:22:47 +03:00
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 {
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)
2020-03-25 00:23:28 +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 14:41:26 +03:00
uf, err := ioutil.TempFile(
2020-03-22 11:01:27 +03:00
a.Config.Server.UploadPath,
2020-03-22 14:41:26 +03:00
fmt.Sprintf("tube-upload-*%s", filepath.Ext(handler.Filename)),
2020-03-22 11:01:27 +03:00
)
2020-03-21 13:34:37 +03:00
if err != nil {
2020-03-22 14:41:26 +03:00
err := fmt.Errorf("error creating temporary file for uploading: %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 14:41:26 +03:00
defer os.Remove(uf.Name())
2020-03-21 13:34:37 +03:00
2020-03-22 14:41:26 +03:00
_, err = io.Copy(uf, file)
2020-03-21 13:34:37 +03:00
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 14:41:26 +03:00
tf, err := ioutil.TempFile(
a.Config.Server.UploadPath,
fmt.Sprintf("tube-transcode-*.mp4"),
)
if err != nil {
err := fmt.Errorf("error creating temporary file for transcoding: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
vf := filepath.Join(
2020-03-22 11:01:27 +03:00
a.Library.Paths[collection].Path,
2020-03-22 14:41:26 +03:00
fmt.Sprintf("%s.mp4", shortuuid.New()),
2020-03-22 11:01:27 +03:00
)
// TODO: Use a proper Job Queue and make this async
if err := utils.RunCmd(
2020-03-22 14:41:26 +03:00
a.Config.Transcoder.Timeout,
2020-03-22 11:01:27 +03:00
"ffmpeg",
"-y",
2020-03-22 14:41:26 +03:00
"-i", uf.Name(),
2020-03-22 11:01:27 +03:00
"-vcodec", "h264",
"-acodec", "aac",
"-strict", "-2",
"-loglevel", "quiet",
2020-03-22 14:41:26 +03:00
tf.Name(),
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 14:41:26 +03:00
if err := os.Rename(tf.Name(), vf); err != nil {
err := fmt.Errorf("error renaming transcoded video: %w", err)
2020-03-22 12:09:14 +03:00
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"]
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"]
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")
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"]
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")
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
}