owncast/controllers/logo.go
Gabe Kangas c6c6f0233d
Expanded linting + fix warnings (#1396)
* Expand the linters and types of warnings to improve consistency and safety

* Fail lint workflow if there are errors

* golint has been replaced by revive

* Hand-pick some of the default exclude list

* Ignore error when trying to delete preview gif

* Ignore linter warning opening playlist path

* Rename user field Id -> ID

* A bunch of renames to address linter warnings

* Rename ChatClient -> Client per linter suggestion best practice

* Rename ChatServer -> Server per linter suggestion best practice

* More linter warning fixes

* Add missing comments to all exported functions and properties
2021-09-12 00:18:15 -07:00

98 lines
2.8 KiB
Go

package controllers
import (
"io/ioutil"
"net/http"
"path/filepath"
"strconv"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/utils"
log "github.com/sirupsen/logrus"
)
// GetLogo will return the logo image as a response.
func GetLogo(w http.ResponseWriter, r *http.Request) {
imageFilename := data.GetLogoPath()
if imageFilename == "" {
returnDefault(w)
return
}
imagePath := filepath.Join("data", imageFilename)
imageBytes, err := getImage(imagePath)
if err != nil {
returnDefault(w)
return
}
contentType := "image/jpeg"
if filepath.Ext(imageFilename) == ".svg" {
contentType = "image/svg+xml"
} else if filepath.Ext(imageFilename) == ".gif" {
contentType = "image/gif"
} else if filepath.Ext(imageFilename) == ".png" {
contentType = "image/png"
}
cacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
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)
if err != nil {
log.Errorln(err)
return
}
cacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
writeBytesAsImage(imageBytes, "image/svg+xml", w, cacheTime)
}
func writeBytesAsImage(data []byte, contentType string, w http.ResponseWriter, cacheSeconds int) {
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Header().Set("Cache-Control", "public, max-age="+strconv.Itoa(cacheSeconds))
if _, err := w.Write(data); err != nil {
log.Println("unable to write image.")
}
}
func getImage(path string) ([]byte, error) {
return ioutil.ReadFile(path) // nolint
}