fix(pets): adds file ext validation on the server side

This commit is contained in:
2024-12-10 17:48:29 -03:00
parent 24c01ff2bb
commit 4c5cdfb13d
2 changed files with 14 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ from app.utils.errors.pets.pet_register_errors import PetRegisterError
from app.utils.flash_message import FlashMessage from app.utils.flash_message import FlashMessage
from app.utils.helpers import pet_sex_id_to_str from app.utils.helpers import pet_sex_id_to_str
from app.utils.validators.pet_validators import PetValidators from app.utils.validators.pet_validators import PetValidators
from app.utils.validators.validators import Validators
class PetService: class PetService:
@staticmethod @staticmethod
@@ -54,14 +55,18 @@ class PetService:
img = request.files['img'] img = request.files['img']
img_url = None img_url = None
if(request.files['img']): if(img):
try: try:
if not Validators.allowed_file_img(img.filename):
raise(PetRegisterError("Invalid image format"))
cloudinary.config(cloud_name = os.environ.get('CLOUD_NAME'), api_key=os.getenv('API_KEY'), cloudinary.config(cloud_name = os.environ.get('CLOUD_NAME'), api_key=os.getenv('API_KEY'),
api_secret=os.getenv('API_SECRET')) api_secret=os.getenv('API_SECRET'))
upload_result = cloudinary.uploader.upload(img) upload_result = cloudinary.uploader.upload(img)
img_url = upload_result['secure_url'] img_url = upload_result['secure_url']
except PetRegisterError as e:
flash(FlashMessage(e.message, AlertType.DANGER.value ))
except: except:
print("err!") print("error")
try: try:
name = PetValidators.is_valid_name(request.form.get('name')) name = PetValidators.is_valid_name(request.form.get('name'))

View File

@@ -1,6 +1,5 @@
import re import re
from typing import Optional from typing import Optional
class Validators: class Validators:
@staticmethod @staticmethod
@@ -17,4 +16,10 @@ class Validators:
float(value) float(value)
return True return True
except: except:
return False return False
@staticmethod
def allowed_file_img(filename):
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'webp'}
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS