2020-06-23 03:11:56 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2020-06-30 09:06:33 +02:00
|
|
|
"fmt"
|
2020-06-23 03:11:56 +02:00
|
|
|
"net/http"
|
2020-07-01 00:21:52 +02:00
|
|
|
"net/url"
|
2020-12-28 00:41:21 +01:00
|
|
|
"os"
|
2020-06-23 03:11:56 +02:00
|
|
|
"path"
|
2020-10-03 23:35:03 +02:00
|
|
|
"path/filepath"
|
2020-07-01 00:21:52 +02:00
|
|
|
"strings"
|
2020-06-30 09:06:33 +02:00
|
|
|
"text/template"
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-07-01 00:21:52 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/core"
|
2021-02-19 08:05:52 +01:00
|
|
|
"github.com/owncast/owncast/core/data"
|
|
|
|
"github.com/owncast/owncast/models"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/router/middleware"
|
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
2021-02-19 08:05:52 +01:00
|
|
|
// MetadataPage represents a server-rendered web page for bots and web scrapers.
|
2020-06-30 09:06:33 +02:00
|
|
|
type MetadataPage struct {
|
2021-02-19 08:05:52 +01:00
|
|
|
RequestedURL string
|
|
|
|
Image string
|
|
|
|
Thumbnail string
|
|
|
|
TagsString string
|
|
|
|
Summary string
|
|
|
|
Name string
|
|
|
|
Tags []string
|
|
|
|
SocialHandles []models.SocialHandle
|
2020-06-30 09:06:33 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// IndexHandler handles the default index route.
|
2020-06-23 03:11:56 +02:00
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
middleware.EnableCors(&w)
|
2020-10-09 01:34:40 +02:00
|
|
|
isIndexRequest := r.URL.Path == "/" || filepath.Base(r.URL.Path) == "index.html" || filepath.Base(r.URL.Path) == ""
|
2020-07-07 06:53:30 +02:00
|
|
|
|
2020-09-30 23:14:39 +02:00
|
|
|
// For search engine bots and social scrapers return a special
|
|
|
|
// server-rendered page.
|
2020-07-14 04:07:30 +02:00
|
|
|
if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest {
|
2020-06-30 09:06:33 +02:00
|
|
|
handleScraperMetadataPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-30 23:14:39 +02:00
|
|
|
// If the ETags match then return a StatusNotModified
|
|
|
|
if responseCode := middleware.ProcessEtags(w, r); responseCode != 0 {
|
|
|
|
w.WriteHeader(responseCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-28 00:41:21 +01:00
|
|
|
// If this is a directory listing request then return a 404
|
|
|
|
info, err := os.Stat(path.Join(config.WebRoot, r.URL.Path))
|
|
|
|
if err != nil || (info.IsDir() && !isIndexRequest) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
if path.Ext(r.URL.Path) == ".m3u8" {
|
2020-08-31 01:07:20 +02:00
|
|
|
middleware.DisableCache(w)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2020-08-31 01:07:20 +02:00
|
|
|
|
2020-09-30 23:14:39 +02:00
|
|
|
// Set a cache control max-age header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
|
|
|
|
2020-10-03 23:35:03 +02:00
|
|
|
http.ServeFile(w, r, path.Join(config.WebRoot, r.URL.Path))
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2020-06-30 09:06:33 +02:00
|
|
|
|
|
|
|
// Return a basic HTML page with server-rendered metadata from the config file
|
|
|
|
// to give to Opengraph clients and web scrapers (bots, web crawlers, etc).
|
|
|
|
func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tmpl := template.Must(template.ParseFiles(path.Join("static", "metadata.html")))
|
|
|
|
|
2020-07-01 00:21:52 +02:00
|
|
|
fullURL, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, r.URL.Path))
|
2020-10-17 00:04:31 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2021-02-21 01:47:47 +01:00
|
|
|
imageURL, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, "/logo"))
|
2020-10-17 00:04:31 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2020-07-01 00:21:52 +02:00
|
|
|
|
2020-07-20 03:36:04 +02:00
|
|
|
status := core.GetStatus()
|
|
|
|
|
|
|
|
// If the thumbnail does not exist or we're offline then just use the logo image
|
2020-07-01 00:21:52 +02:00
|
|
|
var thumbnailURL string
|
2020-10-03 23:35:03 +02:00
|
|
|
if status.Online && utils.DoesFileExists(filepath.Join(config.WebRoot, "thumbnail.jpg")) {
|
2020-07-01 00:21:52 +02:00
|
|
|
thumbnail, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, "/thumbnail.jpg"))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
2020-10-17 00:04:31 +02:00
|
|
|
thumbnailURL = imageURL.String()
|
|
|
|
} else {
|
|
|
|
thumbnailURL = thumbnail.String()
|
2020-07-01 00:21:52 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
thumbnailURL = imageURL.String()
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:05:52 +01:00
|
|
|
tagsString := strings.Join(data.GetServerMetadataTags(), ",")
|
|
|
|
metadata := MetadataPage{
|
2021-02-21 01:47:47 +01:00
|
|
|
Name: data.GetServerName(),
|
2021-02-19 08:05:52 +01:00
|
|
|
RequestedURL: fullURL.String(),
|
|
|
|
Image: imageURL.String(),
|
|
|
|
Thumbnail: thumbnailURL,
|
|
|
|
TagsString: tagsString,
|
|
|
|
Tags: data.GetServerMetadataTags(),
|
|
|
|
SocialHandles: data.GetSocialHandles(),
|
|
|
|
}
|
2020-06-30 09:06:33 +02:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2020-07-01 00:21:52 +02:00
|
|
|
err = tmpl.Execute(w, metadata)
|
2020-06-30 09:06:33 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|