github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/api4/license_local.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  	"bytes"
     8  	"io"
     9  	"net/http"
    10  
    11  	"github.com/masterhung0112/hk_server/v5/audit"
    12  	"github.com/masterhung0112/hk_server/v5/model"
    13  )
    14  
    15  func (api *API) InitLicenseLocal() {
    16  	api.BaseRoutes.ApiRoot.Handle("/license", api.ApiLocal(localAddLicense)).Methods("POST")
    17  	api.BaseRoutes.ApiRoot.Handle("/license", api.ApiLocal(localRemoveLicense)).Methods("DELETE")
    18  }
    19  
    20  func localAddLicense(c *Context, w http.ResponseWriter, r *http.Request) {
    21  	auditRec := c.MakeAuditRecord("localAddLicense", audit.Fail)
    22  	defer c.LogAuditRec(auditRec)
    23  	c.LogAudit("attempt")
    24  
    25  	err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize)
    26  	if err != nil {
    27  		http.Error(w, err.Error(), http.StatusBadRequest)
    28  		return
    29  	}
    30  
    31  	m := r.MultipartForm
    32  
    33  	fileArray, ok := m.File["license"]
    34  	if !ok {
    35  		c.Err = model.NewAppError("addLicense", "api.license.add_license.no_file.app_error", nil, "", http.StatusBadRequest)
    36  		return
    37  	}
    38  
    39  	if len(fileArray) <= 0 {
    40  		c.Err = model.NewAppError("addLicense", "api.license.add_license.array.app_error", nil, "", http.StatusBadRequest)
    41  		return
    42  	}
    43  
    44  	fileData := fileArray[0]
    45  	auditRec.AddMeta("filename", fileData.Filename)
    46  
    47  	file, err := fileData.Open()
    48  	if err != nil {
    49  		c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, err.Error(), http.StatusBadRequest)
    50  		return
    51  	}
    52  	defer file.Close()
    53  
    54  	buf := bytes.NewBuffer(nil)
    55  	io.Copy(buf, file)
    56  
    57  	license, appErr := c.App.Srv().SaveLicense(buf.Bytes())
    58  	if appErr != nil {
    59  		if appErr.Id == model.EXPIRED_LICENSE_ERROR {
    60  			c.LogAudit("failed - expired or non-started license")
    61  		} else if appErr.Id == model.INVALID_LICENSE_ERROR {
    62  			c.LogAudit("failed - invalid license")
    63  		} else {
    64  			c.LogAudit("failed - unable to save license")
    65  		}
    66  		c.Err = appErr
    67  		return
    68  	}
    69  
    70  	auditRec.Success()
    71  	c.LogAudit("success")
    72  
    73  	w.Write([]byte(license.ToJson()))
    74  }
    75  
    76  func localRemoveLicense(c *Context, w http.ResponseWriter, r *http.Request) {
    77  	auditRec := c.MakeAuditRecord("localRemoveLicense", audit.Fail)
    78  	defer c.LogAuditRec(auditRec)
    79  	c.LogAudit("attempt")
    80  
    81  	if err := c.App.Srv().RemoveLicense(); err != nil {
    82  		c.Err = err
    83  		return
    84  	}
    85  
    86  	auditRec.Success()
    87  	c.LogAudit("success")
    88  
    89  	ReturnStatusOK(w)
    90  }