diff --git a/controllers/admin/config.go b/controllers/admin/config.go index 9b7f81226..51b9bc892 100644 --- a/controllers/admin/config.go +++ b/controllers/admin/config.go @@ -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 } diff --git a/router/router.go b/router/router.go index dc42bf723..3dbcb8b1a 100644 --- a/router/router.go +++ b/router/router.go @@ -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))