上传书籍封面.
()
| 416 | |
| 417 | // 上传书籍封面. |
| 418 | func (this *BookController) UploadCover() { |
| 419 | |
| 420 | bookResult, err := this.IsPermission() |
| 421 | if err != nil { |
| 422 | this.JsonResult(6001, err.Error()) |
| 423 | } |
| 424 | |
| 425 | book, err := models.NewBook().Find(bookResult.BookId) |
| 426 | if err != nil { |
| 427 | logs.Error("SaveBook => ", err) |
| 428 | this.JsonResult(6002, err.Error()) |
| 429 | } |
| 430 | |
| 431 | file, moreFile, err := this.GetFile("image-file") |
| 432 | if err != nil { |
| 433 | logs.Error("", err.Error()) |
| 434 | this.JsonResult(500, "读取文件异常") |
| 435 | } |
| 436 | |
| 437 | defer file.Close() |
| 438 | |
| 439 | ext := filepath.Ext(moreFile.Filename) |
| 440 | |
| 441 | if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") { |
| 442 | this.JsonResult(500, "不支持的图片格式") |
| 443 | } |
| 444 | // |
| 445 | x1, _ := strconv.ParseFloat(this.GetString("x"), 10) |
| 446 | y1, _ := strconv.ParseFloat(this.GetString("y"), 10) |
| 447 | w1, _ := strconv.ParseFloat(this.GetString("width"), 10) |
| 448 | h1, _ := strconv.ParseFloat(this.GetString("height"), 10) |
| 449 | |
| 450 | x := int(x1) |
| 451 | y := int(y1) |
| 452 | width := int(w1) |
| 453 | height := int(h1) |
| 454 | |
| 455 | fileName := strconv.FormatInt(time.Now().UnixNano(), 16) |
| 456 | |
| 457 | filePath := filepath.Join("uploads", time.Now().Format("200601"), fileName+ext) |
| 458 | |
| 459 | path := filepath.Dir(filePath) |
| 460 | |
| 461 | os.MkdirAll(path, os.ModePerm) |
| 462 | |
| 463 | err = this.SaveToFile("image-file", filePath) |
| 464 | |
| 465 | if err != nil { |
| 466 | logs.Error("", err) |
| 467 | this.JsonResult(500, "图片保存失败") |
| 468 | } |
| 469 | if utils.StoreType != utils.StoreLocal { |
| 470 | defer func(filePath string) { |
| 471 | os.Remove(filePath) |
| 472 | }(filePath) |
| 473 | } |
| 474 | |
| 475 | //剪切图片 |
nothing calls this directly
no test coverage detected