Skip to content
Permalink
Browse files
优化上传URL图片时的图片格式校验
  • Loading branch information
zmister committed Oct 27, 2021
1 parent 39b2f92 commit 0fff3cf126b28f9cc735bfe4b30edb340c241b6d
Showing with 20 additions and 0 deletions.
  1. +20 −0 app_doc/util_upload_img.py
@@ -115,6 +115,16 @@ def ice_url_img_upload(url,user):
r = requests.get(url, headers=header, stream=True)

if r.status_code == 200:
# 判断是否为允许上传的图片类型
remote_type = r.headers['Content-Type'].split("/")[1]
if remote_type not in settings.ALLOWED_IMG:
logger.error("上传了不允许的URL图片:{}".format(url))
resp_data = {
'error': 0,
'name': {},
'file':{}
}
return resp_data
with open(path_file, 'wb') as f:
f.write(r.content) # 保存文件
Image.objects.create(
@@ -274,6 +284,16 @@ def url_img_upload(url,dir_name,user):
try:
r = requests.get(url, headers=header, stream=True)
if r.status_code == 200:
# 判断是否为允许上传的图片类型
remote_type = r.headers['Content-Type'].split("/")[1]
if remote_type not in settings.ALLOWED_IMG:
logger.error("上传了不允许的URL图片:{}".format(url))
resp_data = {
'msg': '',
'code': 1,
'data': {}
}
return resp_data
with open(path_file, 'wb') as f:
f.write(r.content) # 保存文件
Image.objects.create(

0 comments on commit 0fff3cf

Please sign in to comment.