🔧 Add new constants for config

nominatim_fix
Alix JEUDI--LEMOINE 3 weeks ago
parent cce7d5ccc5
commit 8b9b4e6e08

@ -8,8 +8,17 @@ MONGODB_DATABASE = os.getenv("MONGODB_DATABASE", "memorymap")
# Constants for JWT
SECRET_KEY = os.getenv("JWT_SECRET_KEY", "_2YfT44$xF.Tg_xI63UH3D7:N+>pZN2';j%>7H@?e0:Xor'pV[") # temporary of course :)
ALGORITHM = os.getenv("JWT_ALGORITHM", "HS256") # TODO: check if broken (don't believe)
ALGORITHM = os.getenv("JWT_ALGORITHM", "HS256")
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("JWT_ACCESS_TOKEN_EXPIRE_MINUTES", 30)) # TODO: check what to add here / maybe need to evaluate criticity of that?
# Constants for OAuth2
TOKEN_URL = "/api/v1/login" # Path to the auth
# Constants for images
UPLOAD_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "images")
MAX_IMAGE_SIZE = 8 * 1024 * 1024 # 8 Mo
ALLOWED_MIME_TYPES = [
"image/jpeg",
"image/png",
"image/heic"
]

@ -24,17 +24,8 @@ images_collection = db["images"]
pins_collection = db["pins"]
pin_permissions_collection = db["pin_permissions"]
# Configuration
UPLOAD_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "images")
MAX_IMAGE_SIZE = 8 * 1024 * 1024 # 8 Mo
ALLOWED_MIME_TYPES = [
"image/jpeg",
"image/png",
"image/heic"
]
# Créer le dossier images s'il n'existe pas
os.makedirs(UPLOAD_DIR, exist_ok=True)
os.makedirs(config.UPLOAD_DIR, exist_ok=True)
images_router = APIRouter(
prefix="/image",
@ -116,23 +107,23 @@ async def add_image(
):
# Vérifier la taille du fichier
image_data = await image.read()
if len(image_data) > MAX_IMAGE_SIZE:
if len(image_data) > config.MAX_IMAGE_SIZE:
raise HTTPException(status_code=413, detail="Image too large (max 8MB)")
# Vérifier le type MIME
mime_type = magic.from_buffer(image_data, mime=True)
if mime_type not in ALLOWED_MIME_TYPES:
if mime_type not in config.ALLOWED_MIME_TYPES:
raise HTTPException(status_code=415, detail="Unsupported image type")
# Sauvegarder temporairement le fichier
temp_path = os.path.join(UPLOAD_DIR, f"temp_{image.filename}")
temp_path = os.path.join(config.UPLOAD_DIR, f"temp_{image.filename}")
with open(temp_path, "wb") as f:
f.write(image_data)
try:
# Traiter l'image et obtenir son hash
file_hash, extension = process_image(temp_path)
final_path = os.path.join(UPLOAD_DIR, f"{file_hash}.{extension}")
final_path = os.path.join(config.UPLOAD_DIR, f"{file_hash}.{extension}")
# Si l'image n'existe pas déjà physiquement, la déplacer
if not os.path.exists(final_path):
@ -169,7 +160,7 @@ async def add_image(
)
async def get_image(id: str, current_user: User = Depends(get_current_user)):
image = check_image_permissions(id, current_user)
image_path = os.path.join(UPLOAD_DIR, f"{image['image_hash']}.jpg")
image_path = os.path.join(config.UPLOAD_DIR, f"{image['image_hash']}.jpg")
if not os.path.exists(image_path):
raise HTTPException(status_code=404, detail="Image file not found")
@ -191,7 +182,7 @@ async def delete_image(id: str, current_user: User = Depends(get_current_user)):
# Si plus personne n'utilise l'image, supprimer le fichier
if not other_images:
image_path = os.path.join(UPLOAD_DIR, f"{image['image_hash']}.jpg")
image_path = os.path.join(config.UPLOAD_DIR, f"{image['image_hash']}.jpg")
if os.path.exists(image_path):
os.remove(image_path)
@ -206,7 +197,7 @@ async def update_caption(
caption_data: ImageUpdateCaptionDTO,
current_user: User = Depends(get_current_user)
):
image = check_image_permissions(id, current_user)
check_image_permissions(id, current_user)
images_collection.update_one(
{"_id": ObjectId(id)},

Loading…
Cancel
Save