# import uuid, os, base64 # from PIL import Image # from werkzeug.utils import secure_filename # from config import Config # from werkzeug.utils import secure_filename # # from config import ALLOWED_EXT, UPLOAD_FOLDER # print("utils.py loaded") # print("UPLOAD_FOLDER:", Config.UPLOAD_FOLDER) # print("ALLOWED_EXTENSIONS:", Config.ALLOWED_EXTENSIONS) # def allowed(filename): # return os.path.splitext(filename)[1].lower() in Config.ALLOWED_EXTENSIONS # def save_upload(file): # ext = os.path.splitext(secure_filename(file.filename))[1].lower() # filename = uuid.uuid4().hex + ext # path = os.path.join(Config.UPLOAD_FOLDER, filename) # file.save(path) # return path # # def to_base64(np_img): # # from PIL import Image # # import io # # pil_img = Image.fromarray(np.uint8(np_img)) # # buffered = io.BytesIO() # # pil_img.save(buffered, format="PNG") # # return base64.b64encode(buffered.getvalue()).decode("utf-8") # def to_base64(pil_img): # import io # import base64 # buffered = io.BytesIO() # pil_img.save(buffered, format="PNG") # return base64.b64encode(buffered.getvalue()).decode("utf-8") # # def allowed_file(filename): # # """Check if file extension is allowed""" # # return '.' in filename and \ # # filename.rsplit('.', 1)[1].lower() in Config.ALLOWED_EXTENSIONS # def allowed_file(filename): # print("๐Ÿงพ Received filename:", filename) # if not filename or '.' not in filename: # print("โŒ Invalid filename") # return False # ext = '.' + filename.rsplit('.', 1)[1].lower() # print("๐Ÿ“Ž Extracted extension:", ext) # print("โœ… Allowed extensions:", Config.ALLOWED_EXTENSIONS) # if ext not in Config.ALLOWED_EXTENSIONS: # print("โŒ Extension NOT allowed:", ext) # return False # print("โœ… FILE TYPE OK") # return True # def save_uploaded_file(file): # """Save uploaded file with secure filename""" # if file and allowed_file(file.filename): # # filename = secure_filename(file.filename) # filename = secure_filename(file.filename or "image.jpg") # # Add unique identifier to prevent overwriting # unique_filename = f"{uuid.uuid4().hex}_{filename}" # filepath = os.path.join(Config.UPLOAD_FOLDER, unique_filename) # file.save(filepath) # return filepath # return None # def get_image_dimensions(image_path): # """Get image dimensions for display""" # try: # with Image.open(image_path) as img: # return img.size # except: # return (300, 300) # Default dimensions import uuid, os, base64 from PIL import Image from werkzeug.utils import secure_filename from config import Config from werkzeug.utils import secure_filename # from config import ALLOWED_EXT, UPLOAD_FOLDER print("utils.py loaded") print("UPLOAD_FOLDER:", Config.UPLOAD_FOLDER) print("ALLOWED_EXTENSIONS:", Config.ALLOWED_EXTENSIONS) def allowed(filename): return os.path.splitext(filename)[1].lower() in Config.ALLOWED_EXTENSIONS def save_upload(file): ext = os.path.splitext(secure_filename(file.filename))[1].lower() filename = uuid.uuid4().hex + ext path = os.path.join(Config.UPLOAD_FOLDER, filename) file.save(path) return path # def to_base64(np_img): # from PIL import Image # import io # pil_img = Image.fromarray(np.uint8(np_img)) # buffered = io.BytesIO() # pil_img.save(buffered, format="PNG") # return base64.b64encode(buffered.getvalue()).decode("utf-8") def to_base64(pil_img): import io import base64 buffered = io.BytesIO() pil_img.save(buffered, format="PNG") return base64.b64encode(buffered.getvalue()).decode("utf-8") # def allowed_file(filename): # """Check if file extension is allowed""" # return '.' in filename and \ # filename.rsplit('.', 1)[1].lower() in Config.ALLOWED_EXTENSIONS def allowed_file(filename): print("๐Ÿงพ Received filename:", filename) if not filename or '.' not in filename: print("โŒ Invalid filename") return False ext = '.' + filename.rsplit('.', 1)[1].lower() print("๐Ÿ“Ž Extracted extension:", ext) print("โœ… Allowed extensions:", Config.ALLOWED_EXTENSIONS) if ext not in Config.ALLOWED_EXTENSIONS: print("โŒ Extension NOT allowed:", ext) return False print("โœ… FILE TYPE OK") return True def save_uploaded_file(file): """Save uploaded file with secure filename""" if file and allowed_file(file.filename): # filename = secure_filename(file.filename) filename = secure_filename(file.filename or "image.jpg") # Add unique identifier to prevent overwriting unique_filename = f"{uuid.uuid4().hex}_{filename}" filepath = os.path.join(Config.UPLOAD_FOLDER, unique_filename) file.save(filepath) return filepath return None def get_image_dimensions(image_path): """Get image dimensions for display""" try: with Image.open(image_path) as img: return img.size except: return (300, 300) # Default dimensions