github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/app/brand.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"bytes"
     8  	"image"
     9  	_ "image/gif"
    10  	_ "image/jpeg"
    11  	"image/png"
    12  	"mime/multipart"
    13  	"net/http"
    14  	"time"
    15  
    16  	"github.com/mattermost/mattermost-server/model"
    17  )
    18  
    19  const (
    20  	BRAND_FILE_PATH = "brand/"
    21  	BRAND_FILE_NAME = "image.png"
    22  )
    23  
    24  func (a *App) SaveBrandImage(imageData *multipart.FileHeader) *model.AppError {
    25  	if len(*a.Config().FileSettings.DriverName) == 0 {
    26  		return model.NewAppError("SaveBrandImage", "api.admin.upload_brand_image.storage.app_error", nil, "", http.StatusNotImplemented)
    27  	}
    28  
    29  	file, err := imageData.Open()
    30  	if err != nil {
    31  		return model.NewAppError("SaveBrandImage", "brand.save_brand_image.open.app_error", nil, err.Error(), http.StatusBadRequest)
    32  	}
    33  	defer file.Close()
    34  
    35  	// Decode image config first to check dimensions before loading the whole thing into memory later on
    36  	config, _, err := image.DecodeConfig(file)
    37  	if err != nil {
    38  		return model.NewAppError("SaveBrandImage", "brand.save_brand_image.decode_config.app_error", nil, err.Error(), http.StatusBadRequest)
    39  	}
    40  
    41  	if config.Width*config.Height > model.MaxImageSize {
    42  		return model.NewAppError("SaveBrandImage", "brand.save_brand_image.too_large.app_error", nil, err.Error(), http.StatusBadRequest)
    43  	}
    44  
    45  	file.Seek(0, 0)
    46  
    47  	img, _, err := image.Decode(file)
    48  	if err != nil {
    49  		return model.NewAppError("SaveBrandImage", "brand.save_brand_image.decode.app_error", nil, err.Error(), http.StatusBadRequest)
    50  	}
    51  
    52  	buf := new(bytes.Buffer)
    53  	err = png.Encode(buf, img)
    54  	if err != nil {
    55  		return model.NewAppError("SaveBrandImage", "brand.save_brand_image.encode.app_error", nil, err.Error(), http.StatusInternalServerError)
    56  	}
    57  
    58  	t := time.Now()
    59  	a.MoveFile(BRAND_FILE_PATH+BRAND_FILE_NAME, BRAND_FILE_PATH+t.Format("2006-01-02T15:04:05")+".png")
    60  
    61  	if _, err := a.WriteFile(buf, BRAND_FILE_PATH+BRAND_FILE_NAME); err != nil {
    62  		return model.NewAppError("SaveBrandImage", "brand.save_brand_image.save_image.app_error", nil, "", http.StatusInternalServerError)
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func (a *App) GetBrandImage() ([]byte, *model.AppError) {
    69  	if len(*a.Config().FileSettings.DriverName) == 0 {
    70  		return nil, model.NewAppError("GetBrandImage", "api.admin.get_brand_image.storage.app_error", nil, "", http.StatusNotImplemented)
    71  	}
    72  
    73  	img, err := a.ReadFile(BRAND_FILE_PATH + BRAND_FILE_NAME)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return img, nil
    79  }
    80  
    81  func (a *App) DeleteBrandImage() *model.AppError {
    82  	filePath := BRAND_FILE_PATH + BRAND_FILE_NAME
    83  
    84  	fileExists, err := a.FileExists(filePath)
    85  
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	if !fileExists {
    91  		return model.NewAppError("DeleteBrandImage", "api.admin.delete_brand_image.storage.not_found", nil, "", http.StatusNotFound)
    92  	}
    93  
    94  	return a.RemoveFile(filePath)
    95  }