From 15238bf9dedb50d2029c92aed978c9ecab0f504c Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Fri, 27 Aug 2021 16:20:16 -0700 Subject: [PATCH] Add an endpoint for returning a most-compatible logo (non-svg) used in sharing and indexing. Closes #1286 --- controllers/index.go | 2 +- controllers/logo.go | 33 +++++++++++++++++++++++++++++++++ router/router.go | 3 +++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/controllers/index.go b/controllers/index.go index a8df377b6..b793c516b 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -91,7 +91,7 @@ func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) { if err != nil { log.Panicln(err) } - imageURL, err := url.Parse(fmt.Sprintf("%s://%s%s", scheme, r.Host, "/logo")) + imageURL, err := url.Parse(fmt.Sprintf("%s://%s%s", scheme, r.Host, "/logo/external")) if err != nil { log.Panicln(err) } diff --git a/controllers/logo.go b/controllers/logo.go index 4b3821a74..6db2cc465 100644 --- a/controllers/logo.go +++ b/controllers/logo.go @@ -39,6 +39,39 @@ func GetLogo(w http.ResponseWriter, r *http.Request) { writeBytesAsImage(imageBytes, contentType, w, cacheTime) } +// GetCompatibleLogo will return the logo unless it's a SVG +// and in that case will return a default placeholder. +// Used for sharing to external social networks that generally +// don't support SVG. +func GetCompatibleLogo(w http.ResponseWriter, r *http.Request) { + imageFilename := data.GetLogoPath() + + // If the logo image is not a SVG then we can return it + // without any problems. + if imageFilename != "" && filepath.Ext(imageFilename) != ".svg" { + GetLogo(w, r) + return + } + + // Otherwise use a fallback logo.png. + imagePath := filepath.Join(config.WebRoot, "img", "logo.png") + contentType := "image/png" + imageBytes, err := getImage(imagePath) + if err != nil { + returnDefault(w) + return + } + + cacheTime := utils.GetCacheDurationSecondsForPath(imagePath) + writeBytesAsImage(imageBytes, contentType, w, cacheTime) + + referrer := r.Referer() + if referrer == "" { + referrer = "an external site" + } + log.Warnf("%s requested your logo. because many social networks do not support SVGs we returned a placeholder instead. change your current logo \"%s\" to a png or jpeg to be most compatible with external social networking sites.", referrer, imageFilename) +} + func returnDefault(w http.ResponseWriter) { imagePath := filepath.Join(config.WebRoot, "img", "logo.svg") imageBytes, err := getImage(imagePath) diff --git a/router/router.go b/router/router.go index ec9b07220..bc05fa48a 100644 --- a/router/router.go +++ b/router/router.go @@ -57,6 +57,9 @@ func Start() error { // return the logo http.HandleFunc("/logo", controllers.GetLogo) + // return a logo that's compatible with external social networks + http.HandleFunc("/logo/external", controllers.GetCompatibleLogo) + // return the list of video variants available http.HandleFunc("/api/video/variants", controllers.GetVideoStreamOutputVariants)