1. 获取zip文件 ->临时保存到本地 ->解压 ->解析xml ->创建模板
2. 返回key, 前端通过访问key验证数据处理结果
正在显示
2 个修改的文件
包含
58 行增加
和
23 行删除
@@ -14,4 +14,5 @@ require ( | @@ -14,4 +14,5 @@ require ( | ||
14 | github.com/go-redsync/redsync/v4 v4.8.1 | 14 | github.com/go-redsync/redsync/v4 v4.8.1 |
15 | github.com/linmadan/egglib-go v0.0.0-20210827085852-177fa745932d | 15 | github.com/linmadan/egglib-go v0.0.0-20210827085852-177fa745932d |
16 | github.com/xuri/excelize/v2 v2.6.1 | 16 | github.com/xuri/excelize/v2 v2.6.1 |
17 | + golang.org/x/text v0.3.7 | ||
17 | ) | 18 | ) |
@@ -3,6 +3,7 @@ package controllers | @@ -3,6 +3,7 @@ package controllers | ||
3 | import ( | 3 | import ( |
4 | "archive/zip" | 4 | "archive/zip" |
5 | "bufio" | 5 | "bufio" |
6 | + "bytes" | ||
6 | "encoding/json" | 7 | "encoding/json" |
7 | "fmt" | 8 | "fmt" |
8 | service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_template" | 9 | service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_template" |
@@ -11,7 +12,10 @@ import ( | @@ -11,7 +12,10 @@ import ( | ||
11 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant" | 12 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant" |
12 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/xredis" | 13 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/xredis" |
13 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils" | 14 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils" |
15 | + "golang.org/x/text/encoding/simplifiedchinese" | ||
16 | + "golang.org/x/text/transform" | ||
14 | "io" | 17 | "io" |
18 | + "io/ioutil" | ||
15 | "mime/multipart" | 19 | "mime/multipart" |
16 | "os" | 20 | "os" |
17 | "path" | 21 | "path" |
@@ -204,17 +208,21 @@ func (controller *ImportController) ZipVerify() { | @@ -204,17 +208,21 @@ func (controller *ImportController) ZipVerify() { | ||
204 | controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) | 208 | controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) |
205 | } else { | 209 | } else { |
206 | bytes, err := xredis.GetBytes(in.VerifyKey) | 210 | bytes, err := xredis.GetBytes(in.VerifyKey) |
207 | - if err != nil { | ||
208 | - controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) | ||
209 | - } else { | 211 | + if err == nil { |
210 | out := &command.OutResult{} | 212 | out := &command.OutResult{} |
211 | - err := json.Unmarshal(bytes, out) | ||
212 | - if err != nil { | ||
213 | - controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) | ||
214 | - } else { | 213 | + err = json.Unmarshal(bytes, out) |
214 | + if err == nil { | ||
215 | controller.Response(out, nil) | 215 | controller.Response(out, nil) |
216 | + return | ||
216 | } | 217 | } |
217 | } | 218 | } |
219 | + | ||
220 | + // 有异常直接返回完成标记 | ||
221 | + if err != nil { | ||
222 | + out := &command.OutResult{} | ||
223 | + out.Status = "completed" | ||
224 | + controller.Response(out, nil) | ||
225 | + } | ||
218 | } | 226 | } |
219 | } | 227 | } |
220 | 228 | ||
@@ -247,15 +255,14 @@ func (controller *ImportController) ZipImport() { | @@ -247,15 +255,14 @@ func (controller *ImportController) ZipImport() { | ||
247 | controller.Response(map[string]string{"key": key}, nil) | 255 | controller.Response(map[string]string{"key": key}, nil) |
248 | go func() { | 256 | go func() { |
249 | // 解压目标文件夹 | 257 | // 解压目标文件夹 |
250 | - dstDir := constant.UPLOAD_ZIP_PATH + "/" + day + "/" + id | 258 | + dstDir := constant.UPLOAD_ZIP_PATH + "/" + day + "-" + id |
251 | fileNames, err := controller.Unzip(zipPath, dstDir) | 259 | fileNames, err := controller.Unzip(zipPath, dstDir) |
252 | if err != nil { | 260 | if err != nil { |
253 | _ = xredis.Remove(key) | 261 | _ = xredis.Remove(key) |
262 | + _ = os.RemoveAll(dstDir) | ||
254 | } else { | 263 | } else { |
255 | - | ||
256 | success := make([]command.ErrorI, 0) | 264 | success := make([]command.ErrorI, 0) |
257 | failure := make([]command.ErrorI, 0) | 265 | failure := make([]command.ErrorI, 0) |
258 | - | ||
259 | for i := range fileNames { | 266 | for i := range fileNames { |
260 | fn := fileNames[i] | 267 | fn := fileNames[i] |
261 | f, err := os.Open(path.Join(dstDir, fn)) | 268 | f, err := os.Open(path.Join(dstDir, fn)) |
@@ -266,7 +273,7 @@ func (controller *ImportController) ZipImport() { | @@ -266,7 +273,7 @@ func (controller *ImportController) ZipImport() { | ||
266 | }) | 273 | }) |
267 | continue | 274 | continue |
268 | } | 275 | } |
269 | - if err := controller.parserAndInsert(f); err != nil { | 276 | + if err := controller.parserAndInsert(f, fn); err != nil { |
270 | failure = append(failure, command.ErrorI{ | 277 | failure = append(failure, command.ErrorI{ |
271 | FileName: fn, | 278 | FileName: fn, |
272 | Message: err.Error(), | 279 | Message: err.Error(), |
@@ -279,12 +286,18 @@ func (controller *ImportController) ZipImport() { | @@ -279,12 +286,18 @@ func (controller *ImportController) ZipImport() { | ||
279 | Message: "", | 286 | Message: "", |
280 | }) | 287 | }) |
281 | } | 288 | } |
289 | + | ||
282 | out = command.OutResult{ | 290 | out = command.OutResult{ |
283 | Status: "completed", | 291 | Status: "completed", |
284 | Success: success, | 292 | Success: success, |
285 | Failure: failure, | 293 | Failure: failure, |
286 | } | 294 | } |
287 | - _ = xredis.Set(key, out, 1*time.Hour) | 295 | + |
296 | + // 30分钟后失效 | ||
297 | + _ = xredis.Set(key, out, 30*time.Minute) | ||
298 | + | ||
299 | + // 删除临时文件夹 | ||
300 | + _ = os.RemoveAll(dstDir) | ||
288 | } | 301 | } |
289 | 302 | ||
290 | }() | 303 | }() |
@@ -299,7 +312,7 @@ func (controller *ImportController) writeLocal(file multipart.File) (string, str | @@ -299,7 +312,7 @@ func (controller *ImportController) writeLocal(file multipart.File) (string, str | ||
299 | } | 312 | } |
300 | id2 := fmt.Sprintf("%v", id) | 313 | id2 := fmt.Sprintf("%v", id) |
301 | day := time.Now().Format("2006-01-02") | 314 | day := time.Now().Format("2006-01-02") |
302 | - zipPath := constant.UPLOAD_ZIP_PATH + "/" + day + "/" + id2 + "/" + "temp" | 315 | + zipPath := constant.UPLOAD_ZIP_PATH + "/" + day + "-" + id2 + "/" + "temp" |
303 | 316 | ||
304 | var fd *os.File | 317 | var fd *os.File |
305 | 318 | ||
@@ -347,18 +360,35 @@ func (controller *ImportController) Unzip(zipPath, dstDir string) ([]string, err | @@ -347,18 +360,35 @@ func (controller *ImportController) Unzip(zipPath, dstDir string) ([]string, err | ||
347 | } | 360 | } |
348 | defer reader.Close() | 361 | defer reader.Close() |
349 | 362 | ||
350 | - for _, file := range reader.File { | ||
351 | - if err := controller.unzipFile(file, dstDir); err != nil { | 363 | + var decodeName string |
364 | + for _, f := range reader.File { | ||
365 | + if f.Flags == 0 { // 如果标致位是0,则是默认的本地编码(默认为gbk),如果标志为是 1 << 11也就是 2048(则是utf-8编码) | ||
366 | + r := bytes.NewReader([]byte(f.Name)) | ||
367 | + decoder := transform.NewReader(r, simplifiedchinese.GB18030.NewDecoder()) | ||
368 | + content, _ := ioutil.ReadAll(decoder) | ||
369 | + decodeName = string(content) | ||
370 | + } else { | ||
371 | + decodeName = f.Name | ||
372 | + } | ||
373 | + | ||
374 | + if err := controller.unzipFile(f, decodeName, dstDir); err != nil { | ||
352 | return fileNames, err | 375 | return fileNames, err |
353 | } else { | 376 | } else { |
354 | - fileNames = append(fileNames, file.Name) | 377 | + fileNames = append(fileNames, decodeName) |
355 | } | 378 | } |
356 | } | 379 | } |
357 | return fileNames, nil | 380 | return fileNames, nil |
358 | } | 381 | } |
359 | 382 | ||
360 | // 解析文件并插入数据 | 383 | // 解析文件并插入数据 |
361 | -func (controller *ImportController) parserAndInsert(file *os.File) error { | 384 | +func (controller *ImportController) parserAndInsert(file *os.File, fileName string) error { |
385 | + defer func() { | ||
386 | + err := file.Close() | ||
387 | + if err != nil { | ||
388 | + return | ||
389 | + } | ||
390 | + }() | ||
391 | + | ||
362 | reader, err := excelize.OpenReader(file) | 392 | reader, err := excelize.OpenReader(file) |
363 | if err != nil { | 393 | if err != nil { |
364 | return application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()) | 394 | return application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()) |
@@ -379,27 +409,31 @@ func (controller *ImportController) parserAndInsert(file *os.File) error { | @@ -379,27 +409,31 @@ func (controller *ImportController) parserAndInsert(file *os.File) error { | ||
379 | return application.ThrowError(application.ARG_ERROR, "没有数据内容") | 409 | return application.ThrowError(application.ARG_ERROR, "没有数据内容") |
380 | } | 410 | } |
381 | 411 | ||
412 | + var filenameWithSuffix = path.Base(fileName) | ||
413 | + var fileSuffix = path.Ext(filenameWithSuffix) | ||
414 | + var filenameOnly = strings.TrimSuffix(filenameWithSuffix, fileSuffix) | ||
415 | + | ||
382 | ruService := service.NewEvaluationTemplateService() | 416 | ruService := service.NewEvaluationTemplateService() |
383 | in := &templateCommand.CreateTemplateCommand{} | 417 | in := &templateCommand.CreateTemplateCommand{} |
384 | 418 | ||
385 | ua := middlewares.GetUser(controller.Ctx) | 419 | ua := middlewares.GetUser(controller.Ctx) |
386 | in.CompanyId = ua.CompanyId | 420 | in.CompanyId = ua.CompanyId |
387 | in.CreatorId = ua.UserId | 421 | in.CreatorId = ua.UserId |
388 | - in.Name = file.Name() | 422 | + in.Name = filenameOnly |
389 | in.Describe = "" | 423 | in.Describe = "" |
390 | in.NodeContents = list | 424 | in.NodeContents = list |
391 | 425 | ||
392 | if _, err := ruService.Create(in); err != nil { | 426 | if _, err := ruService.Create(in); err != nil { |
393 | - return application.ThrowError(application.ARG_ERROR, "数据创建错误") | 427 | + return application.ThrowError(application.ARG_ERROR, err.Error()) |
394 | } | 428 | } |
395 | } | 429 | } |
396 | return nil | 430 | return nil |
397 | } | 431 | } |
398 | 432 | ||
399 | -func (controller *ImportController) unzipFile(file *zip.File, dstDir string) error { | 433 | +func (controller *ImportController) unzipFile(f *zip.File, fName string, dstDir string) error { |
400 | // create the directory of file | 434 | // create the directory of file |
401 | - filePath := path.Join(dstDir, file.Name) | ||
402 | - if file.FileInfo().IsDir() { | 435 | + filePath := path.Join(dstDir, fName) |
436 | + if f.FileInfo().IsDir() { | ||
403 | if err := os.MkdirAll(filePath, os.ModePerm); err != nil { | 437 | if err := os.MkdirAll(filePath, os.ModePerm); err != nil { |
404 | return err | 438 | return err |
405 | } | 439 | } |
@@ -410,7 +444,7 @@ func (controller *ImportController) unzipFile(file *zip.File, dstDir string) err | @@ -410,7 +444,7 @@ func (controller *ImportController) unzipFile(file *zip.File, dstDir string) err | ||
410 | } | 444 | } |
411 | 445 | ||
412 | // open the file | 446 | // open the file |
413 | - rc, err := file.Open() | 447 | + rc, err := f.Open() |
414 | if err != nil { | 448 | if err != nil { |
415 | return err | 449 | return err |
416 | } | 450 | } |
-
请 注册 或 登录 后发表评论