From bc0e09f222d8bac7a70fadc3123c0fd54dc3698d Mon Sep 17 00:00:00 2001 From: lealife Date: Sat, 28 Nov 2015 14:44:07 +0800 Subject: [PATCH] =?UTF-8?q?file=20upload=20path=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=8C=96=20GetRandomFilePath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/AttachController.go | 16 ++++++++++++---- app/controllers/FileController.go | 6 ++++-- app/controllers/api/ApiBaseController.go | 6 ++++-- app/service/FileService.go | 12 +++++++----- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/app/controllers/AttachController.go b/app/controllers/AttachController.go index 3852fad..e82d142 100644 --- a/app/controllers/AttachController.go +++ b/app/controllers/AttachController.go @@ -67,7 +67,9 @@ func (c Attach) uploadAttach(noteId string) (re info.Re) { } // 生成上传路径 - filePath := "files/" + c.GetUserId() + "/attachs" + // filePath := "files/" + c.GetUserId() + "/attachs" + newGuid := NewGuid() + filePath := "files/" + GetRandomFilePath(c.GetUserId(), newGuid) + "/attachs" dir := revel.BasePath + "/" + filePath err = os.MkdirAll(dir, 0755) if err != nil { @@ -76,7 +78,7 @@ func (c Attach) uploadAttach(noteId string) (re info.Re) { // 生成新的文件名 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 { @@ -165,10 +167,16 @@ func (c Attach) DownloadAll(noteId string) revel.Result { filename = "all.tar.gz" } + dir := revel.BasePath + "/files/attach_all" + + if !MkdirAll(dir) { + return c.RenderText("error") + } + // file write - fw, err := os.Create(revel.BasePath + "/files/" + filename) + fw, err := os.Create(dir + "/" + filename) if err != nil { - return c.RenderText("") + return c.RenderText("error") } // defer fw.Close() // 不需要关闭, 还要读取给用户下载 diff --git a/app/controllers/FileController.go b/app/controllers/FileController.go index 0f54e8f..8a1fda8 100644 --- a/app/controllers/FileController.go +++ b/app/controllers/FileController.go @@ -109,7 +109,8 @@ func (c File) uploadImage(from, albumId string) (re info.Re) { if from == "logo" || from == "blogLogo" { fileUrlPath = "public/upload/" + Digest3(userId) + "/" + userId + "/images/logo" } else { - fileUrlPath = "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images" + // fileUrlPath = "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images" + fileUrlPath = "files/" + GetRandomFilePath(userId, newGuid) + "/images" } dir := revel.BasePath + "/" + fileUrlPath @@ -242,7 +243,8 @@ func (c File) CopyHttpImage(src string) revel.Result { // 生成上传路径 newGuid := NewGuid() userId := c.GetUserId() - fileUrlPath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images" + // fileUrlPath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images" + fileUrlPath := "files/" + GetRandomFilePath(userId, newGuid) + "/images" dir := revel.BasePath + "/" + fileUrlPath err := os.MkdirAll(dir, 0755) if err != nil { diff --git a/app/controllers/api/ApiBaseController.go b/app/controllers/api/ApiBaseController.go index 1b975aa..e0b8e34 100644 --- a/app/controllers/api/ApiBaseController.go +++ b/app/controllers/api/ApiBaseController.go @@ -75,7 +75,8 @@ func (c ApiBaseContrller) uploadAttach(name string, noteId string) (ok bool, msg // 生成上传路径 newGuid := NewGuid() - filePath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/attachs" + // filePath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/attachs" + filePath := "files/" + GetRandomFilePath(userId, newGuid) + "/attachs" dir := revel.BasePath + "/" + filePath err = os.MkdirAll(dir, 0755) @@ -130,7 +131,8 @@ func (c ApiBaseContrller) upload(name string, noteId string, isAttach bool) (ok newGuid := NewGuid() // 生成上传路径 userId := c.getUserId() - fileUrlPath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images" + // fileUrlPath := "files/" + Digest3(userId) + "/" + userId + "/" + Digest2(newGuid) + "/images" + fileUrlPath := "files/" + GetRandomFilePath(userId, newGuid) + "/images" dir := revel.BasePath + "/" + fileUrlPath err = os.MkdirAll(dir, 0755) diff --git a/app/service/FileService.go b/app/service/FileService.go index 9e3569a..2710ce8 100644 --- a/app/service/FileService.go +++ b/app/service/FileService.go @@ -242,6 +242,7 @@ func (this *FileService) GetFile(userId, fileId string) string { return "" } +// 复制共享的笔记时, 复制其中的图片到我本地 // 复制图片 func (this *FileService) CopyImage(userId, fileId, toUserId string) (bool, string) { // 是否已经复制过了 @@ -252,7 +253,6 @@ func (this *FileService) CopyImage(userId, fileId, toUserId string) (bool, strin } // 复制之 - file := info.File{} db.GetByIdAndUserId(db.Files, fileId, userId, &file) @@ -261,18 +261,20 @@ func (this *FileService) CopyImage(userId, fileId, toUserId string) (bool, strin } _, ext := SplitFilename(file.Name) - newFilename := NewGuid() + ext + guid := NewGuid() + newFilename := guid + ext - dir := "files/" + toUserId + "/images" + // TODO 统一目录格式 + // dir := "files/" + toUserId + "/images" + dir := "files/" + GetRandomFilePath(toUserId, guid) + "/images" filePath := dir + "/" + newFilename - err := os.MkdirAll(dir, 0755) + err := os.MkdirAll(revel.BasePath+dir, 0755) if err != nil { return false, "" } _, err = CopyFile(revel.BasePath+"/"+file.Path, revel.BasePath+"/"+filePath) if err != nil { - Log(err) return false, "" }