55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
"""Fonctions utilitaires pour l'application"""
|
|
from typing import Dict, Any
|
|
from fastapi import HTTPException
|
|
from nc_py_api import Nextcloud
|
|
from config import settings
|
|
|
|
|
|
def get_nextcloud_client() -> Nextcloud:
|
|
"""Crée et retourne un client Nextcloud"""
|
|
try:
|
|
nc = Nextcloud(
|
|
nextcloud_url=settings.nextcloud_url,
|
|
nc_auth_user=settings.nextcloud_username,
|
|
nc_auth_pass=settings.nextcloud_password
|
|
)
|
|
return nc
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail=f"Erreur de connexion à Nextcloud: {str(e)}"
|
|
)
|
|
|
|
|
|
def format_file_info(file_info) -> Dict[str, Any]:
|
|
"""Formate les informations d'un fichier/dossier"""
|
|
result = {
|
|
"name": getattr(file_info, 'name', ''),
|
|
"path": getattr(file_info, 'user_path', getattr(file_info, 'path', '')),
|
|
"is_dir": getattr(file_info, 'is_dir', False),
|
|
}
|
|
|
|
# Attributs optionnels
|
|
if hasattr(file_info, 'info'):
|
|
info = file_info.info
|
|
result["size"] = getattr(info, 'size', 0)
|
|
result["content_type"] = getattr(info, 'mimetype', None)
|
|
if hasattr(info, 'last_modified'):
|
|
result["last_modified"] = info.last_modified.isoformat() if info.last_modified else None
|
|
else:
|
|
result["last_modified"] = None
|
|
result["etag"] = getattr(info, 'etag', None)
|
|
else:
|
|
result["size"] = getattr(file_info, 'size', 0)
|
|
result["content_type"] = getattr(file_info, 'mimetype', getattr(file_info, 'content_type', None))
|
|
if hasattr(file_info, 'last_modified') and file_info.last_modified:
|
|
result["last_modified"] = file_info.last_modified.isoformat()
|
|
else:
|
|
result["last_modified"] = None
|
|
result["etag"] = getattr(file_info, 'etag', None)
|
|
|
|
result["file_id"] = getattr(file_info, 'file_id', None)
|
|
|
|
return result
|
|
|