diff --git a/controllers/index.go b/controllers/index.go index 9a2e2dc8c..e42c2f4a2 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -2,31 +2,36 @@ package controllers import ( "fmt" - "log" "net/http" + "net/url" "path" + "strings" "text/template" + "github.com/mssola/user_agent" + log "github.com/sirupsen/logrus" + "github.com/gabek/owncast/config" "github.com/gabek/owncast/core" "github.com/gabek/owncast/router/middleware" "github.com/gabek/owncast/utils" - - "xojoc.pw/useragent" ) type MetadataPage struct { Config config.InstanceDetails RequestedURL string + Image string + Thumbnail string + TagsString string } //IndexHandler handles the default index route func IndexHandler(w http.ResponseWriter, r *http.Request) { middleware.EnableCors(&w) - ua := useragent.Parse(r.UserAgent()) + ua := user_agent.New(r.UserAgent()) - if ua.Type == useragent.Crawler && r.URL.Path == "/" { + if ua != nil && ua.Bot() && (r.URL.Path == "/" || r.URL.Path == "/index.html") { handleScraperMetadataPage(w, r) return } @@ -46,11 +51,27 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) { tmpl := template.Must(template.ParseFiles(path.Join("static", "metadata.html"))) - fullURL := fmt.Sprintf("http://%s%s", r.Host, r.URL.Path) - metadata := MetadataPage{config.Config.InstanceDetails, fullURL} + fullURL, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, r.URL.Path)) + + imageURL, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, config.Config.InstanceDetails.Logo["large"])) + + // If the thumbnail does not exist then just use the logo image + var thumbnailURL string + if utils.DoesFileExists("webroot/thumbnail.jpg") { + thumbnail, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, "/thumbnail.jpg")) + if err != nil { + log.Errorln(err) + } + thumbnailURL = thumbnail.String() + } else { + thumbnailURL = imageURL.String() + } + + tagsString := strings.Join(config.Config.InstanceDetails.Tags, ",") + metadata := MetadataPage{config.Config.InstanceDetails, fullURL.String(), imageURL.String(), thumbnailURL, tagsString} w.Header().Set("Content-Type", "text/html") - err := tmpl.Execute(w, metadata) + err = tmpl.Execute(w, metadata) if err != nil { log.Panicln(err) diff --git a/go.mod b/go.mod index ab2cfd95a..06f241d5c 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/ipfs/interface-go-ipfs-core v0.2.7 github.com/libp2p/go-libp2p-peer v0.2.0 github.com/libp2p/go-libp2p-peerstore v0.2.6 + github.com/mssola/user_agent v0.5.2 github.com/multiformats/go-multiaddr v0.2.2 github.com/radovskyb/watcher v1.0.7 github.com/sirupsen/logrus v1.6.0 @@ -18,5 +19,4 @@ require ( github.com/yutopp/go-rtmp v0.0.0-20191212152852-4e41609a99bb golang.org/x/net v0.0.0-20200602114024-627f9648deb9 gopkg.in/yaml.v2 v2.3.0 - xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe ) diff --git a/go.sum b/go.sum index b91feb203..995d1a9e3 100644 --- a/go.sum +++ b/go.sum @@ -749,6 +749,8 @@ github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc= github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/mssola/user_agent v0.5.2 h1:CZkTUahjL1+OcZ5zv3kZr8QiJ8jy2H08vZIEkBeRbxo= +github.com/mssola/user_agent v0.5.2/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI= github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= github.com/multiformats/go-multiaddr v0.0.1/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= @@ -1192,5 +1194,3 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck= sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0= -xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe h1:KHyqPlOEFFT7OPh4WR7qFzNNndwj1VuwV+rZ+Tb3bio= -xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe/go.mod h1:71om/Qz9HbIEjbUrkrzmJiF26FSh6tcwqSFdBBkLtJQ= diff --git a/static/metadata.html b/static/metadata.html index 3cf101b01..d98e43ad9 100644 --- a/static/metadata.html +++ b/static/metadata.html @@ -4,25 +4,30 @@
-