Update README: Add documentation for downloading files in Base64 format, including endpoint details, parameters, examples, and response structure. Also, introduce new download_base64.py controller in the project structure.
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
4c7ee65d34
commit
571a80b725
40
README.md
40
README.md
@ -222,7 +222,43 @@ Obtient les informations détaillées d'un fichier ou dossier spécifique.
|
|||||||
curl http://localhost:8000/info/Documents/rapport.pdf
|
curl http://localhost:8000/info/Documents/rapport.pdf
|
||||||
```
|
```
|
||||||
|
|
||||||
### 5. Upload un fichier
|
### 5. Télécharger un fichier en Base64
|
||||||
|
```
|
||||||
|
GET /download-base64/{path}
|
||||||
|
```
|
||||||
|
Télécharge un fichier depuis Nextcloud et le retourne encodé en base64.
|
||||||
|
|
||||||
|
**Paramètres :**
|
||||||
|
- `path` : Chemin complet du fichier (ex: `/Documents/rapport.pdf`)
|
||||||
|
|
||||||
|
**Exemples :**
|
||||||
|
```powershell
|
||||||
|
# Télécharger un fichier
|
||||||
|
curl http://localhost:8000/download-base64/Documents/rapport.pdf
|
||||||
|
|
||||||
|
# Avec PowerShell
|
||||||
|
$response = Invoke-RestMethod -Uri "http://localhost:8000/download-base64/Documents/rapport.pdf"
|
||||||
|
$fileBase64 = $response.file.file_base64
|
||||||
|
# Décoder le fichier
|
||||||
|
$bytes = [Convert]::FromBase64String($fileBase64)
|
||||||
|
[System.IO.File]::WriteAllBytes("C:\chemin\destination\rapport.pdf", $bytes)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Réponse exemple :**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"file": {
|
||||||
|
"name": "rapport.pdf",
|
||||||
|
"path": "/Documents/rapport.pdf",
|
||||||
|
"size": 1024567,
|
||||||
|
"content_type": "application/pdf",
|
||||||
|
"file_base64": "JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBl..."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. Upload un fichier
|
||||||
```
|
```
|
||||||
POST /upload
|
POST /upload
|
||||||
```
|
```
|
||||||
@ -291,7 +327,9 @@ PyNextcloud/
|
|||||||
│ ├── health.py # Health check
|
│ ├── health.py # Health check
|
||||||
│ ├── list.py # Lister les fichiers/dossiers
|
│ ├── list.py # Lister les fichiers/dossiers
|
||||||
│ ├── info.py # Informations d'un fichier/dossier
|
│ ├── info.py # Informations d'un fichier/dossier
|
||||||
|
│ ├── download_base64.py # Télécharger un fichier en base64
|
||||||
│ ├── upload.py # Upload de fichiers
|
│ ├── upload.py # Upload de fichiers
|
||||||
|
│ ├── upload_base64.py # Upload de fichiers en base64
|
||||||
│ └── debug.py # Routes de débogage
|
│ └── debug.py # Routes de débogage
|
||||||
├── requirements.txt # Dépendances Python
|
├── requirements.txt # Dépendances Python
|
||||||
├── env.example # Exemple de fichier de configuration
|
├── env.example # Exemple de fichier de configuration
|
||||||
|
|||||||
87
controllers/download_base64.py
Normal file
87
controllers/download_base64.py
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
"""Contrôleur pour télécharger des fichiers depuis Nextcloud en base64"""
|
||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from nc_py_api import NextcloudException
|
||||||
|
from utils import get_nextcloud_client, format_file_info
|
||||||
|
import base64
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/download-base64/{path:path}")
|
||||||
|
async def download_file_base64(path: str):
|
||||||
|
"""
|
||||||
|
Télécharge un fichier depuis Nextcloud et le retourne encodé en base64
|
||||||
|
|
||||||
|
Args:
|
||||||
|
path: Chemin complet du fichier (ex: /Documents/rapport.pdf)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON contenant le fichier encodé en base64 et ses métadonnées
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Créer le client Nextcloud
|
||||||
|
nc = get_nextcloud_client()
|
||||||
|
|
||||||
|
# Normaliser le chemin du fichier
|
||||||
|
if not path.startswith("/"):
|
||||||
|
file_path = "/" + path.strip("/")
|
||||||
|
else:
|
||||||
|
file_path = path.strip()
|
||||||
|
|
||||||
|
# Vérifier que le fichier existe et récupérer ses informations
|
||||||
|
try:
|
||||||
|
file_info = nc.files.by_path(file_path)
|
||||||
|
except NextcloudException as e:
|
||||||
|
if "404" in str(e):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f"Le fichier '{file_path}' n'existe pas"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=500,
|
||||||
|
detail=f"Erreur Nextcloud: {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Vérifier que c'est bien un fichier et non un dossier
|
||||||
|
if file_info.is_dir:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=f"'{file_path}' est un dossier, pas un fichier"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Télécharger le contenu du fichier
|
||||||
|
try:
|
||||||
|
file_content = nc.files.download(file_path)
|
||||||
|
except NextcloudException as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=500,
|
||||||
|
detail=f"Erreur lors du téléchargement : {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Encoder le contenu en base64
|
||||||
|
file_base64 = base64.b64encode(file_content).decode('utf-8')
|
||||||
|
|
||||||
|
# Préparer la réponse avec les métadonnées
|
||||||
|
result = {
|
||||||
|
"success": True,
|
||||||
|
"file": {
|
||||||
|
"name": file_info.name,
|
||||||
|
"path": file_path,
|
||||||
|
"size": len(file_content),
|
||||||
|
"content_type": getattr(file_info.info, 'mimetype', 'application/octet-stream') if hasattr(file_info, 'info') else 'application/octet-stream',
|
||||||
|
"file_base64": file_base64
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSONResponse(content=result)
|
||||||
|
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=500,
|
||||||
|
detail=f"Erreur interne: {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user