Skip to content

Commit

Permalink
优化上传URL图片时的图片格式校验
Browse files Browse the repository at this point in the history
  • Loading branch information
zmister committed Oct 27, 2021
1 parent 39b2f92 commit 0fff3cf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app_doc/util_upload_img.py
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0fff3cf

Please sign in to comment.