Performance: Allowed file types validation
This affects page load speed and user interaction responsiveness by preventing unnecessary file uploads and server processing.
from flask import request ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS file = request.files['file'] if file and allowed_file(file.filename): file.save('/uploads/' + file.filename) else: return 'Invalid file type', 400
from flask import request file = request.files['file'] # No validation on file type file.save('/uploads/' + file.filename)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No file type validation | N/A | N/A | N/A | [X] Bad |
| Early file type validation on server | N/A | N/A | N/A | [OK] Good |
photo.png and allowed_extensions = {'png', 'jpg'}?
filename = 'photo.png'
if filename.split('.')[-1] in allowed_extensions:
result = 'Allowed'
else:
result = 'Not Allowed'allowed_extensions = {'txt', 'pdf'}
filename = 'report.pdf'
if filename.split('.')[-1] == allowed_extensions:
print('File allowed')
else:
print('File not allowed')Pass the test and this pattern moves unknown