склади и инструмент готовы

This commit is contained in:
2025-12-07 19:36:28 +03:00
parent 54bf21d52d
commit 65a3bc1671
65 changed files with 3485 additions and 115 deletions
+34 -37
View File
@@ -1,56 +1,53 @@
from utils.loggers import logger
# def saveImage(imageData, fileName: str):
# try:
# imageFormat = imageData.split(';')[0].split('/')[1]
# if imageFormat != 'png':
# logger.error(f"Неподдерживаемый формат изображения: {imageFormat}")
# return False
# imageData = imageData.split(';base64,')[1]
# with open(f"static/images/{fileName}", "wb") as f:
# f.write(base64.b64decode(imageData))
# logger.info(f"Изображение {fileName} успешно сохранено")
# return True
# except Exception as e:
# logger.error(f"Ошибка сохранения изображения: {str(e)}")
# return False
# UPLOAD_DIR = "uploads"
# os.makedirs(UPLOAD_DIR, exist_ok=True)
def saveImage(file_bytes, fileName: str):
def saveImage(file_bytes: bytes, file_name: str) -> bool:
import os
from PIL import Image
import io
# Загружаем изображение через Pillow
# Убедимся, что путь существует
file_name = f"api/{file_name}"
os.makedirs(os.path.dirname(file_name), exist_ok=True)
# Загружаем изображение
try:
img = Image.open(io.BytesIO(file_bytes))
except Exception:
logger.error("Неподдерживаемый формат изображения")
return False
# Конвертация (если нужно)
if img.mode not in ("RGB", "RGBA", "P"):
img = img.convert("RGB")
# Сохранение в выбранный формат
try:
logger.info(f"Сохраняем изображение {fileName}")
img.save(fileName, "PNG")
img.load()
except Exception as e:
logger.error(f"Ошибка сохранения изображения: {str(e)}")
logger.error(f"[ImageSave] Unsupported image format: {e}")
return False
logger.info(f"Изображение {fileName} успешно сохранено")
return True
# Конвертация в нормальный режим (PNG поддерживает RGBA/RGB)
try:
if img.mode not in ("RGB", "RGBA"):
img = img.convert("RGB")
except Exception as e:
logger.error(f"[ImageSave] Mode conversion error: {e}")
return False
# Сохраняем как PNG
try:
target_path = file_name
if not target_path.lower().endswith(".png"):
target_path += ".png"
logger.info(f"[ImageSave] Saving image to {target_path}")
img.save(target_path, "PNG")
return True
except Exception as e:
logger.error(f"[ImageSave] Error saving image: {e}")
return False
def deleteImage(fileName: str):
if fileName.endswith("default.png"):
return True
try:
import os
file_name = f"api/{file_name}"
logger.info(f"Удаляем изображение {fileName}")
os.remove(f"static/images/{fileName}")
logger.info(f"Изображение {fileName} успешно удалено")