Skip to content

Commit 4650ca8

Browse files
committed
优化图片上传
1 parent 61b337d commit 4650ca8

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

controllers/DocumentController.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -765,19 +765,24 @@ func (this *DocumentController) Upload() {
765765

766766
fileName := strconv.FormatInt(time.Now().UnixNano(), 16)
767767

768+
tmpPath := fmt.Sprintf("cache/%v-%v-%v", bookIdentify, time.Now().Format("200601"), fileName+ext)
769+
err = this.SaveToFile(name, tmpPath)
770+
if err != nil {
771+
beego.Error("SaveToFile => ", err)
772+
this.JsonResult(6005, "保存文件失败")
773+
}
774+
775+
defer func() { os.Remove(tmpPath) }()
776+
768777
prefix := "uploads"
769778
savePath := filepath.Join("projects", bookIdentify, time.Now().Format("200601"), fileName+ext)
770779
if utils.StoreType != utils.StoreOss {
771780
savePath = filepath.Join(prefix, savePath)
781+
os.MkdirAll(filepath.Dir(savePath), os.ModePerm)
782+
os.Rename(tmpPath, savePath)
772783
}
773-
savePath = strings.ReplaceAll(savePath, "\\", "/")
774-
os.MkdirAll(filepath.Dir(savePath), os.ModePerm)
775784

776-
err = this.SaveToFile(name, savePath)
777-
if err != nil {
778-
beego.Error("SaveToFile => ", err)
779-
this.JsonResult(6005, "保存文件失败")
780-
}
785+
savePath = strings.ReplaceAll(savePath, "\\", "/")
781786

782787
attachment := models.NewAttachment()
783788
attachment.BookId = bookId
@@ -805,7 +810,7 @@ func (this *DocumentController) Upload() {
805810
}
806811

807812
if utils.StoreType == utils.StoreOss {
808-
if err := store.ModelStoreOss.MoveToOss(savePath, savePath, true, false); err != nil {
813+
if err := store.ModelStoreOss.MoveToOss(tmpPath, savePath, true, false); err != nil {
809814
beego.Error(err.Error())
810815
} else {
811816
if fileType == "video" || fileType == "audio" {

0 commit comments

Comments
 (0)