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
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() // 不需要关闭, 还要读取给用户下载