github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+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 } 18 19 func getBrandImage(c *Context, w http.ResponseWriter, r *http.Request) { 20 // No permission check required 21 22 img, err := c.App.GetBrandImage() 23 if err != nil { 24 w.WriteHeader(http.StatusNotFound) 25 w.Write(nil) 26 return 27 } 28 29 w.Header().Set("Content-Type", "image/png") 30 w.Write(img) 31 } 32 33 func uploadBrandImage(c *Context, w http.ResponseWriter, r *http.Request) { 34 defer io.Copy(ioutil.Discard, r.Body) 35 36 if r.ContentLength > *c.App.Config().FileSettings.MaxFileSize { 37 c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.too_large.app_error", nil, "", http.StatusRequestEntityTooLarge) 38 return 39 } 40 41 if err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize); err != nil { 42 c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.parse.app_error", nil, "", http.StatusBadRequest) 43 return 44 } 45 46 m := r.MultipartForm 47 48 imageArray, ok := m.File["image"] 49 if !ok { 50 c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.no_file.app_error", nil, "", http.StatusBadRequest) 51 return 52 } 53 54 if len(imageArray) <= 0 { 55 c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.array.app_error", nil, "", http.StatusBadRequest) 56 return 57 } 58 59 if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { 60 c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) 61 return 62 } 63 64 if err := c.App.SaveBrandImage(imageArray[0]); err != nil { 65 c.Err = err 66 return 67 } 68 69 c.LogAudit("") 70 71 w.WriteHeader(http.StatusCreated) 72 ReturnStatusOK(w) 73 }