Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

2.8 KB
Raw Blame
package handlers

import (
	"net/http"
	"os"
	"path/filepath"
	"strconv"

	log "github.com/sirupsen/logrus"

	"github.com/owncast/owncast/config"
	"github.com/owncast/owncast/static"
	"github.com/owncast/owncast/utils"
)

// GetLogo will return the logo image as a response.
func (h *Handlers) GetLogo(w http.ResponseWriter, r *http.Request) {
	imageFilename := h.configRepository.GetLogoPath()
	if imageFilename == "" {
		returnDefault(w)
		return
	}
	imagePath := filepath.Join(config.DataDirectory, 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" //nolint:goconst
	}

	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 (h *Handlers) GetCompatibleLogo(w http.ResponseWriter, r *http.Request) {
	imageFilename := h.configRepository.GetLogoPath()

	// If the logo image is not a SVG then we can return it
	// without any problems.
	if imageFilename != "" && filepath.Ext(imageFilename) != ".svg" {
		h.GetLogo(w, r)
		return
	}

	// Otherwise use a fallback logo.png.
	imagePath := filepath.Join(config.DataDirectory, "logo.png")
	contentType := "image/png"
	imageBytes, err := getImage(imagePath)
	if err != nil {
		returnDefault(w)
		return
	}

	cacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
	writeBytesAsImage(imageBytes, contentType, w, cacheTime)

	h.hasWarnedSVGLogoLock.Lock()
	defer h.hasWarnedSVGLogoLock.Unlock()
	if !h.hasWarnedSVGLogo {
		log.Warnf("an external site requested your logo. because many social networks do not support SVGs we returned a placeholder instead. change your current logo to a png or jpeg to be most compatible with external social networking sites.")
		h.hasWarnedSVGLogo = true
	}
}

func returnDefault(w http.ResponseWriter) {
	imageBytes := static.GetLogo()
	cacheTime := utils.GetCacheDurationSecondsForPath("logo.png")
	writeBytesAsImage(imageBytes, "image/png", 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 os.ReadFile(path) // nolint
}