From 8b9b4e6e08bf90c091e489db2ce79890d0699476 Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Tue, 20 May 2025 16:28:32 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Add=20new=20constants=20for=20co?= =?UTF-8?q?nfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config.py | 13 +++++++++++-- app/routes/images.py | 25 ++++++++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/config.py b/app/config.py index 3d163f2..0c932af 100644 --- a/app/config.py +++ b/app/config.py @@ -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 \ No newline at end of file +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" +] \ No newline at end of file diff --git a/app/routes/images.py b/app/routes/images.py index a8c5100..69ebcdf 100644 --- a/app/routes/images.py +++ b/app/routes/images.py @@ -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)},