Browse Source

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.
pull/846/head
nebunez 5 years ago committed by GitHub
parent
commit
826aa3f158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 48
      controllers/admin/config.go
  2. 2
      router/router.go

48
controllers/admin/config.go

@ -1,8 +1,10 @@ @@ -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) { @@ -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) { @@ -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
}
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(imgPath); err != nil {
if err := data.SetLogoPath("logo" + extension); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
return
}

2
router/router.go

@ -155,7 +155,7 @@ func Start() error { @@ -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))

Loading…
Cancel
Save