0

update logo POST route (#773)

The logo POST route now accepts a base64 encoded image instead of a path to an
image in the data folder. The route now saves the posted image to the data
folder, with the correct file type extension, and updates logo path in the
database appropriately.
This commit is contained in:
nebunez 2021-03-21 19:07:10 -04:00 committed by GitHub
parent 80579c5e7e
commit 826aa3f158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 8 deletions

View File

@ -1,8 +1,10 @@
package admin
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"reflect"
@ -178,8 +180,8 @@ func SetStreamKey(w http.ResponseWriter, r *http.Request) {
controllers.WriteSimpleResponse(w, true, "changed")
}
// SetLogoPath will handle the web config request to validate and set the logo path.
func SetLogoPath(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
}
@ -189,14 +191,46 @@ func SetLogoPath(w http.ResponseWriter, r *http.Request) {
return
}
imgPath := configValue.Value.(string)
fullPath := filepath.Join("data", imgPath)
if !utils.DoesFileExists(fullPath) {
controllers.WriteSimpleResponse(w, false, fmt.Sprintf("%s does not exist", fullPath))
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
}
if err := data.SetLogoPath(imgPath); err != nil {
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
}

View File

@ -155,7 +155,7 @@ func Start() error {
http.HandleFunc("/api/integrations/clients", middleware.RequireAccessToken(models.ScopeHasAdminAccess, controllers.GetConnectedClients))
// Logo path
http.HandleFunc("/api/admin/config/logo", middleware.RequireAdminAuth(admin.SetLogoPath))
http.HandleFunc("/api/admin/config/logo", middleware.RequireAdminAuth(admin.SetLogo))
// Server tags
http.HandleFunc("/api/admin/config/tags", middleware.RequireAdminAuth(admin.SetTags))