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
2 changed files with 42 additions and 8 deletions

View File

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

View File

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