From da7d31fa00171c26265bfee10fb9f2a01f63a01f Mon Sep 17 00:00:00 2001 From: lealife Date: Sat, 10 Oct 2015 14:12:22 +0800 Subject: [PATCH] =?UTF-8?q?files,=20upload=E4=B8=8B=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=BF=87=E5=A4=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/leanote/leanote/issues/225 --- app/controllers/FileController.go | 76 +++++++++++++----------- app/controllers/api/ApiBaseController.go | 14 +++-- app/lea/Util.go | 16 +++++ app/service/ThemeService.go | 12 +++- 4 files changed, 78 insertions(+), 40 deletions(-) diff --git a/app/controllers/FileController.go b/app/controllers/FileController.go index 49f4d1e..7ec448e 100644 --- a/app/controllers/FileController.go +++ b/app/controllers/FileController.go @@ -69,13 +69,13 @@ func (c File) UploadAvatar() revel.Result { c.UpdateSession("Logo", re.Id); } } - + return c.RenderJson(re) } // leaui image plugin upload image func (c File) UploadImageLeaui(albumId string) revel.Result { - re := c.uploadImage("", albumId); + re := c.uploadImage("", albumId) return c.RenderJson(re) } @@ -84,74 +84,80 @@ func (c File) UploadImageLeaui(albumId string) revel.Result { func (c File) uploadImage(from, albumId string) (re info.Re) { var fileUrlPath = "" var fileId = "" - var resultCode = 0 // 1表示正常 + var resultCode = 0 // 1表示正常 var resultMsg = "error" // 错误信息 var Ok = false - + defer func() { re.Id = fileId // 只是id, 没有其它信息 re.Code = resultCode re.Msg = resultMsg re.Ok = Ok }() - + file, handel, err := c.Request.FormFile("file") if err != nil { return re } defer file.Close() + // 生成上传路径 + newGuid := NewGuid() + + userId := c.GetUserId() + if(from == "logo" || from == "blogLogo") { - fileUrlPath = "public/upload/" + c.GetUserId() + "/images/logo" + fileUrlPath = "public/upload/" + Digest3(userId) + "/" + userId + "/images/logo" } else { - fileUrlPath = "files/" + c.GetUserId() + "/images" + fileUrlPath = "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images" } - dir := revel.BasePath + "/" + fileUrlPath + + dir := revel.BasePath + "/" + fileUrlPath err = os.MkdirAll(dir, 0755) if err != nil { return re } // 生成新的文件名 filename := handel.Filename - - var ext string; + + var ext string if from == "pasteImage" { - ext = ".png"; // TODO 可能不是png类型 + ext = ".png" // TODO 可能不是png类型 } else { _, ext = SplitFilename(filename) - if(ext != ".gif" && ext != ".jpg" && ext != ".png" && ext != ".bmp" && ext != ".jpeg") { + if ext != ".gif" && ext != ".jpg" && ext != ".png" && ext != ".bmp" && ext != ".jpeg" { resultMsg = "Please upload image" return re } } - filename = NewGuid() + ext + filename = newGuid + ext data, err := ioutil.ReadAll(file) if err != nil { LogJ(err) return re } - + var maxFileSize float64 - if(from == "logo") { - maxFileSize = configService.GetUploadSize("uploadAvatarSize"); + if from == "logo" { + maxFileSize = configService.GetUploadSize("uploadAvatarSize") } else if from == "blogLogo" { - maxFileSize = configService.GetUploadSize("uploadBlogLogoSize"); + maxFileSize = configService.GetUploadSize("uploadBlogLogoSize") } else { - maxFileSize = configService.GetUploadSize("uploadImageSize"); + maxFileSize = configService.GetUploadSize("uploadImageSize") } if maxFileSize <= 0 { maxFileSize = 1000 } - + // > 2M? - if(float64(len(data)) > maxFileSize * float64(1024*1024)) { + if float64(len(data)) > maxFileSize*float64(1024*1024) { resultCode = 0 resultMsg = fmt.Sprintf("The file Size is bigger than %vM", maxFileSize) return re } - - toPath := dir + "/" + filename; + + toPath := dir + "/" + filename err = ioutil.WriteFile(toPath, data, 0777) if err != nil { LogJ(err) @@ -164,26 +170,27 @@ func (c File) uploadImage(from, albumId string) (re info.Re) { fileUrlPath += "/" + filename resultCode = 1 resultMsg = "Upload Success!" - + // File fileInfo := info.File{Name: filename, Title: handel.Filename, - Path: fileUrlPath, - Size: filesize} - - id := bson.NewObjectId(); + Path: fileUrlPath, + Size: filesize} + + id := bson.NewObjectId() fileInfo.FileId = id fileId = id.Hex() - if(from == "logo" || from == "blogLogo") { - fileId = "public/upload/" + c.GetUserId() + "/images/logo/" + filename - } + if(from == "logo" || from == "blogLogo") { + fileId = fileUrlPath + } + Ok, resultMsg = fileService.AddImage(fileInfo, albumId, c.GetUserId(), from == "" || from == "pasteImage") resultMsg = c.Message(resultMsg) - - fileInfo.Path = ""; // 不要返回 + + fileInfo.Path = "" // 不要返回 re.Item = fileInfo - + return re } @@ -204,6 +211,7 @@ func (c File) DeleteImage(fileId string) revel.Result { re.Ok, re.Msg = fileService.DeleteImage(c.GetUserId(), fileId) return c.RenderJson(re) } + //----------- // 输出image @@ -257,4 +265,4 @@ func (c File) CopyHttpImage(src string) revel.Result { re.Ok, re.Msg = fileService.AddImage(fileInfo, "", c.GetUserId(), true) return c.RenderJson(re) -} \ No newline at end of file +} diff --git a/app/controllers/api/ApiBaseController.go b/app/controllers/api/ApiBaseController.go index 4c70c06..b903cc6 100644 --- a/app/controllers/api/ApiBaseController.go +++ b/app/controllers/api/ApiBaseController.go @@ -74,7 +74,9 @@ func (c ApiBaseContrller) uploadAttach(name string, noteId string) (ok bool, msg } // 生成上传路径 - filePath := "files/" + userId + "/attachs" + newGuid := NewGuid() + filePath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/attachs" + dir := revel.BasePath + "/" + filePath err = os.MkdirAll(dir, 0755) if err != nil { @@ -83,7 +85,7 @@ func (c ApiBaseContrller) uploadAttach(name string, noteId string) (ok bool, msg // 生成新的文件名 filename := handel.Filename _, ext := SplitFilename(filename) // .doc - filename = NewGuid() + ext + filename = newGuid + ext toPath := dir + "/" + filename; err = ioutil.WriteFile(toPath, data, 0777) if err != nil { @@ -124,8 +126,12 @@ func (c ApiBaseContrller) upload(name string, noteId string, isAttach bool) (ok return } defer file.Close() + + newGuid := NewGuid() // 生成上传路径 - fileUrlPath := "files/" + c.getUserId() + "/images" + userId := c.getUserId() + fileUrlPath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images" + dir := revel.BasePath + "/" + fileUrlPath err = os.MkdirAll(dir, 0755) if err != nil { @@ -139,7 +145,7 @@ func (c ApiBaseContrller) upload(name string, noteId string, isAttach bool) (ok return } - filename = NewGuid() + ext + filename = newGuid + ext data, err := ioutil.ReadAll(file) if err != nil { return diff --git a/app/lea/Util.go b/app/lea/Util.go index 2f4aa28..503d46b 100644 --- a/app/lea/Util.go +++ b/app/lea/Util.go @@ -23,6 +23,22 @@ func Md5(s string) string { return hex.EncodeToString(h.Sum(nil)) } +// 3位数的转换, 为了用bson.id -> 3位数 +func Digest3(str string) string { + var b rune = 0 + for _, k := range str { + b += k + } + return fmt.Sprintf("%d", b % 1000) +} +func Digest2(str string) string { + var b rune = 0 + for _, k := range str { + b += k + } + return fmt.Sprintf("%d", b % 100) +} + // Guid func NewGuid() string { b := make([]byte, 48) diff --git a/app/service/ThemeService.go b/app/service/ThemeService.go index 0867299..71f7c56 100644 --- a/app/service/ThemeService.go +++ b/app/service/ThemeService.go @@ -85,13 +85,13 @@ func (this *ThemeService) getDefaultTheme(style string) info.Theme { // 用户的主题路径设置 func (this *ThemeService) getUserThemeBasePath(userId string) string { - return revel.BasePath + "/public/upload/" + userId + "/themes" + return revel.BasePath + "/public/upload/" + Digest3(userId) + "/" + userId + "/themes" } func (this *ThemeService) getUserThemePath(userId, themeId string) string { return this.getUserThemeBasePath(userId) + "/" + themeId } func (this *ThemeService) getUserThemePath2(userId, themeId string) string { - return "public/upload/" + userId + "/themes/" + themeId + return "public/upload/" + Digest3(userId) + "/" + userId + "/themes/" + themeId } // 新建主题 @@ -412,10 +412,18 @@ func (this *ThemeService) ImportTheme(userId, path string) (ok bool, msg string) themeIdO := bson.NewObjectId() themeId := themeIdO.Hex() targetPath := this.getUserThemePath(userId, themeId) // revel.BasePath + "/public/upload/" + userId + "/themes/" + themeId + + err := os.MkdirAll(targetPath, 0755) + if err != nil { + msg = "error" + return + } if ok, msg = archive.Unzip(path, targetPath); !ok { DeleteFile(targetPath) + Log("oh no") return } + // 主题验证 if ok, msg = this.ValidateTheme(targetPath, "", ""); !ok { DeleteFile(targetPath)