github.com/mad-app/mattermost-server@v5.11.1+incompatible/api4/brand.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "io" 8 "io/ioutil" 9 "net/http" 10 11 "github.com/mattermost/mattermost-server/model" 12 ) 13 14 func (api *API) InitBrand() { 15 api.BaseRoutes.Brand.Handle("/image", api.ApiHandlerTrustRequester(getBrandImage)).Methods("GET") 16 api.BaseRoutes.Brand.Handle("/image", api.ApiSessionRequired(uploadBrandImage)).Methods("POST") 17 api.BaseRoutes.Brand.Handle("/image", api.ApiSessionRequired(deleteBrandImage)).Methods("DELETE") 18 } 19 20 func getBrandImage(c *Context, w http.ResponseWriter, r *http.Request) { 21 // No permission check required 22 23 img, err := c.App.GetBrandImage() 24 if err != nil { 25 w.WriteHeader(http.StatusNotFound) 26 w.Write(nil) 27 return 28 } 29 30 w.Header().Set("Content-Type", "image/png") 31 w.Write(img) 32 } 33 34 func uploadBrandImage(c *Context, w http.ResponseWriter, r *http.Request) { 35 defer io.Copy(ioutil.Discard, r.Body) 36 37 if r.ContentLength > *c.App.Config().FileSettings.MaxFileSize { 38 c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.too_large.app_error", nil, "", http.StatusRequestEntityTooLarge) 39 return 40 } 41 42 if err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize); err != nil { 43 c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.parse.app_error", nil, "", http.StatusBadRequest) 44 return 45 } 46 47 m := r.MultipartForm 48 49 imageArray, ok := m.File["image"] 50 if !ok { 51 c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.no_file.app_error", nil, "", http.StatusBadRequest) 52 return 53 } 54 55 if len(imageArray) <= 0 { 56 c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.array.app_error", nil, "", http.StatusBadRequest) 57 return 58 } 59 60 if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) { 61 c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) 62 return 63 } 64 65 if err := c.App.SaveBrandImage(imageArray[0]); err != nil { 66 c.Err = err 67 return 68 } 69 70 c.LogAudit("") 71 72 w.WriteHeader(http.StatusCreated) 73 ReturnStatusOK(w) 74 } 75 76 func deleteBrandImage(c *Context, w http.ResponseWriter, r *http.Request) { 77 if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) { 78 c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) 79 return 80 } 81 82 if err := c.App.DeleteBrandImage(); err != nil { 83 c.Err = err 84 return 85 } 86 87 ReturnStatusOK(w) 88 }