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"
|
2020-03-25 16:02:04 +03:00
|
|
|
"strings"
|
2019-06-27 22:22:47 +03:00
|
|
|
|
2022-08-26 02:17:24 +03:00
|
|
|
"git.mills.io/prologic/tube/app/middleware"
|
2022-08-01 06:22:17 +03:00
|
|
|
"git.mills.io/prologic/tube/importers"
|
2022-11-08 02:56:12 +03:00
|
|
|
"git.mills.io/prologic/tube/media"
|
2022-08-30 04:17:08 +03:00
|
|
|
"git.mills.io/prologic/tube/static"
|
|
|
|
"git.mills.io/prologic/tube/templates"
|
2022-08-01 06:22:17 +03:00
|
|
|
"git.mills.io/prologic/tube/utils"
|
2022-08-26 02:17:24 +03:00
|
|
|
|
2023-01-16 14:33:12 +03:00
|
|
|
"github.com/cyphar/filepath-securejoin"
|
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
|
2020-03-25 05:40:00 +03:00
|
|
|
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()
|
2020-03-25 05:40:00 +03:00
|
|
|
// 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)
|
2022-08-30 04:17:08 +03:00
|
|
|
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)
|
2022-08-30 04:17:08 +03:00
|
|
|
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-21 02:55:06 +03:00
|
|
|
|
2020-03-27 11:16:23 +03:00
|
|
|
importTemplate := template.New("import").Funcs(templateFuncs)
|
2022-08-30 04:17:08 +03:00
|
|
|
template.Must(importTemplate.Parse(templates.MustGetTemplate("import.html")))
|
|
|
|
template.Must(importTemplate.Parse(templates.MustGetTemplate("base.html")))
|
2020-03-27 07:24:44 +03:00
|
|
|
a.Templates.Add("import", importTemplate)
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// Setup Router
|
2022-08-26 02:17:24 +03:00
|
|
|
authPassword := os.Getenv("auth_password")
|
2022-09-24 08:29:27 +03:00
|
|
|
isSandstorm := os.Getenv("SANDSTORM")
|
2022-08-26 02:17:24 +03:00
|
|
|
|
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")
|
2022-09-24 08:29:27 +03:00
|
|
|
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")
|
|
|
|
}
|
2020-03-27 07:24:44 +03:00
|
|
|
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")
|
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",
|
2022-08-30 04:17:08 +03:00
|
|
|
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 {
|
2019-07-03 21:25:42 +03:00
|
|
|
for _, pc := range a.Config.Library {
|
2022-11-25 04:21:03 +03:00
|
|
|
pc.Path = filepath.Clean(pc.Path)
|
2019-07-03 21:25:42 +03:00
|
|
|
p := &media.Path{
|
2023-01-16 14:33:12 +03:00
|
|
|
Path: pc.Path,
|
|
|
|
Prefix: pc.Prefix,
|
|
|
|
PreserveUploadFilename: pc.PreserveUploadFilename,
|
2019-07-03 21:25:42 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2023-01-16 14:29:55 +03:00
|
|
|
if _, err := os.Stat(a.Config.Server.UploadPath) ; err != nil && os.IsNotExist(err) {
|
|
|
|
log.Warn(
|
|
|
|
fmt.Sprintf("app: upload path '%s' does not exist. Creating it now.",
|
|
|
|
a.Config.Server.UploadPath))
|
|
|
|
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)
|
|
|
|
}
|
2020-03-29 12:02: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 {
|
2020-03-25 09:22:41 +03:00
|
|
|
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"))
|
2020-03-28 08:59:16 +03:00
|
|
|
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
|
2020-03-28 08:59:16 +03:00
|
|
|
Quality string
|
2021-06-08 16:22:14 +03:00
|
|
|
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,
|
2020-03-28 08:59:16 +03:00
|
|
|
Quality: quality,
|
2021-06-08 16:22:14 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 14:33:12 +03:00
|
|
|
func filenameWithoutExtension(path string) (stem string) {
|
|
|
|
var basename string = filepath.Base(path)
|
|
|
|
return basename[0:len(basename)-len(filepath.Ext(basename))]
|
|
|
|
}
|
|
|
|
|
2020-03-21 13:34:37 +03:00
|
|
|
// HTTP handler for /upload
|
|
|
|
func (a *App) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
2021-06-08 16:22:14 +03:00
|
|
|
ctx := &struct {
|
|
|
|
Config *Config
|
|
|
|
Playing *media.Video
|
|
|
|
}{
|
|
|
|
Config: a.Config,
|
|
|
|
Playing: &media.Video{ID: ""},
|
2020-04-08 18:53:54 +03:00
|
|
|
}
|
2020-03-21 13:34:37 +03:00
|
|
|
a.render("upload", w, ctx)
|
|
|
|
} else if r.Method == "POST" {
|
2020-03-29 10:41:10 +03:00
|
|
|
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()
|
|
|
|
|
2020-03-27 14:04:10 +03:00
|
|
|
title := r.FormValue("video_title")
|
|
|
|
description := r.FormValue("video_description")
|
2022-11-25 05:48:06 +03:00
|
|
|
if _, exists := a.Library.Paths[r.FormValue("target_library_path")]; !exists {
|
|
|
|
err := fmt.Errorf("uploading to invalid library path: %s", r.FormValue("target_library_path"))
|
|
|
|
log.Error(err)
|
|
|
|
return
|
2020-03-22 11:18:59 +03:00
|
|
|
}
|
2022-11-25 05:48:06 +03:00
|
|
|
targetLibraryPath := r.FormValue("target_library_path")
|
2020-03-22 11:18:59 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-16 14:33:12 +03:00
|
|
|
// Here we set the final filename for the video file after transcoding.
|
|
|
|
var vf string
|
|
|
|
if a.Config.Server.PreserveUploadFilename ||
|
|
|
|
a.Library.Paths[targetLibraryPath].PreserveUploadFilename {
|
|
|
|
vf, err = securejoin.SecureJoin(
|
|
|
|
a.Library.Paths[targetLibraryPath].Path,
|
|
|
|
fmt.Sprintf("%s.mp4", filenameWithoutExtension(handler.Filename)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
vf, err = securejoin.SecureJoin(
|
|
|
|
a.Library.Paths[targetLibraryPath].Path,
|
|
|
|
fmt.Sprintf("%s.mp4", shortuuid.New()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error creating file name in target library: %w", err)
|
|
|
|
log.Error(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If the (sanitized) original filename collides with an existing file,
|
|
|
|
// we try to add a shortuuid() to it until we find one that doesn't exist.
|
|
|
|
for _, err := os.Stat(vf) ; ! os.IsNotExist(err) ; _, err = os.Stat(vf) {
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warn("File '"+ vf + "' already exists.");
|
|
|
|
vf, err = securejoin.SecureJoin(
|
|
|
|
a.Library.Paths[targetLibraryPath].Path,
|
|
|
|
fmt.Sprintf("%s_%s.mp4", filenameWithoutExtension(vf), shortuuid.New()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error creating file name in target library: %w", err)
|
|
|
|
log.Error(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warn("Using filename '" + vf + "' instead.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-26 07:34:19 +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",
|
2020-03-27 14:04:10 +03:00
|
|
|
"-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
|
|
|
|
}
|
|
|
|
|
2020-03-26 07:34:19 +03:00
|
|
|
if err := utils.RunCmd(
|
|
|
|
a.Config.Thumbnailer.Timeout,
|
2022-08-01 06:22:17 +03:00
|
|
|
"ffmpeg",
|
|
|
|
"-i", uf.Name(),
|
|
|
|
"-y",
|
|
|
|
"-vf", "thumbnail",
|
2022-08-26 02:15:27 +03:00
|
|
|
"-t", fmt.Sprint(a.Config.Thumbnailer.PositionFromStart),
|
2022-08-01 06:22:17 +03:00
|
|
|
"-vframes", "1",
|
|
|
|
"-strict", "-2",
|
|
|
|
"-loglevel", "quiet",
|
|
|
|
thumbFn1,
|
2020-03-26 07:34:19 +03:00
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2020-03-28 10:18:01 +03:00
|
|
|
// TODO: Make this a background job
|
|
|
|
// Resize for lower quality options
|
2020-03-31 12:04:21 +03:00
|
|
|
for size, suffix := range a.Config.Transcoder.Sizes {
|
2020-03-28 10:18:01 +03:00
|
|
|
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)
|
2020-03-27 07:24:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP handler for /import
|
|
|
|
func (a *App) importHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
2023-01-21 03:59:27 +03:00
|
|
|
ctx := &struct {
|
|
|
|
Config *Config
|
|
|
|
Playing *media.Video
|
|
|
|
}{
|
|
|
|
Config: a.Config,
|
|
|
|
Playing: &media.Video{ID: ""},
|
|
|
|
}
|
2020-03-27 07:24:44 +03:00
|
|
|
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)
|
2020-03-27 07:54:48 +03:00
|
|
|
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)
|
2020-03-27 07:54:48 +03:00
|
|
|
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())
|
|
|
|
|
2020-03-29 10:41:10 +03:00
|
|
|
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 {
|
2020-03-27 07:54:48 +03:00
|
|
|
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 {
|
2020-03-27 07:54:48 +03:00
|
|
|
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),
|
2020-03-27 07:54:48 +03:00
|
|
|
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
|
|
|
|
}
|
2020-03-28 10:18:01 +03:00
|
|
|
|
|
|
|
// TODO: Make this a background job
|
|
|
|
// Resize for lower quality options
|
2020-03-31 12:04:21 +03:00
|
|
|
for size, suffix := range a.Config.Transcoder.Sizes {
|
2020-03-28 10:18:01 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 07:54:48 +03:00
|
|
|
fmt.Fprintf(w, "Video successfully imported!")
|
2020-03-27 07:24:44 +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-28 08:59:16 +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-28 08:59:16 +03:00
|
|
|
Sort string
|
|
|
|
Quality string
|
2021-06-08 16:22:14 +03:00
|
|
|
Config *Config
|
2019-06-29 01:51:38 +03:00
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
2020-03-28 08:59:16 +03:00
|
|
|
Sort: sort,
|
|
|
|
Quality: quality,
|
2021-06-08 16:22:14 +03:00
|
|
|
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
|
|
|
|
}
|
2020-03-25 05:40:00 +03:00
|
|
|
|
2020-03-25 09:01:29 +03:00
|
|
|
views, err := a.Store.GetViews(id)
|
2020-03-25 05:40:00 +03:00
|
|
|
if err != nil {
|
2020-03-25 09:01:29 +03:00
|
|
|
err := fmt.Errorf("error retrieving views for %s: %w", id, err)
|
2020-03-25 05:40:00 +03:00
|
|
|
log.Warn(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
playing.Views = views
|
|
|
|
|
2020-03-25 09:01:29 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-25 16:02:04 +03:00
|
|
|
sort := strings.ToLower(r.URL.Query().Get("sort"))
|
|
|
|
switch sort {
|
|
|
|
case "views":
|
2020-03-25 09:22:41 +03:00
|
|
|
media.By(media.SortByViews).Sort(playlist)
|
2020-03-26 08:06:29 +03:00
|
|
|
case "", "timestamp":
|
2020-03-25 09:22:41 +03:00
|
|
|
media.By(media.SortByTimestamp).Sort(playlist)
|
2020-03-25 16:02:04 +03:00
|
|
|
default:
|
|
|
|
// By default the playlist is sorted by Timestamp
|
|
|
|
log.Warnf("invalid sort critiera: %s", sort)
|
2020-03-25 09:22:41 +03:00
|
|
|
}
|
|
|
|
|
2020-03-28 08:59:16 +03:00
|
|
|
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 {
|
2020-03-25 16:02:04 +03:00
|
|
|
Sort string
|
2020-03-28 08:59:16 +03:00
|
|
|
Quality string
|
2021-06-08 16:22:14 +03:00
|
|
|
Config *Config
|
2019-06-27 22:22:47 +03:00
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
2020-03-25 16:02:04 +03:00
|
|
|
Sort: sort,
|
2020-03-28 08:59:16 +03:00
|
|
|
Quality: quality,
|
2021-06-08 16:22:14 +03:00
|
|
|
Config: a.Config,
|
2019-06-27 22:22:47 +03:00
|
|
|
Playing: playing,
|
2020-03-25 09:01:29 +03:00
|
|
|
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"]
|
2020-03-28 08:59:16 +03:00
|
|
|
|
2019-07-03 21:25:42 +03:00
|
|
|
prefix, ok := vars["prefix"]
|
|
|
|
if ok {
|
|
|
|
id = path.Join(prefix, id)
|
|
|
|
}
|
2020-03-28 08:59:16 +03:00
|
|
|
|
2019-06-27 22:22:47 +03:00
|
|
|
log.Printf("/v/%s", id)
|
2020-03-28 08:59:16 +03:00
|
|
|
|
2019-06-27 22:22:47 +03:00
|
|
|
m, ok := a.Library.Videos[id]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-03-25 05:40:00 +03:00
|
|
|
|
2020-03-28 08:59:16 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:01:29 +03:00
|
|
|
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)
|
2020-03-25 05:40:00 +03:00
|
|
|
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")
|
2020-03-28 08:59:16 +03:00
|
|
|
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"]
|
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")
|
2022-08-30 04:17:08 +03:00
|
|
|
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
|
|
|
}
|