file upload path 统一化 GetRandomFilePath

This commit is contained in:
lealife
2015-11-28 14:44:07 +08:00
parent 0dab71e72b
commit bc0e09f222
4 changed files with 27 additions and 13 deletions

View File

@ -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 dir := revel.BasePath + "/" + filePath
err = os.MkdirAll(dir, 0755) err = os.MkdirAll(dir, 0755)
if err != nil { if err != nil {
@ -76,7 +78,7 @@ func (c Attach) uploadAttach(noteId string) (re info.Re) {
// 生成新的文件名 // 生成新的文件名
filename := handel.Filename filename := handel.Filename
_, ext := SplitFilename(filename) // .doc _, ext := SplitFilename(filename) // .doc
filename = NewGuid() + ext filename = newGuid + ext
toPath := dir + "/" + filename toPath := dir + "/" + filename
err = ioutil.WriteFile(toPath, data, 0777) err = ioutil.WriteFile(toPath, data, 0777)
if err != nil { if err != nil {
@ -165,10 +167,16 @@ func (c Attach) DownloadAll(noteId string) revel.Result {
filename = "all.tar.gz" filename = "all.tar.gz"
} }
dir := revel.BasePath + "/files/attach_all"
if !MkdirAll(dir) {
return c.RenderText("error")
}
// file write // file write
fw, err := os.Create(revel.BasePath + "/files/" + filename) fw, err := os.Create(dir + "/" + filename)
if err != nil { if err != nil {
return c.RenderText("") return c.RenderText("error")
} }
// defer fw.Close() // 不需要关闭, 还要读取给用户下载 // defer fw.Close() // 不需要关闭, 还要读取给用户下载

View File

@ -109,7 +109,8 @@ func (c File) uploadImage(from, albumId string) (re info.Re) {
if from == "logo" || from == "blogLogo" { if from == "logo" || from == "blogLogo" {
fileUrlPath = "public/upload/" + Digest3(userId) + "/" + userId + "/images/logo" fileUrlPath = "public/upload/" + Digest3(userId) + "/" + userId + "/images/logo"
} else { } 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 dir := revel.BasePath + "/" + fileUrlPath
@ -242,7 +243,8 @@ func (c File) CopyHttpImage(src string) revel.Result {
// 生成上传路径 // 生成上传路径
newGuid := NewGuid() newGuid := NewGuid()
userId := c.GetUserId() 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 dir := revel.BasePath + "/" + fileUrlPath
err := os.MkdirAll(dir, 0755) err := os.MkdirAll(dir, 0755)
if err != nil { if err != nil {

View File

@ -75,7 +75,8 @@ func (c ApiBaseContrller) uploadAttach(name string, noteId string) (ok bool, msg
// 生成上传路径 // 生成上传路径
newGuid := NewGuid() 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 dir := revel.BasePath + "/" + filePath
err = os.MkdirAll(dir, 0755) err = os.MkdirAll(dir, 0755)
@ -130,7 +131,8 @@ func (c ApiBaseContrller) upload(name string, noteId string, isAttach bool) (ok
newGuid := NewGuid() newGuid := NewGuid()
// 生成上传路径 // 生成上传路径
userId := c.getUserId() 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 dir := revel.BasePath + "/" + fileUrlPath
err = os.MkdirAll(dir, 0755) err = os.MkdirAll(dir, 0755)

View File

@ -242,6 +242,7 @@ func (this *FileService) GetFile(userId, fileId string) string {
return "" return ""
} }
// 复制共享的笔记时, 复制其中的图片到我本地
// 复制图片 // 复制图片
func (this *FileService) CopyImage(userId, fileId, toUserId string) (bool, string) { 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{} file := info.File{}
db.GetByIdAndUserId(db.Files, fileId, userId, &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) _, 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 filePath := dir + "/" + newFilename
err := os.MkdirAll(dir, 0755) err := os.MkdirAll(revel.BasePath+dir, 0755)
if err != nil { if err != nil {
return false, "" return false, ""
} }
_, err = CopyFile(revel.BasePath+"/"+file.Path, revel.BasePath+"/"+filePath) _, err = CopyFile(revel.BasePath+"/"+file.Path, revel.BasePath+"/"+filePath)
if err != nil { if err != nil {
Log(err)
return false, "" return false, ""
} }