upload file size limit [ok]

This commit is contained in:
life
2014-11-09 18:00:23 +08:00
parent 2a457d6027
commit 346abfe91d
14 changed files with 165 additions and 16 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -5,6 +5,7 @@ import (
// . "github.com/leanote/leanote/app/lea"
"github.com/leanote/leanote/app/info"
"strings"
"fmt"
)
// admin 首页
@ -117,3 +118,12 @@ func (c AdminSetting) Mongodb(mongodumpPath, mongorestorePath string) revel.Resu
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)
}

View File

@ -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 {

View File

@ -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"))
}

View File

@ -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
}

View File

@ -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>

View File

@ -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" .}}

View File

@ -32,6 +32,7 @@
<script src="/js/require.js"></script>
<script>
var UrlPrefix = "{{.siteUrl}}"; // 为了发weibo
var GlobalConfigs = {{.globalConfigs|jsonJs}}; // 2014/11/9 beta2
require.config({
baseUrl: '/public',
paths: {

View File

@ -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 -->

View File

@ -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();

View File

@ -34,7 +34,7 @@
right: 10px;
width: 300px;
max-height: 240px;
overflow: scroll;
overflow: auto;
z-index: 3;
}
/**/

View File

@ -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);
},