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 @@ </span> </a> </li> + + <li> + <a href="/admin/t?t=setting/upload"> + <span> + Upload File Size Limit + </span> + </a> + </li> </ul> </li> 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" .}} +<div class="m-b-md"> <h3 class="m-b-none">Upload File Size Limit</h3></div> + +<div class="row"> + +<div class="col-sm-6"> + <form id="add_user_form"> + <section class="panel panel-default"> + <div class="panel-body"> + <div class="form-group"> + <label>Image</label> + <input type="text" class="form-control" name="uploadImageSize" value="{{.str.uploadImageSize}}"> + MB. Default is unlimit; + </div> + <div class="form-group"> + <label>Avatar</label> + <input type="text" class="form-control" name="uploadAvatarSize" value="{{.str.uploadAvatarSize}}"> + MB. Default is unlimit; + </div> + <div class="form-group"> + <label>Blog Logo</label> + <input type="text" class="form-control" name="uploadBlogLogoSize" value="{{.str.uploadBlogLogoSize}}"> + MB. Default is unlimit; + </div> + <div class="form-group"> + <label>Attachment</label> + <input type="text" class="form-control" name="uploadAttachSize" value="{{.str.uploadAttachSize}}"> + MB. Default is unlimit; + </div> + </div> + + <footer class="panel-footer text-right bg-light lter"> + <button type="submit" id="submit" class="btn btn-success btn-s-xs">Submit</button> + </footer> + </section> + </form> +</div> + +</div> + +{{template "admin/footer.html" .}} +<script src="/public/admin/js/jquery-validation-1.13.0/jquery.validate.js"></script> +<script> +$(function() { + init_validator("#add_user_form"); + + $("#submit").click(function(e){ + e.preventDefault(); + var t = this; + if($("#add_user_form").valid()) { + $(t).button('loading'); + ajaxPost("/adminSetting/uploadSize", getFormJsonData("add_user_form"), function(ret){ + $(t).button('reset') + if(!ret.Ok) { + art.alert(ret.Msg) + } else { + art.tips("Success"); + } + }); + } + }); +}); +</script> + +{{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" .}} <script src="/js/require.js"></script> <script> -var UrlPrefix = "{{.siteUrl}}"; // 为了发weibo +var UrlPrefix = "{{.siteUrl}}"; // 为了发weibo +var GlobalConfigs = {{.globalConfigs|jsonJs}}; // 2014/11/9 beta2 require.config({ baseUrl: '/public', paths: { diff --git a/app/views/note/note-dev.html b/app/views/note/note-dev.html index dc5fc22..fd7ad9c 100644 --- a/app/views/note/note-dev.html +++ b/app/views/note/note-dev.html @@ -841,6 +841,7 @@ var notes = json({{.notes}}); var noteContentJson = json({{.noteContentJson}}); var tagsJson = json({{.tagsJson}}); LEA.locale = "{{.locale}}"; +var GlobalConfigs = {{.globalConfigs|jsonJs}}; // 2014/11/9 beta2 </script> <!-- 渲染view --> 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 = $('<li><div class="alert alert-danger"><a class="close" data-dismiss="alert">×</a></div></li>'); + tpl.find('div').append('<b>Warning:</b> ' + data.files[0].name + ' <small>[<i>' + formatFileSize(data.files[0].size) + '</i>] is bigger than ' + maxFileSize + 'M</small> '); + tpl.appendTo(ul); + return; + } + var tpl = $('<li><div class="alert alert-info"><img class="loader" src="public/images/ajax-loader.gif"> <a class="close" data-dismiss="alert">×</a></div></li>'); - // Append the file name and file size tpl.find('div').append(data.files[0].name + ' <small>[<i>' + formatFileSize(data.files[0].size) + '</i>]</small>'); @@ -763,6 +771,11 @@ var o = { var tpl = $('<li><div class="alert alert-danger"><a class="close" data-dismiss="alert">×</a></div></li>'); tpl.find('div').append('<b>Error:</b> ' + data.files[0].name + ' <small>[<i>' + formatFileSize(data.files[0].size) + '</i>]</small> ' + data.result.Msg); data.context.append(tpl); + setTimeout((function(tpl) { + return function() { + tpl.remove(); + } + })(tpl), 3000); } $("#upload-msg").scrollTop(1000); },