104 lines
2.4 KiB
Go
104 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/xuri/excelize/v2"
|
|
"gitstore.ru/tolikproh/sirius/internal/model"
|
|
)
|
|
|
|
var (
|
|
TEXT_TYPES = map[string]interface{}{
|
|
"text/plain; charset=utf-8": nil,
|
|
}
|
|
)
|
|
|
|
func (s *Handler) GinConvert(c *gin.Context) {
|
|
s.log.Debug("start convert")
|
|
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, s.cfg.Srv.MaxSizeFile<<20)
|
|
|
|
file, fileHeader, err := c.Request.FormFile("file")
|
|
if err != nil {
|
|
s.log.Errorf("error code: 001. %s", err.Error())
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
buffer := make([]byte, fileHeader.Size)
|
|
file.Read(buffer)
|
|
|
|
buffer = bytes.TrimPrefix(buffer, []byte("\xef\xbb\xbf"))
|
|
|
|
fileType := http.DetectContentType(buffer)
|
|
|
|
// Validate File Type
|
|
if _, ex := TEXT_TYPES[fileType]; !ex {
|
|
s.log.Errorf("error code: 002. formated data not text/plain")
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": errors.New("formated data not text/plain").Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
if !json.Valid(buffer) {
|
|
s.log.Errorf("error code: 003. formated data not json")
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": errors.New("formated data not json").Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var sirius model.Sirius
|
|
|
|
err = json.Unmarshal(buffer, &sirius)
|
|
if err != nil {
|
|
s.log.Errorf("error code: 004. %s", err.Error())
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": errors.New("Error unmarshal file").Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
buff, err := SaveToExel(sirius.NewBolid().ZoneInfo())
|
|
if err != nil {
|
|
s.log.Errorf("error code: 005. %s", err.Error())
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": errors.New("Error create file").Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.Data(200, "application/vnd.ms-excel", buff.Bytes())
|
|
|
|
s.log.Printf("converted: ok. filename: %s; file size: %d bytes", fileHeader.Filename, fileHeader.Size)
|
|
|
|
}
|
|
|
|
func SaveToExel(data []model.ZoneInfo) (*bytes.Buffer, error) {
|
|
f := excelize.NewFile()
|
|
defer f.Close()
|
|
|
|
// Create a new sheet.
|
|
f.SetCellValue("Sheet1", "A2", "№ зоны")
|
|
f.SetCellValue("Sheet1", "B2", "Описание зоны")
|
|
f.SetCellValue("Sheet1", "C2", "Адреса в зоне")
|
|
i := 3
|
|
for _, d := range data {
|
|
f.SetCellValue("Sheet1", "A"+strconv.Itoa(i), d.ZoneNum)
|
|
f.SetCellValue("Sheet1", "B"+strconv.Itoa(i), d.ZoneName)
|
|
f.SetCellValue("Sheet1", "C"+strconv.Itoa(i), d.InputString())
|
|
i++
|
|
}
|
|
|
|
return f.WriteToBuffer()
|
|
}
|