From d879aa32d63a86c3ab283375ad9f2b58e0affee5 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Sat, 18 Nov 2023 11:52:51 +1100 Subject: [PATCH] fix: Cleanup file name on upload --- avatar.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/avatar.py b/avatar.py index 7900cd6..9980a54 100644 --- a/avatar.py +++ b/avatar.py @@ -4,6 +4,7 @@ import db import random import json import varo +import re IMAGE_LOCATION = os.getenv('IMAGE_LOCATION') @@ -25,7 +26,10 @@ def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS def save_avatar(file,owner): - filename = file.filename + filename = re.sub(r'[^a-zA-Z0-9]', '', filename).lower() + + + while os.path.exists(f"{IMAGE_LOCATION}/{filename}"): filename = f"{random.randint(0,1000000)}-{filename}" file.save(f"{IMAGE_LOCATION}/{filename}")