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 首页
@ -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)
}

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

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

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