tube/app/app.go

743 lines
19 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"
"strings"
2019-06-27 22:22:47 +03:00
"git.mills.io/prologic/tube/app/middleware"
"git.mills.io/prologic/tube/importers"
2022-11-08 02:56:12 +03:00
"git.mills.io/prologic/tube/media"
"git.mills.io/prologic/tube/static"
"git.mills.io/prologic/tube/templates"
"git.mills.io/prologic/tube/utils"
2020-03-27 11:16:23 +03:00
"github.com/dustin/go-humanize"
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"
2021-06-08 15:49:56 +03:00
shortuuid "github.com/lithammer/shortuuid/v3"
2020-03-21 14:11:42 +03:00
log "github.com/sirupsen/logrus"
2019-06-27 22:22:47 +03:00
)
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
Store Store
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()
// Setup Store
store, err := NewBitcaskStore(cfg.Server.StorePath)
if err != nil {
err := fmt.Errorf("error opening store %s: %w", cfg.Server.StorePath, err)
return nil, err
}
a.Store = store
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
a.Templates = newTemplateStore("base")
2020-03-27 11:16:23 +03:00
templateFuncs := map[string]interface{}{
"bytes": func(size int64) string { return humanize.Bytes(uint64(size)) },
}
indexTemplate := template.New("index").Funcs(templateFuncs)
template.Must(indexTemplate.Parse(templates.MustGetTemplate("index.html")))
template.Must(indexTemplate.Parse(templates.MustGetTemplate("base.html")))
2020-03-21 13:34:37 +03:00
a.Templates.Add("index", indexTemplate)
2020-03-27 11:16:23 +03:00
uploadTemplate := template.New("upload").Funcs(templateFuncs)
template.Must(uploadTemplate.Parse(templates.MustGetTemplate("upload.html")))
template.Must(uploadTemplate.Parse(templates.MustGetTemplate("base.html")))
2020-03-21 13:34:37 +03:00
a.Templates.Add("upload", uploadTemplate)
2020-03-27 11:16:23 +03:00
importTemplate := template.New("import").Funcs(templateFuncs)
template.Must(importTemplate.Parse(templates.MustGetTemplate("import.html")))
template.Must(importTemplate.Parse(templates.MustGetTemplate("base.html")))
a.Templates.Add("import", importTemplate)
2019-06-30 01:02:05 +03:00
// Setup Router
authPassword := os.Getenv("auth_password")
isSandstorm := os.Getenv("SANDSTORM")
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")
if isSandstorm == "1" {
r.HandleFunc("/upload", middleware.RequireSandstormPermission(a.uploadHandler, "upload")).Methods("GET", "OPTIONS", "POST")
} else {
r.HandleFunc("/upload", middleware.OptionallyRequireAdminAuth(a.uploadHandler, authPassword)).Methods("GET", "OPTIONS", "POST")
}
r.HandleFunc("/import", a.importHandler).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(static.GetFilesystem()),
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
}
if err := os.MkdirAll(a.Config.Server.UploadPath, 0o755); err != nil {
return fmt.Errorf(
"error creating upload path %s: %w",
a.Config.Server.UploadPath, err,
)
}
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 {
http.Redirect(w, r, fmt.Sprintf("/v/%s?%s", pl[0].ID, r.URL.RawQuery), 302)
2019-06-29 01:51:38 +03:00
} else {
2020-03-27 14:50:19 +03:00
sort := strings.ToLower(r.URL.Query().Get("sort"))
quality := strings.ToLower(r.URL.Query().Get("quality"))
2020-03-21 13:34:37 +03:00
ctx := &struct {
2020-03-25 18:01:35 +03:00
Sort string
Quality string
Config *Config
2019-06-29 01:51:38 +03:00
Playing *media.Video
Playlist media.Playlist
}{
2020-03-27 14:50:19 +03:00
Sort: sort,
Quality: quality,
Config: a.Config,
2019-06-29 01:51:38 +03:00
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 {
Config *Config
Playing *media.Video
}{
Config: a.Config,
Playing: &media.Video{ID: ""},
}
2020-03-21 13:34:37 +03:00
a.render("upload", w, ctx)
} else if r.Method == "POST" {
r.ParseMultipartForm(a.Config.Server.MaxUploadSize)
2020-03-21 13:34:37 +03:00
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()
title := r.FormValue("video_title")
description := r.FormValue("video_description")
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
)
thumbFn1 := fmt.Sprintf("%s.jpg", strings.TrimSuffix(tf.Name(), filepath.Ext(tf.Name())))
thumbFn2 := fmt.Sprintf("%s.jpg", strings.TrimSuffix(vf, filepath.Ext(vf)))
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",
"-metadata", fmt.Sprintf("title=%s", title),
"-metadata", fmt.Sprintf("comment=%s", description),
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
}
if err := utils.RunCmd(
a.Config.Thumbnailer.Timeout,
"ffmpeg",
"-i", uf.Name(),
"-y",
"-vf", "thumbnail",
"-t", fmt.Sprint(a.Config.Thumbnailer.PositionFromStart),
"-vframes", "1",
"-strict", "-2",
"-loglevel", "quiet",
thumbFn1,
); err != nil {
err := fmt.Errorf("error generating thumbnail: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := os.Rename(thumbFn1, thumbFn2); err != nil {
err := fmt.Errorf("error renaming generated thumbnail: %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
}
// TODO: Make this a background job
// Resize for lower quality options
for size, suffix := range a.Config.Transcoder.Sizes {
log.
WithField("size", size).
WithField("vf", filepath.Base(vf)).
Info("resizing video for lower quality playback")
sf := fmt.Sprintf(
"%s#%s.mp4",
strings.TrimSuffix(vf, filepath.Ext(vf)),
suffix,
)
if err := utils.RunCmd(
a.Config.Transcoder.Timeout,
"ffmpeg",
"-y",
"-i", vf,
"-s", size,
"-c:v", "libx264",
"-c:a", "aac",
"-crf", "18",
"-strict", "-2",
"-loglevel", "quiet",
"-metadata", fmt.Sprintf("title=%s", title),
"-metadata", fmt.Sprintf("comment=%s", description),
sf,
); 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: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)
}
}
// HTTP handler for /import
func (a *App) importHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
ctx := &struct{}{}
a.render("import", w, ctx)
} else if r.Method == "POST" {
r.ParseMultipartForm(1024)
url := r.FormValue("url")
if url == "" {
err := fmt.Errorf("error, no url supplied")
log.Error(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// TODO: Make collection user selectable from drop-down in Form
// 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-28 05:16:45 +03:00
videoImporter, err := importers.NewImporter(url)
if err != nil {
err := fmt.Errorf("error creating video importer for %s: %w", url, err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
videoInfo, err := videoImporter.GetVideoInfo(url)
if err != nil {
err := fmt.Errorf("error retriving video info for %s: %w", url, err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
uf, err := ioutil.TempFile(
a.Config.Server.UploadPath,
fmt.Sprintf("tube-import-*.mp4"),
)
if err != nil {
err := fmt.Errorf("error creating temporary file for importing: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer os.Remove(uf.Name())
log.WithField("video_url", videoInfo.VideoURL).Info("requesting video size")
res, err := http.Head(videoInfo.VideoURL)
if err != nil {
err := fmt.Errorf("error getting size of video %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
contentLength := utils.SafeParseInt64(res.Header.Get("Content-Length"), -1)
if contentLength == -1 {
err := fmt.Errorf("error calculating size of video")
log.WithField("contentLength", contentLength).Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if contentLength > a.Config.Server.MaxUploadSize {
err := fmt.Errorf(
"imported video would exceed maximum upload size of %s",
humanize.Bytes(uint64(a.Config.Server.MaxUploadSize)),
)
log.
WithField("contentLength", contentLength).
WithField("max_upload_size", a.Config.Server.MaxUploadSize).
Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.WithField("contentLength", contentLength).Info("downloading video")
2020-03-28 05:16:45 +03:00
if err := utils.Download(videoInfo.VideoURL, uf.Name()); err != nil {
err := fmt.Errorf("error downloading video %s: %w", url, err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
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(
a.Library.Paths[collection].Path,
fmt.Sprintf("%s.mp4", shortuuid.New()),
)
thumbFn1 := fmt.Sprintf("%s.jpg", strings.TrimSuffix(tf.Name(), filepath.Ext(tf.Name())))
thumbFn2 := fmt.Sprintf("%s.jpg", strings.TrimSuffix(vf, filepath.Ext(vf)))
2020-03-28 05:16:45 +03:00
if err := utils.Download(videoInfo.ThumbnailURL, thumbFn1); err != nil {
err := fmt.Errorf("error downloading thumbnail: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// TODO: Use a proper Job Queue and make this async
if err := utils.RunCmd(
a.Config.Transcoder.Timeout,
"ffmpeg",
"-y",
"-i", uf.Name(),
"-vcodec", "h264",
"-acodec", "aac",
"-strict", "-2",
"-loglevel", "quiet",
2020-03-28 05:16:45 +03:00
"-metadata", fmt.Sprintf("title=%s", videoInfo.Title),
"-metadata", fmt.Sprintf("comment=%s", videoInfo.Description),
tf.Name(),
); err != nil {
err := fmt.Errorf("error transcoding video: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := os.Rename(thumbFn1, thumbFn2); err != nil {
err := fmt.Errorf("error renaming generated thumbnail: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := os.Rename(tf.Name(), vf); err != nil {
err := fmt.Errorf("error renaming transcoded video: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// TODO: Make this a background job
// Resize for lower quality options
for size, suffix := range a.Config.Transcoder.Sizes {
log.
WithField("size", size).
WithField("vf", filepath.Base(vf)).
Info("resizing video for lower quality playback")
sf := fmt.Sprintf(
"%s#%s.mp4",
strings.TrimSuffix(vf, filepath.Ext(vf)),
suffix,
)
if err := utils.RunCmd(
a.Config.Transcoder.Timeout,
"ffmpeg",
"-y",
"-i", vf,
"-s", size,
"-c:v", "libx264",
"-c:a", "aac",
"-crf", "18",
"-strict", "-2",
"-loglevel", "quiet",
"-metadata", fmt.Sprintf("title=%s", videoInfo.Title),
"-metadata", fmt.Sprintf("comment=%s", videoInfo.Description),
sf,
); err != nil {
err := fmt.Errorf("error transcoding video: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
fmt.Fprintf(w, "Video successfully imported!")
} 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 {
sort := strings.ToLower(r.URL.Query().Get("sort"))
quality := strings.ToLower(r.URL.Query().Get("quality"))
2020-03-21 13:34:37 +03:00
ctx := &struct {
Sort string
Quality string
Config *Config
2019-06-29 01:51:38 +03:00
Playing *media.Video
Playlist media.Playlist
}{
Sort: sort,
Quality: quality,
Config: a.Config,
2019-06-29 01:51:38 +03:00
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
}
views, err := a.Store.GetViews(id)
if err != nil {
err := fmt.Errorf("error retrieving views for %s: %w", id, err)
log.Warn(err)
}
playing.Views = views
playlist := a.Library.Playlist()
// TODO: Optimize this? Bitcask has no concept of MultiGet / MGET
for _, video := range playlist {
views, err := a.Store.GetViews(video.ID)
if err != nil {
err := fmt.Errorf("error retrieving views for %s: %w", video.ID, err)
log.Warn(err)
}
video.Views = views
}
sort := strings.ToLower(r.URL.Query().Get("sort"))
switch sort {
case "views":
media.By(media.SortByViews).Sort(playlist)
2020-03-26 08:06:29 +03:00
case "", "timestamp":
media.By(media.SortByTimestamp).Sort(playlist)
default:
// By default the playlist is sorted by Timestamp
log.Warnf("invalid sort critiera: %s", sort)
}
quality := strings.ToLower(r.URL.Query().Get("quality"))
switch quality {
case "", "720p", "480p", "360p", "240p":
default:
log.WithField("quality", quality).Warn("invalid quality")
quality = ""
}
2019-06-27 22:22:47 +03:00
w.Header().Set("Content-Type", "text/html; charset=utf-8")
2020-03-21 13:34:37 +03:00
ctx := &struct {
Sort string
Quality string
Config *Config
2019-06-27 22:22:47 +03:00
Playing *media.Video
Playlist media.Playlist
}{
Sort: sort,
Quality: quality,
Config: a.Config,
2019-06-27 22:22:47 +03:00
Playing: playing,
Playlist: 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)
2019-06-27 22:22:47 +03:00
m, ok := a.Library.Videos[id]
if !ok {
return
}
var videoPath string
quality := strings.ToLower(r.URL.Query().Get("quality"))
switch quality {
case "720p", "480p", "360p", "240p":
videoPath = fmt.Sprintf(
"%s#%s.mp4",
strings.TrimSuffix(m.Path, filepath.Ext(m.Path)),
quality,
)
if !utils.FileExists(videoPath) {
log.
WithField("quality", quality).
WithField("videoPath", videoPath).
Warn("video with specified quality does not exist (defaulting to default quality)")
videoPath = m.Path
}
case "":
videoPath = m.Path
default:
log.WithField("quality", quality).Warn("invalid quality")
videoPath = m.Path
}
if err := a.Store.Migrate(prefix, id); err != nil {
err := fmt.Errorf("error migrating store data: %w", err)
log.Warn(err)
}
if err := a.Store.IncViews(id); err != nil {
err := fmt.Errorf("error updating view for %s: %w", id, err)
log.Warn(err)
}
2019-06-27 22:22:47 +03:00
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, videoPath)
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(static.MustGetFile("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
}