github.com/demisto/mattermost-server@v4.9.0-rc3+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  	"net/http"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  func (api *API) InitBrand() {
    13  	api.BaseRoutes.Brand.Handle("/image", api.ApiHandlerTrustRequester(getBrandImage)).Methods("GET")
    14  	api.BaseRoutes.Brand.Handle("/image", api.ApiSessionRequired(uploadBrandImage)).Methods("POST")
    15  }
    16  
    17  func getBrandImage(c *Context, w http.ResponseWriter, r *http.Request) {
    18  	// No permission check required
    19  
    20  	if img, err := c.App.GetBrandImage(); err != nil {
    21  		w.WriteHeader(http.StatusNotFound)
    22  		w.Write(nil)
    23  	} else {
    24  		w.Header().Set("Content-Type", "image/png")
    25  		w.Write(img)
    26  	}
    27  }
    28  
    29  func uploadBrandImage(c *Context, w http.ResponseWriter, r *http.Request) {
    30  	if r.ContentLength > *c.App.Config().FileSettings.MaxFileSize {
    31  		c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.too_large.app_error", nil, "", http.StatusRequestEntityTooLarge)
    32  		return
    33  	}
    34  
    35  	if err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize); err != nil {
    36  		c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.parse.app_error", nil, "", http.StatusBadRequest)
    37  		return
    38  	}
    39  
    40  	m := r.MultipartForm
    41  
    42  	imageArray, ok := m.File["image"]
    43  	if !ok {
    44  		c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.no_file.app_error", nil, "", http.StatusBadRequest)
    45  		return
    46  	}
    47  
    48  	if len(imageArray) <= 0 {
    49  		c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.array.app_error", nil, "", http.StatusBadRequest)
    50  		return
    51  	}
    52  
    53  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    54  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    55  		return
    56  	}
    57  
    58  	if err := c.App.SaveBrandImage(imageArray[0]); err != nil {
    59  		c.Err = err
    60  		return
    61  	}
    62  
    63  	c.LogAudit("")
    64  
    65  	w.WriteHeader(http.StatusCreated)
    66  	ReturnStatusOK(w)
    67  }