iris-backend / utils.py
trretretret's picture
Fix static structure for HF deployment
9232fb9
Raw
History Blame Contribute Delete
5.19 kB
# 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