diff --git a/controllers/upload.py b/controllers/upload.py index 5b320cf..f6088d1 100644 --- a/controllers/upload.py +++ b/controllers/upload.py @@ -2,7 +2,7 @@ from fastapi import APIRouter, HTTPException, UploadFile, File, Form from fastapi.responses import JSONResponse from nc_py_api import NextcloudException -from utils import get_nextcloud_client, create_directory_recursive +from utils import get_nextcloud_client, create_directory_recursive, normalize_upload_paths from typing import Optional router = APIRouter() @@ -32,15 +32,8 @@ async def upload_file( # Déterminer le nom du fichier final_filename = filename if filename else file.filename - # Normaliser le chemin de destination - if path == "/" or path == "": - destination_path = f"/{final_filename}" - directory_path = "/" - else: - # Enlever les slashes de début/fin et ajouter le nom du fichier - clean_path = path.strip("/") - destination_path = f"/{clean_path}/{final_filename}" - directory_path = f"/{clean_path}" + # Normaliser les chemins (gère le cas où path contient déjà le nom du fichier) + directory_path, destination_path = normalize_upload_paths(path, final_filename) # Créer le dossier de destination récursivement s'il n'existe pas if directory_path != "/": diff --git a/controllers/upload_base64.py b/controllers/upload_base64.py index fd01d89..fadc68c 100644 --- a/controllers/upload_base64.py +++ b/controllers/upload_base64.py @@ -2,7 +2,7 @@ from fastapi import APIRouter, HTTPException, Form from fastapi.responses import JSONResponse from nc_py_api import NextcloudException -from utils import get_nextcloud_client, create_directory_recursive +from utils import get_nextcloud_client, create_directory_recursive, normalize_upload_paths from typing import Optional import base64 @@ -32,15 +32,8 @@ async def upload_file_base64( # Créer le client Nextcloud nc = get_nextcloud_client() - # Normaliser le chemin de destination - if path == "/" or path == "": - destination_path = f"/{filename}" - directory_path = "/" - else: - # Enlever les slashes de début/fin et ajouter le nom du fichier - clean_path = path.strip("/") - destination_path = f"/{clean_path}/{filename}" - directory_path = f"/{clean_path}" + # Normaliser les chemins (gère le cas où path contient déjà le nom du fichier) + directory_path, destination_path = normalize_upload_paths(path, filename) # Créer le dossier de destination récursivement s'il n'existe pas if directory_path != "/": diff --git a/utils.py b/utils.py index 5b00652..bcd1212 100644 --- a/utils.py +++ b/utils.py @@ -21,6 +21,48 @@ def get_nextcloud_client() -> Nextcloud: ) +def normalize_upload_paths(path: str, filename: str) -> tuple[str, str]: + """ + Normalise les chemins d'upload pour gérer les cas où le path contient déjà le nom du fichier + + Args: + path: Chemin fourni par l'utilisateur (peut contenir ou non le nom du fichier) + filename: Nom du fichier + + Returns: + tuple: (directory_path, destination_path) + """ + if path == "/" or path == "": + return "/", f"/{filename}" + + # Nettoyer le chemin + clean_path = path.strip("/") + + # Si le path se termine par le même nom que le filename, on l'enlève + # Ex: /GED/Mouvements/symbol.svg avec filename=symbol.svg -> /GED/Mouvements + path_parts = clean_path.split("/") + last_part = path_parts[-1] + + # Vérifier si la dernière partie du path est le même que le filename + # ou si elle a une extension de fichier (contient un point dans les 5 derniers caractères) + if last_part == filename or (len(last_part) > 4 and "." in last_part[-5:]): + # Enlever la dernière partie qui est probablement un nom de fichier + if len(path_parts) > 1: + directory_path = "/" + "/".join(path_parts[:-1]) + else: + directory_path = "/" + else: + directory_path = "/" + clean_path + + # Construire le chemin de destination final + if directory_path == "/": + destination_path = f"/{filename}" + else: + destination_path = f"{directory_path}/{filename}" + + return directory_path, destination_path + + def create_directory_recursive(nc: Nextcloud, directory_path: str) -> None: """ Crée un dossier et tous ses parents récursivement si nécessaire