Moin限制上传文件大小
Posted on Thu 11 March 2010 in 我用(IT)
1. 解决方法
# AttachFile.py line 572
# restrict upload file length
#add by LiYan @2010/03/11
limit = 1024*1024*300
file_size = bytes
if file_size>=limit:
# get attached file name
attach_dir = getAttachDir(request, pagename)
fpath = os.path.join(attach_dir, target).encode(config.charset)
os.remove(fpath)
return _(u"reach file limit(300M)! your file size= %d" % file_size)
2. 分析过程
- 首先定位一下儿可能需要改的地方
-
因为上传对应的链接是?action=AttachFile,所以可以很方便的判定上传工作是一个叫AttachFile的Action,在我本地的地址是
-
$lib/python2.x/site-packages/MoinMoin/action/AttachFile.py
-
-
-
打开AttachFile.py,定位上传的地方:
-
def _do_upload(pagename, request):
-
- 找到上传的逻辑:
-
target, bytes = add_attachment(request, pagename, target, file_upload.stream, overwrite=overwrite)
target是文件名
bytes是文件大小
-
- 都在add_attachment了:
- 呵呵,是直接把文件流存到本地了
- 所以,想避免大文件上传就是上传完了之后,如果文件超过指定大小了,就删掉
- 搞定
3. 调试
- 看看file_upload.stream里面能不能获得文件大小
-
AttachFile.py 568
return _(", ".join(dir(file_upload.stream)))
-
- file_upload.content_length是0,呵呵