files, upload下文件过多

https://github.com/leanote/leanote/issues/225
This commit is contained in:
lealife
2015-10-10 14:12:22 +08:00
parent 62bc74d3c6
commit da7d31fa00
4 changed files with 78 additions and 40 deletions

View File

@ -75,7 +75,7 @@ func (c File) UploadAvatar() revel.Result {
// leaui image plugin upload image // leaui image plugin upload image
func (c File) UploadImageLeaui(albumId string) revel.Result { func (c File) UploadImageLeaui(albumId string) revel.Result {
re := c.uploadImage("", albumId); re := c.uploadImage("", albumId)
return c.RenderJson(re) return c.RenderJson(re)
} }
@ -84,7 +84,7 @@ func (c File) UploadImageLeaui(albumId string) revel.Result {
func (c File) uploadImage(from, albumId string) (re info.Re) { func (c File) uploadImage(from, albumId string) (re info.Re) {
var fileUrlPath = "" var fileUrlPath = ""
var fileId = "" var fileId = ""
var resultCode = 0 // 1表示正常 var resultCode = 0 // 1表示正常
var resultMsg = "error" // 错误信息 var resultMsg = "error" // 错误信息
var Ok = false var Ok = false
@ -100,13 +100,19 @@ func (c File) uploadImage(from, albumId string) (re info.Re) {
return re return re
} }
defer file.Close() defer file.Close()
// 生成上传路径 // 生成上传路径
newGuid := NewGuid()
userId := c.GetUserId()
if(from == "logo" || from == "blogLogo") { if(from == "logo" || from == "blogLogo") {
fileUrlPath = "public/upload/" + c.GetUserId() + "/images/logo" fileUrlPath = "public/upload/" + Digest3(userId) + "/" + userId + "/images/logo"
} else { } 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) err = os.MkdirAll(dir, 0755)
if err != nil { if err != nil {
return re return re
@ -114,18 +120,18 @@ func (c File) uploadImage(from, albumId string) (re info.Re) {
// 生成新的文件名 // 生成新的文件名
filename := handel.Filename filename := handel.Filename
var ext string; var ext string
if from == "pasteImage" { if from == "pasteImage" {
ext = ".png"; // TODO 可能不是png类型 ext = ".png" // TODO 可能不是png类型
} else { } else {
_, ext = SplitFilename(filename) _, 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" resultMsg = "Please upload image"
return re return re
} }
} }
filename = NewGuid() + ext filename = newGuid + ext
data, err := ioutil.ReadAll(file) data, err := ioutil.ReadAll(file)
if err != nil { if err != nil {
LogJ(err) LogJ(err)
@ -133,25 +139,25 @@ func (c File) uploadImage(from, albumId string) (re info.Re) {
} }
var maxFileSize float64 var maxFileSize float64
if(from == "logo") { if from == "logo" {
maxFileSize = configService.GetUploadSize("uploadAvatarSize"); maxFileSize = configService.GetUploadSize("uploadAvatarSize")
} else if from == "blogLogo" { } else if from == "blogLogo" {
maxFileSize = configService.GetUploadSize("uploadBlogLogoSize"); maxFileSize = configService.GetUploadSize("uploadBlogLogoSize")
} else { } else {
maxFileSize = configService.GetUploadSize("uploadImageSize"); maxFileSize = configService.GetUploadSize("uploadImageSize")
} }
if maxFileSize <= 0 { if maxFileSize <= 0 {
maxFileSize = 1000 maxFileSize = 1000
} }
// > 2M? // > 2M?
if(float64(len(data)) > maxFileSize * float64(1024*1024)) { if float64(len(data)) > maxFileSize*float64(1024*1024) {
resultCode = 0 resultCode = 0
resultMsg = fmt.Sprintf("The file Size is bigger than %vM", maxFileSize) resultMsg = fmt.Sprintf("The file Size is bigger than %vM", maxFileSize)
return re return re
} }
toPath := dir + "/" + filename; toPath := dir + "/" + filename
err = ioutil.WriteFile(toPath, data, 0777) err = ioutil.WriteFile(toPath, data, 0777)
if err != nil { if err != nil {
LogJ(err) LogJ(err)
@ -168,20 +174,21 @@ func (c File) uploadImage(from, albumId string) (re info.Re) {
// File // File
fileInfo := info.File{Name: filename, fileInfo := info.File{Name: filename,
Title: handel.Filename, Title: handel.Filename,
Path: fileUrlPath, Path: fileUrlPath,
Size: filesize} Size: filesize}
id := bson.NewObjectId(); id := bson.NewObjectId()
fileInfo.FileId = id fileInfo.FileId = id
fileId = id.Hex() fileId = id.Hex()
if(from == "logo" || from == "blogLogo") { if(from == "logo" || from == "blogLogo") {
fileId = "public/upload/" + c.GetUserId() + "/images/logo/" + filename fileId = fileUrlPath
} }
Ok, resultMsg = fileService.AddImage(fileInfo, albumId, c.GetUserId(), from == "" || from == "pasteImage") Ok, resultMsg = fileService.AddImage(fileInfo, albumId, c.GetUserId(), from == "" || from == "pasteImage")
resultMsg = c.Message(resultMsg) resultMsg = c.Message(resultMsg)
fileInfo.Path = ""; // 不要返回 fileInfo.Path = "" // 不要返回
re.Item = fileInfo re.Item = fileInfo
return re return re
@ -204,6 +211,7 @@ func (c File) DeleteImage(fileId string) revel.Result {
re.Ok, re.Msg = fileService.DeleteImage(c.GetUserId(), fileId) re.Ok, re.Msg = fileService.DeleteImage(c.GetUserId(), fileId)
return c.RenderJson(re) return c.RenderJson(re)
} }
//----------- //-----------
// 输出image // 输出image

View File

@ -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 dir := revel.BasePath + "/" + filePath
err = os.MkdirAll(dir, 0755) err = os.MkdirAll(dir, 0755)
if err != nil { if err != nil {
@ -83,7 +85,7 @@ func (c ApiBaseContrller) uploadAttach(name string, noteId string) (ok bool, msg
// 生成新的文件名 // 生成新的文件名
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 {
@ -124,8 +126,12 @@ func (c ApiBaseContrller) upload(name string, noteId string, isAttach bool) (ok
return return
} }
defer file.Close() 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 dir := revel.BasePath + "/" + fileUrlPath
err = os.MkdirAll(dir, 0755) err = os.MkdirAll(dir, 0755)
if err != nil { if err != nil {
@ -139,7 +145,7 @@ func (c ApiBaseContrller) upload(name string, noteId string, isAttach bool) (ok
return return
} }
filename = NewGuid() + ext filename = newGuid + ext
data, err := ioutil.ReadAll(file) data, err := ioutil.ReadAll(file)
if err != nil { if err != nil {
return return

View File

@ -23,6 +23,22 @@ func Md5(s string) string {
return hex.EncodeToString(h.Sum(nil)) 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 // Guid
func NewGuid() string { func NewGuid() string {
b := make([]byte, 48) b := make([]byte, 48)

View File

@ -85,13 +85,13 @@ func (this *ThemeService) getDefaultTheme(style string) info.Theme {
// 用户的主题路径设置 // 用户的主题路径设置
func (this *ThemeService) getUserThemeBasePath(userId string) string { 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 { func (this *ThemeService) getUserThemePath(userId, themeId string) string {
return this.getUserThemeBasePath(userId) + "/" + themeId return this.getUserThemeBasePath(userId) + "/" + themeId
} }
func (this *ThemeService) getUserThemePath2(userId, themeId string) string { 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() themeIdO := bson.NewObjectId()
themeId := themeIdO.Hex() themeId := themeIdO.Hex()
targetPath := this.getUserThemePath(userId, themeId) // revel.BasePath + "/public/upload/" + userId + "/themes/" + themeId targetPath := this.getUserThemePath(userId, themeId) // revel.BasePath + "/public/upload/" + userId + "/themes/" + themeId
if ok, msg = archive.Unzip(path, targetPath); !ok {
DeleteFile(targetPath) err := os.MkdirAll(targetPath, 0755)
if err != nil {
msg = "error"
return return
} }
if ok, msg = archive.Unzip(path, targetPath); !ok {
DeleteFile(targetPath)
Log("oh no")
return
}
// 主题验证 // 主题验证
if ok, msg = this.ValidateTheme(targetPath, "", ""); !ok { if ok, msg = this.ValidateTheme(targetPath, "", ""); !ok {
DeleteFile(targetPath) DeleteFile(targetPath)