作者 郑周

1. 获取zip文件 ->临时保存到本地 ->解压 ->解析xml ->创建模板

2. 返回key, 前端通过访问key验证数据处理结果
... ... @@ -14,4 +14,5 @@ require (
github.com/go-redsync/redsync/v4 v4.8.1
github.com/linmadan/egglib-go v0.0.0-20210827085852-177fa745932d
github.com/xuri/excelize/v2 v2.6.1
golang.org/x/text v0.3.7
)
... ...
... ... @@ -3,6 +3,7 @@ package controllers
import (
"archive/zip"
"bufio"
"bytes"
"encoding/json"
"fmt"
service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_template"
... ... @@ -11,7 +12,10 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/xredis"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"io/ioutil"
"mime/multipart"
"os"
"path"
... ... @@ -204,17 +208,21 @@ func (controller *ImportController) ZipVerify() {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
bytes, err := xredis.GetBytes(in.VerifyKey)
if err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
if err == nil {
out := &command.OutResult{}
err := json.Unmarshal(bytes, out)
if err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
err = json.Unmarshal(bytes, out)
if err == nil {
controller.Response(out, nil)
return
}
}
// 有异常直接返回完成标记
if err != nil {
out := &command.OutResult{}
out.Status = "completed"
controller.Response(out, nil)
}
}
}
... ... @@ -247,15 +255,14 @@ func (controller *ImportController) ZipImport() {
controller.Response(map[string]string{"key": key}, nil)
go func() {
// 解压目标文件夹
dstDir := constant.UPLOAD_ZIP_PATH + "/" + day + "/" + id
dstDir := constant.UPLOAD_ZIP_PATH + "/" + day + "-" + id
fileNames, err := controller.Unzip(zipPath, dstDir)
if err != nil {
_ = xredis.Remove(key)
_ = os.RemoveAll(dstDir)
} else {
success := make([]command.ErrorI, 0)
failure := make([]command.ErrorI, 0)
for i := range fileNames {
fn := fileNames[i]
f, err := os.Open(path.Join(dstDir, fn))
... ... @@ -266,7 +273,7 @@ func (controller *ImportController) ZipImport() {
})
continue
}
if err := controller.parserAndInsert(f); err != nil {
if err := controller.parserAndInsert(f, fn); err != nil {
failure = append(failure, command.ErrorI{
FileName: fn,
Message: err.Error(),
... ... @@ -279,12 +286,18 @@ func (controller *ImportController) ZipImport() {
Message: "",
})
}
out = command.OutResult{
Status: "completed",
Success: success,
Failure: failure,
}
_ = xredis.Set(key, out, 1*time.Hour)
// 30分钟后失效
_ = xredis.Set(key, out, 30*time.Minute)
// 删除临时文件夹
_ = os.RemoveAll(dstDir)
}
}()
... ... @@ -299,7 +312,7 @@ func (controller *ImportController) writeLocal(file multipart.File) (string, str
}
id2 := fmt.Sprintf("%v", id)
day := time.Now().Format("2006-01-02")
zipPath := constant.UPLOAD_ZIP_PATH + "/" + day + "/" + id2 + "/" + "temp"
zipPath := constant.UPLOAD_ZIP_PATH + "/" + day + "-" + id2 + "/" + "temp"
var fd *os.File
... ... @@ -347,18 +360,35 @@ func (controller *ImportController) Unzip(zipPath, dstDir string) ([]string, err
}
defer reader.Close()
for _, file := range reader.File {
if err := controller.unzipFile(file, dstDir); err != nil {
var decodeName string
for _, f := range reader.File {
if f.Flags == 0 { // 如果标致位是0,则是默认的本地编码(默认为gbk),如果标志为是 1 << 11也就是 2048(则是utf-8编码)
r := bytes.NewReader([]byte(f.Name))
decoder := transform.NewReader(r, simplifiedchinese.GB18030.NewDecoder())
content, _ := ioutil.ReadAll(decoder)
decodeName = string(content)
} else {
decodeName = f.Name
}
if err := controller.unzipFile(f, decodeName, dstDir); err != nil {
return fileNames, err
} else {
fileNames = append(fileNames, file.Name)
fileNames = append(fileNames, decodeName)
}
}
return fileNames, nil
}
// 解析文件并插入数据
func (controller *ImportController) parserAndInsert(file *os.File) error {
func (controller *ImportController) parserAndInsert(file *os.File, fileName string) error {
defer func() {
err := file.Close()
if err != nil {
return
}
}()
reader, err := excelize.OpenReader(file)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error())
... ... @@ -379,27 +409,31 @@ func (controller *ImportController) parserAndInsert(file *os.File) error {
return application.ThrowError(application.ARG_ERROR, "没有数据内容")
}
var filenameWithSuffix = path.Base(fileName)
var fileSuffix = path.Ext(filenameWithSuffix)
var filenameOnly = strings.TrimSuffix(filenameWithSuffix, fileSuffix)
ruService := service.NewEvaluationTemplateService()
in := &templateCommand.CreateTemplateCommand{}
ua := middlewares.GetUser(controller.Ctx)
in.CompanyId = ua.CompanyId
in.CreatorId = ua.UserId
in.Name = file.Name()
in.Name = filenameOnly
in.Describe = ""
in.NodeContents = list
if _, err := ruService.Create(in); err != nil {
return application.ThrowError(application.ARG_ERROR, "数据创建错误")
return application.ThrowError(application.ARG_ERROR, err.Error())
}
}
return nil
}
func (controller *ImportController) unzipFile(file *zip.File, dstDir string) error {
func (controller *ImportController) unzipFile(f *zip.File, fName string, dstDir string) error {
// create the directory of file
filePath := path.Join(dstDir, file.Name)
if file.FileInfo().IsDir() {
filePath := path.Join(dstDir, fName)
if f.FileInfo().IsDir() {
if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
return err
}
... ... @@ -410,7 +444,7 @@ func (controller *ImportController) unzipFile(file *zip.File, dstDir string) err
}
// open the file
rc, err := file.Open()
rc, err := f.Open()
if err != nil {
return err
}
... ...