采用了对源码进行修改,增加对中文的支持。
使用环境 werkzeug 0.16.0 和 python 3.7。
关于 secure_filename() 的过滤代码写在 \Python36\Lib\site-packages\werkzeug\utils.py
初始代码
if isinstance(filename, text_type):
from unicodedata import normalize
filename = normalize('NFKD', filename).encode('ascii', 'ignore') # 转码
if not PY2:
filename = filename.decode('ascii') # 解码
for sep in os.path.sep, os.path.altsep:
if sep:
filename = filename.replace(sep, ' ')
filename = str(_filename_ascii_strip_re.sub('', '_'.join( # 正则
filename.split()))).strip('._')
from unicodedata import normalize
filename = normalize('NFKD', filename).encode('ascii', 'ignore') # 转码
if not PY2:
filename = filename.decode('ascii') # 解码
for sep in os.path.sep, os.path.altsep:
if sep:
filename = filename.replace(sep, ' ')
filename = str(_filename_ascii_strip_re.sub('', '_'.join( # 正则
filename.split()))).strip('._')
修改后代码
37行
正则增加对汉字和日语假名的部分
\u4E00-\u9FBF 中文
\u3040-\u30FF 假名
\u31F0-\u31FF 片假名扩展
_filename_ascii_add_strip_re = re.compile(r'[^A-Za-z0-9_\u4E00-\u9FBF\u3040-\u30FF\u31F0-\u31FF.-]')
384行 的两个”ascii”修改为 “utf-8”,两个都要修改
if isinstance(filename, text_type):
from unicodedata import normalize
filename = normalize('NFKD', filename).encode('utf-8', 'ignore') # 转码
if not PY2:
filename = filename.decode('utf-8') # 解码
for sep in os.path.sep, os.path.altsep:
if sep:
filename = filename.replace(sep, ' ')
from unicodedata import normalize
filename = normalize('NFKD', filename).encode('utf-8', 'ignore') # 转码
if not PY2:
filename = filename.decode('utf-8') # 解码
for sep in os.path.sep, os.path.altsep:
if sep:
filename = filename.replace(sep, ' ')
原文链接:https://blog.csdn.net/qq_30490489/article/details/92000197