Browse Source

add offline stream image to client & cleaning up duplicate code

develop
forest 3 months ago
parent
commit
21e54594e2
4 changed files with 28 additions and 93 deletions
  1. +7
    -56
      controllers/admin/config.go
  2. +11
    -10
      controllers/admin/serverConfig.go
  3. +8
    -26
      controllers/logo.go
  4. +2
    -1
      webroot/js/app.js

+ 7
- 56
controllers/admin/config.go View File

@ -182,64 +182,15 @@ func SetStreamKey(w http.ResponseWriter, r *http.Request) {
// SetLogo will handle a new logo image file being uploaded.
func SetLogo(w http.ResponseWriter, r *http.Request) {
if !requirePOST(w, r) {
return
}
configValue, success := getValueFromRequest(w, r)
if !success {
return
}
s := strings.SplitN(configValue.Value.(string), ",", 2)
if len(s) < 2 {
controllers.WriteSimpleResponse(w, false, "Error splitting base64 image data.")
return
}
bytes, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
return
}
splitHeader := strings.Split(s[0], ":")
if len(splitHeader) < 2 {
controllers.WriteSimpleResponse(w, false, "Error splitting base64 image header.")
return
}
contentType := strings.Split(splitHeader[1], ";")[0]
extension := ""
if contentType == "image/svg+xml" {
extension = ".svg"
} else if contentType == "image/gif" {
extension = ".gif"
} else if contentType == "image/png" {
extension = ".png"
} else if contentType == "image/jpeg" {
extension = ".jpeg"
}
if extension == "" {
controllers.WriteSimpleResponse(w, false, "Missing or invalid contentType in base64 image.")
return
}
imgPath := filepath.Join("data", "logo"+extension)
if err := ioutil.WriteFile(imgPath, bytes, 0644); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
return
}
if err := data.SetLogoPath("logo" + extension); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
return
}
controllers.WriteSimpleResponse(w, true, "changed")
handleSetImage(w, r, "logo", data.SetLogoPath)
}
// SetLogo will handle a new logo image file being uploaded.
func SetOfflineStreamImage(w http.ResponseWriter, r *http.Request) {
handleSetImage(w, r, "offlineStreamImage", data.SetOfflineStreamImagePath)
}
func handleSetImage(w http.ResponseWriter, r *http.Request, name string, imagePathSetter func(string) error) {
if !requirePOST(w, r) {
return
}
@ -282,13 +233,13 @@ func SetOfflineStreamImage(w http.ResponseWriter, r *http.Request) {
return
}
imgPath := filepath.Join("data", "offlineStreamImage"+extension)
imgPath := filepath.Join("data", name+extension)
if err := ioutil.WriteFile(imgPath, bytes, 0644); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
return
}
if err := data.SetOfflineStreamImagePath("offlineStreamImage" + extension); err != nil {
if err := imagePathSetter(name + extension); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
return
}


+ 11
- 10
controllers/admin/serverConfig.go View File

@ -32,16 +32,17 @@ func GetServerConfig(w http.ResponseWriter, r *http.Request) {
}
response := serverConfigAdminResponse{
InstanceDetails: webConfigResponse{
Name: data.GetServerName(),
Summary: data.GetServerSummary(),
Tags: data.GetServerMetadataTags(),
ExtraPageContent: data.GetExtraPageBodyContent(),
StreamTitle: data.GetStreamTitle(),
WelcomeMessage: data.GetServerWelcomeMessage(),
Logo: data.GetLogoPath(),
SocialHandles: data.GetSocialHandles(),
NSFW: data.GetNSFW(),
CustomStyles: data.GetCustomStyles(),
Name: data.GetServerName(),
Summary: data.GetServerSummary(),
Tags: data.GetServerMetadataTags(),
ExtraPageContent: data.GetExtraPageBodyContent(),
StreamTitle: data.GetStreamTitle(),
WelcomeMessage: data.GetServerWelcomeMessage(),
Logo: data.GetLogoPath(),
OfflineStreamImage: data.GetOfflineStreamImagePath(),
SocialHandles: data.GetSocialHandles(),
NSFW: data.GetNSFW(),
CustomStyles: data.GetCustomStyles(),
},
FFmpegPath: ffmpeg,
StreamKey: data.GetStreamKey(),


+ 8
- 26
controllers/logo.go View File

@ -14,42 +14,24 @@ import (
// 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
}
handleGetImage(w, r, data.GetLogoPath(), returnDefault)
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)
}
// GetLogo will return the logo image as a response.
func GetOfflineStreamImage(w http.ResponseWriter, r *http.Request) {
imageFilename := data.GetOfflineStreamImagePath()
handleGetImage(w, r, data.GetOfflineStreamImagePath(), GetLogo)
}
func handleGetImage(w http.ResponseWriter, r *http.Request, imageFilename string, returnDefault func(http.ResponseWriter, *http.Request)) {
if imageFilename == "" {
returnDefault(w)
returnDefault(w, r)
return
}
imagePath := filepath.Join("data", imageFilename)
imageBytes, err := getImage(imagePath)
if err != nil {
returnDefault(w)
returnDefault(w, r)
return
}
@ -66,7 +48,7 @@ func GetOfflineStreamImage(w http.ResponseWriter, r *http.Request) {
writeBytesAsImage(imageBytes, contentType, w, cacheTime)
}
func returnDefault(w http.ResponseWriter) {
func returnDefault(w http.ResponseWriter, r *http.Request) {
imagePath := filepath.Join(config.WebRoot, "img", "logo.svg")
imageBytes, err := getImage(imagePath)
if err != nil {


+ 2
- 1
webroot/js/app.js View File

@ -511,6 +511,7 @@ export default class App extends Component {
const {
version: appVersion,
logo = TEMP_IMAGE,
offlineStreamImage = TEMP_IMAGE,
socialHandles = [],
summary,
tags = [],
@ -551,7 +552,7 @@ export default class App extends Component {
const poster = isPlaying
? null
: html` <${VideoPoster} offlineImage=${logo} active=${streamOnline} /> `;
: html` <${VideoPoster} offlineImage=${offlineStreamImage} active=${streamOnline} /> `;
const externalActionButtons =
externalActions &&


Loading…
Cancel
Save