Refactor upload_base64 endpoint: Change from using a Pydantic model to FastAPI Form parameters for file uploads, improving clarity and usability of the API. Update variable references for consistency.
Some checks failed
Docker Build / build (push) Has been cancelled
Some checks failed
Docker Build / build (push) Has been cancelled
This commit is contained in:
parent
9c79988559
commit
4c7ee65d34
@ -1,7 +1,6 @@
|
||||
"""Contrôleur pour uploader des fichiers en base64 (pour clients Omnis/anciens)"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi import APIRouter, HTTPException, Form
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel
|
||||
from nc_py_api import NextcloudException
|
||||
from utils import get_nextcloud_client
|
||||
from typing import Optional
|
||||
@ -10,21 +9,21 @@ import base64
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class FileUploadBase64(BaseModel):
|
||||
"""Modèle pour l'upload de fichier en base64"""
|
||||
path: str # Chemin du dossier de destination
|
||||
filename: str # Nom du fichier
|
||||
file_base64: str # Contenu du fichier encodé en base64
|
||||
content_type: Optional[str] = "application/octet-stream" # Type MIME du fichier
|
||||
|
||||
|
||||
@router.post("/upload-base64")
|
||||
async def upload_file_base64(data: FileUploadBase64):
|
||||
async def upload_file_base64(
|
||||
path: str = Form(..., description="Chemin du dossier de destination"),
|
||||
filename: str = Form(..., description="Nom du fichier"),
|
||||
file_base64: str = Form(..., description="Contenu du fichier encodé en base64"),
|
||||
content_type: Optional[str] = Form("application/octet-stream", description="Type MIME du fichier")
|
||||
):
|
||||
"""
|
||||
Upload un fichier vers Nextcloud à partir de données base64
|
||||
|
||||
Args:
|
||||
data: Objet contenant le fichier en base64 et les métadonnées
|
||||
path: Chemin du dossier de destination
|
||||
filename: Nom du fichier
|
||||
file_base64: Contenu du fichier encodé en base64
|
||||
content_type: Type MIME du fichier
|
||||
|
||||
Returns:
|
||||
JSON contenant les informations du fichier uploadé
|
||||
@ -34,13 +33,13 @@ async def upload_file_base64(data: FileUploadBase64):
|
||||
nc = get_nextcloud_client()
|
||||
|
||||
# Normaliser le chemin de destination
|
||||
if data.path == "/" or data.path == "":
|
||||
destination_path = f"/{data.filename}"
|
||||
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 = data.path.strip("/")
|
||||
destination_path = f"/{clean_path}/{data.filename}"
|
||||
clean_path = path.strip("/")
|
||||
destination_path = f"/{clean_path}/{filename}"
|
||||
directory_path = f"/{clean_path}"
|
||||
|
||||
# Vérifier si le dossier existe, sinon le créer
|
||||
@ -62,7 +61,7 @@ async def upload_file_base64(data: FileUploadBase64):
|
||||
|
||||
# Décoder le fichier base64
|
||||
try:
|
||||
file_content = base64.b64decode(data.file_base64)
|
||||
file_content = base64.b64decode(file_base64)
|
||||
except Exception as decode_error:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
@ -79,7 +78,7 @@ async def upload_file_base64(data: FileUploadBase64):
|
||||
if "409" in str(e) or "412" in str(e):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail=f"Le fichier '{data.filename}' existe déjà"
|
||||
detail=f"Le fichier '{filename}' existe déjà"
|
||||
)
|
||||
else:
|
||||
raise HTTPException(
|
||||
@ -92,10 +91,10 @@ async def upload_file_base64(data: FileUploadBase64):
|
||||
"success": True,
|
||||
"message": "Fichier uploadé avec succès",
|
||||
"file": {
|
||||
"name": data.filename,
|
||||
"name": filename,
|
||||
"path": destination_path,
|
||||
"size": len(file_content),
|
||||
"content_type": data.content_type
|
||||
"content_type": content_type
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user