github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/api4/brand.go (about)

     1  // Copyright (c) 2015-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/vnforks/kid/v5/audit"
    12  	"github.com/vnforks/kid/v5/model"
    13  )
    14  
    15  func (api *API) InitBrand() {
    16  	api.BaseRoutes.Brand.Handle("/image", api.ApiHandlerTrustRequester(getBrandImage)).Methods("GET")
    17  	api.BaseRoutes.Brand.Handle("/image", api.ApiSessionRequired(uploadBrandImage)).Methods("POST")
    18  	api.BaseRoutes.Brand.Handle("/image", api.ApiSessionRequired(deleteBrandImage)).Methods("DELETE")
    19  }
    20  
    21  func getBrandImage(c *Context, w http.ResponseWriter, r *http.Request) {
    22  	// No permission check required
    23  
    24  	img, err := c.App.GetBrandImage()
    25  	if err != nil {
    26  		w.WriteHeader(http.StatusNotFound)
    27  		w.Write(nil)
    28  		return
    29  	}
    30  
    31  	w.Header().Set("Content-Type", "image/png")
    32  	w.Write(img)
    33  }
    34  
    35  func uploadBrandImage(c *Context, w http.ResponseWriter, r *http.Request) {
    36  	defer io.Copy(ioutil.Discard, r.Body)
    37  
    38  	if r.ContentLength > *c.App.Config().FileSettings.MaxFileSize {
    39  		c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.too_large.app_error", nil, "", http.StatusRequestEntityTooLarge)
    40  		return
    41  	}
    42  
    43  	if err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize); err != nil {
    44  		c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.parse.app_error", nil, "", http.StatusBadRequest)
    45  		return
    46  	}
    47  
    48  	m := r.MultipartForm
    49  
    50  	imageArray, ok := m.File["image"]
    51  	if !ok {
    52  		c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.no_file.app_error", nil, "", http.StatusBadRequest)
    53  		return
    54  	}
    55  
    56  	if len(imageArray) <= 0 {
    57  		c.Err = model.NewAppError("uploadBrandImage", "api.admin.upload_brand_image.array.app_error", nil, "", http.StatusBadRequest)
    58  		return
    59  	}
    60  
    61  	auditRec := c.MakeAuditRecord("uploadBrandImage", audit.Fail)
    62  	defer c.LogAuditRec(auditRec)
    63  
    64  	if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
    65  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    66  		return
    67  	}
    68  
    69  	if err := c.App.SaveBrandImage(imageArray[0]); err != nil {
    70  		c.Err = err
    71  		return
    72  	}
    73  
    74  	auditRec.Success()
    75  	c.LogAudit("")
    76  
    77  	w.WriteHeader(http.StatusCreated)
    78  	ReturnStatusOK(w)
    79  }
    80  
    81  func deleteBrandImage(c *Context, w http.ResponseWriter, r *http.Request) {
    82  	auditRec := c.MakeAuditRecord("deleteBrandImage", audit.Fail)
    83  	defer c.LogAuditRec(auditRec)
    84  
    85  	if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
    86  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    87  		return
    88  	}
    89  
    90  	if err := c.App.DeleteBrandImage(); err != nil {
    91  		c.Err = err
    92  		return
    93  	}
    94  
    95  	auditRec.Success()
    96  
    97  	ReturnStatusOK(w)
    98  }