From 346abfe91d49b7bd12e547edd2084da935f979b3 Mon Sep 17 00:00:00 2001 From: life Date: Sun, 9 Nov 2014 18:00:23 +0800 Subject: [PATCH] upload file size limit [ok] --- app/controllers/AttachController.go | 9 ++- app/controllers/FileController.go | 23 +++++-- app/controllers/NoteController.go | 2 + .../admin/AdminSettingController.go | 10 +++ .../member/MemberUserController.go | 1 + app/init.go | 4 ++ app/service/ConfigService.go | 24 +++++++ app/views/admin/nav.html | 8 +++ app/views/admin/setting/upload.html | 65 +++++++++++++++++++ app/views/member/user/avatar.html | 3 +- app/views/note/note-dev.html | 1 + public/js/app/attachment_upload.js | 10 +-- .../plugins/leaui_image/public/css/style.css | 2 +- .../plugins/leaui_image/public/js/main.js | 19 +++++- 14 files changed, 165 insertions(+), 16 deletions(-) create mode 100644 app/views/admin/setting/upload.html diff --git a/app/controllers/AttachController.go b/app/controllers/AttachController.go index cb91632..90246d7 100644 --- a/app/controllers/AttachController.go +++ b/app/controllers/AttachController.go @@ -11,6 +11,7 @@ import ( "strings" "time" "io" + "fmt" "archive/tar" "compress/gzip" ) @@ -56,8 +57,12 @@ func (c Attach) uploadAttach(noteId string) (re info.Re) { return re } // > 5M? - if(len(data) > 5 * 1024 * 1024) { - resultMsg = "File is bigger than 5M" + maxFileSize := configService.GetUploadSize("uploadAttachSize"); + if maxFileSize <= 0 { + maxFileSize = 1000 + } + if(float64(len(data)) > maxFileSize * float64(1024*1024)) { + resultMsg = fmt.Sprintf("附件大于%vM", maxFileSize) return re } diff --git a/app/controllers/FileController.go b/app/controllers/FileController.go index e8609bd..b95e0d3 100644 --- a/app/controllers/FileController.go +++ b/app/controllers/FileController.go @@ -9,6 +9,7 @@ import ( "github.com/leanote/leanote/app/info" "io/ioutil" "os" + "fmt" "strconv" "strings" ) @@ -21,7 +22,7 @@ type File struct { // 上传的是博客logo // TODO logo不要设置权限, 另外的目录 func (c File) UploadBlogLogo() revel.Result { - re := c.uploadImage("logo", ""); + re := c.uploadImage("blogLogo", ""); c.RenderArgs["fileUrlPath"] = re.Id c.RenderArgs["resultCode"] = re.Code @@ -100,7 +101,7 @@ func (c File) uploadImage(from, albumId string) (re info.Re) { } defer file.Close() // 生成上传路径 - if(from == "logo") { + if(from == "logo" || from == "blogLogo") { fileUrlPath = "public/upload/" + c.GetUserId() + "/images/logo" } else { fileUrlPath = "files/" + c.GetUserId() + "/images" @@ -131,10 +132,22 @@ func (c File) uploadImage(from, albumId string) (re info.Re) { return re } + var maxFileSize float64 + if(from == "logo") { + maxFileSize = configService.GetUploadSize("uploadAvatarSize"); + } else if from == "blogLogo" { + maxFileSize = configService.GetUploadSize("uploadBlogLogoSize"); + } else { + maxFileSize = configService.GetUploadSize("uploadImageSize"); + } + if maxFileSize <= 0 { + maxFileSize = 1000 + } + // > 2M? - if(len(data) > 5 * 1024 * 1024) { + if(float64(len(data)) > maxFileSize * float64(1024*1024)) { resultCode = 0 - resultMsg = "图片大于2M" + resultMsg = fmt.Sprintf("图片大于%vM", maxFileSize) return re } @@ -161,7 +174,7 @@ func (c File) uploadImage(from, albumId string) (re info.Re) { id := bson.NewObjectId(); fileInfo.FileId = id fileId = id.Hex() - if(from == "logo") { + if(from == "logo" || from == "blogLogo") { fileId = "public/upload/" + c.GetUserId() + "/images/logo/" + filename } diff --git a/app/controllers/NoteController.go b/app/controllers/NoteController.go index 6811c3f..33e6dad 100644 --- a/app/controllers/NoteController.go +++ b/app/controllers/NoteController.go @@ -66,6 +66,8 @@ func (c Note) Index() revel.Result { c.RenderArgs["tagsJson"] = c.Json(tagService.GetTags(c.GetUserId())) + c.RenderArgs["globalConfigs"] = configService.GetGlobalConfigForUser() + if isDev, _ := revel.Config.Bool("mode.dev"); isDev { return c.RenderTemplate("note/note-dev.html") } else { diff --git a/app/controllers/admin/AdminSettingController.go b/app/controllers/admin/AdminSettingController.go index e46c58d..f36e892 100644 --- a/app/controllers/admin/AdminSettingController.go +++ b/app/controllers/admin/AdminSettingController.go @@ -5,6 +5,7 @@ import ( // . "github.com/leanote/leanote/app/lea" "github.com/leanote/leanote/app/info" "strings" + "fmt" ) // admin 首页 @@ -115,5 +116,14 @@ func (c AdminSetting) Mongodb(mongodumpPath, mongorestorePath string) revel.Resu re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "mongodumpPath", mongodumpPath) re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "mongorestorePath", mongorestorePath) + return c.RenderJson(re) +} + +func (c AdminSetting) UploadSize(uploadImageSize, uploadAvatarSize, uploadBlogLogoSize, uploadAttachSize float64) revel.Result { + re := info.NewRe() + re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "uploadImageSize", fmt.Sprintf("%v", uploadImageSize)) + re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "uploadAvatarSize", fmt.Sprintf("%v", uploadAvatarSize)) + re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "uploadBlogLogoSize", fmt.Sprintf("%v", uploadBlogLogoSize)) + re.Ok = configService.UpdateGlobalStringConfig(c.GetUserId(), "uploadAttachSize", fmt.Sprintf("%v", uploadAttachSize)) return c.RenderJson(re) } \ No newline at end of file diff --git a/app/controllers/member/MemberUserController.go b/app/controllers/member/MemberUserController.go index a413470..4fdb2eb 100644 --- a/app/controllers/member/MemberUserController.go +++ b/app/controllers/member/MemberUserController.go @@ -35,6 +35,7 @@ func (c MemberUser) Avatar() revel.Result { c.SetUserInfo() c.SetLocale() c.RenderArgs["title"] = "Avatar" + c.RenderArgs["globalConfigs"] = configService.GetGlobalConfigForUser() return c.RenderTemplate("member/user/avatar.html"); } func (c MemberUser) AddAccount() revel.Result { diff --git a/app/init.go b/app/init.go index 94b6245..f491511 100644 --- a/app/init.go +++ b/app/init.go @@ -82,6 +82,10 @@ func init() { b, _ := json.Marshal(i) return string(b) } + revel.TemplateFuncs["jsonJs"] = func(i interface{}) template.JS { + b, _ := json.Marshal(i) + return template.JS(string(b)) + } revel.TemplateFuncs["datetime"] = func(t time.Time) template.HTML { return template.HTML(t.Format("2006-01-02 15:04:05")) } diff --git a/app/service/ConfigService.go b/app/service/ConfigService.go index 28a10d4..fd79093 100644 --- a/app/service/ConfigService.go +++ b/app/service/ConfigService.go @@ -539,3 +539,27 @@ func (this *ConfigService) IsGoodSubDomain(domain string) bool { } return true } + +// 上传大小 +func (this *ConfigService) GetUploadSize(key string) float64 { + f, _ := strconv.ParseFloat(this.GetGlobalStringConfig(key), 64) + return f; +} +func (this *ConfigService) GetUploadSizeLimit() map[string]float64 { + return map[string]float64{ + "uploadImageSize": this.GetUploadSize("uploadImageSize"), + "uploadBlogLogoSize":this.GetUploadSize("uploadBlogLogoSize"), + "uploadAttachSize":this.GetUploadSize("uploadAttachSize"), + "uploadAvatarSize":this.GetUploadSize("uploadAvatarSize"), + } +} +// 为用户得到全局的配置 +// NoteController调用 +func (this *ConfigService) GetGlobalConfigForUser() map[string]interface{} { + uploadSizeConfigs := this.GetUploadSizeLimit(); + config := map[string]interface{}{} + for k, v := range uploadSizeConfigs { + config[k] = v + } + return config +} diff --git a/app/views/admin/nav.html b/app/views/admin/nav.html index dbc2be6..bda8028 100644 --- a/app/views/admin/nav.html +++ b/app/views/admin/nav.html @@ -155,6 +155,14 @@ + +
  • + + + Upload File Size Limit + + +
  • diff --git a/app/views/admin/setting/upload.html b/app/views/admin/setting/upload.html new file mode 100644 index 0000000..ebc7a5a --- /dev/null +++ b/app/views/admin/setting/upload.html @@ -0,0 +1,65 @@ +{{template "admin/top.html" .}} +

    Upload File Size Limit

    + +
    + +
    +
    +
    +
    +
    + + + MB. Default is unlimit; +
    +
    + + + MB. Default is unlimit; +
    +
    + + + MB. Default is unlimit; +
    +
    + + + MB. Default is unlimit; +
    +
    + +
    + +
    +
    +
    +
    + +
    + +{{template "admin/footer.html" .}} + + + +{{template "admin/end.html" .}} \ No newline at end of file diff --git a/app/views/member/user/avatar.html b/app/views/member/user/avatar.html index f3fa94c..0250c5d 100644 --- a/app/views/member/user/avatar.html +++ b/app/views/member/user/avatar.html @@ -31,7 +31,8 @@ {{template "member/footer.html" .}} diff --git a/public/js/app/attachment_upload.js b/public/js/app/attachment_upload.js index 2998f3a..8b25956 100644 --- a/public/js/app/attachment_upload.js +++ b/public/js/app/attachment_upload.js @@ -83,10 +83,11 @@ define('attachment_upload', ['jquery.ui.widget', 'fileupload'], function(){ // 检查文件大小 var size = data.files[0].size; - if(typeof size == 'number' && size > 1024 * 1024 * 5) { + var maxFileSize = +GlobalConfigs["uploadAttachSize"] || 100; + if(typeof size == 'number' && size > 1024 * 1024 * maxFileSize) { tpl.find("img").remove(); tpl.removeClass("alert-info").addClass("alert-danger"); - tpl.append(" Warning: File size is bigger than 5M"); + tpl.append(" Warning: File size is bigger than " + maxFileSize + "M"); setTimeout((function(tpl) { return function() { tpl.remove(); @@ -156,10 +157,11 @@ define('attachment_upload', ['jquery.ui.widget', 'fileupload'], function(){ // 检查文件大小 var size = data.files[0].size; - if(typeof size == 'number' && size > 1024 * 1024) { + var maxFileSize = +GlobalConfigs["uploadAvatarSize"] || 100; + if(typeof size == 'number' && size > 1024 * 1024 * maxFileSize) { tpl.find("img").remove(); tpl.removeClass("alert-info").addClass("alert-danger"); - tpl.append(" Warning: File size is bigger than 1M"); + tpl.append(" Warning: File size is bigger than " + maxFileSize + "M"); setTimeout((function(tpl) { return function() { tpl.remove(); diff --git a/public/tinymce/plugins/leaui_image/public/css/style.css b/public/tinymce/plugins/leaui_image/public/css/style.css index c5ade2f..f1e9dee 100644 --- a/public/tinymce/plugins/leaui_image/public/css/style.css +++ b/public/tinymce/plugins/leaui_image/public/css/style.css @@ -34,7 +34,7 @@ right: 10px; width: 300px; max-height: 240px; - overflow: scroll; + overflow: auto; z-index: 3; } /**/ diff --git a/public/tinymce/plugins/leaui_image/public/js/main.js b/public/tinymce/plugins/leaui_image/public/js/main.js index 946eb50..670840a 100644 --- a/public/tinymce/plugins/leaui_image/public/js/main.js +++ b/public/tinymce/plugins/leaui_image/public/js/main.js @@ -718,7 +718,7 @@ var o = { $('#upload').fileupload({ dataType: 'json', acceptFileTypes: /(\.|\/)(gif|jpg|jpeg|png|jpe)$/i, - maxFileSize: 210000, + // maxFileSize: 210000, // This element will accept file drag/drop uploading dropZone: $('#drop'), @@ -734,9 +734,17 @@ var o = { // This function is called when a file is added to the queue; // either via the browse button, or via drag/drop: add: function(e, data) { - + // 文件大小限制 + var size = data.files[0].size; + var maxFileSize = +parent.GlobalConfigs["uploadImageSize"] || 100; + if(typeof size == 'number' && size > 1024 * 1024 * maxFileSize) { + var tpl = $('
  • '); + tpl.find('div').append('Warning: ' + data.files[0].name + ' [' + formatFileSize(data.files[0].size) + '] is bigger than ' + maxFileSize + 'M '); + tpl.appendTo(ul); + return; + } + var tpl = $('
  • '); - // Append the file name and file size tpl.find('div').append(data.files[0].name + ' [' + formatFileSize(data.files[0].size) + ']'); @@ -763,6 +771,11 @@ var o = { var tpl = $('
  • '); tpl.find('div').append('Error: ' + data.files[0].name + ' [' + formatFileSize(data.files[0].size) + '] ' + data.result.Msg); data.context.append(tpl); + setTimeout((function(tpl) { + return function() { + tpl.remove(); + } + })(tpl), 3000); } $("#upload-msg").scrollTop(1000); },