feat: Refactored uplocad_image(), MIME check skipping added
This commit is contained in:
parent
87f6c3a048
commit
8e4ab793f9
38
imgs.py
38
imgs.py
@ -48,30 +48,38 @@ def upload_image():
|
|||||||
# Handle request from CLI
|
# Handle request from CLI
|
||||||
if request.files.get('image'):
|
if request.files.get('image'):
|
||||||
file = request.files.get('image')
|
file = request.files.get('image')
|
||||||
if file.content_type in config['imgs.allowed_mime_types']:
|
rq = 'cli'
|
||||||
image_name = upload_file(file)
|
|
||||||
return get_image_url(image_name) + '\n'
|
|
||||||
else:
|
|
||||||
# Prevent recource leek. Force close buffered file
|
|
||||||
request.body.close()
|
|
||||||
response.status = 415
|
|
||||||
return 'Error: bad file MIME type\n'
|
|
||||||
# Handle request from web-browser
|
# Handle request from web-browser
|
||||||
elif request.files.get('image_web'):
|
elif request.files.get('image_web'):
|
||||||
file = request.files.get('image_web')
|
file = request.files.get('image_web')
|
||||||
|
rq = 'web'
|
||||||
|
|
||||||
|
if config['imgs.allowed_mime_types'] == '*':
|
||||||
|
# Skip MIME checking.
|
||||||
|
image_name = upload_file(file)
|
||||||
|
else:
|
||||||
if file.content_type in config['imgs.allowed_mime_types']:
|
if file.content_type in config['imgs.allowed_mime_types']:
|
||||||
|
# Upload file!
|
||||||
image_name = upload_file(file)
|
image_name = upload_file(file)
|
||||||
return template('index.tpl',
|
|
||||||
uploaded = True, not_found = False, bad_mime_type = False,
|
|
||||||
base_url = get_base_url(), image_url = get_image_url(image_name))
|
|
||||||
else:
|
else:
|
||||||
|
# Show MIME type error!
|
||||||
# Prevent recource leek. Force close buffered file
|
# Prevent recource leek. Force close buffered file
|
||||||
request.body.close()
|
request.body.close()
|
||||||
response.status = 415
|
response.status = 415
|
||||||
return template('index.tpl',
|
if rq == 'cli':
|
||||||
uploaded = False, not_found = False, bad_mime_type = True,
|
return 'Error: bad file MIME type\n'
|
||||||
allowed_mime_types = config['imgs.allowed_mime_types'],
|
else:
|
||||||
base_url = get_base_url(), image_url = 'None')
|
return template('index.tpl',
|
||||||
|
uploaded = False, not_found = False, bad_mime_type = True,
|
||||||
|
allowed_mime_types = config['imgs.allowed_mime_types'],
|
||||||
|
base_url = get_base_url(), image_url = 'None')
|
||||||
|
# Return 200 OK
|
||||||
|
if rq == 'cli':
|
||||||
|
return get_image_url(image_name) + '\n'
|
||||||
|
else:
|
||||||
|
return template('index.tpl',
|
||||||
|
uploaded = True, not_found = False, bad_mime_type = False,
|
||||||
|
base_url = get_base_url(), image_url = get_image_url(image_name))
|
||||||
|
|
||||||
@get('/<image_name>')
|
@get('/<image_name>')
|
||||||
def send_image(image_name):
|
def send_image(image_name):
|
||||||
|
Loading…
Reference in New Issue
Block a user