diff --git a/backend/main.py b/backend/main.py index 4c7f681..c05abaf 100644 --- a/backend/main.py +++ b/backend/main.py @@ -411,7 +411,22 @@ async def api_file_raw(vault_name: str, path: str = Query(..., description="Rela if not file_path.exists() or not file_path.is_file(): raise HTTPException(status_code=404, detail=f"File not found: {path}") - raw = file_path.read_text(encoding="utf-8", errors="replace") + try: + raw = file_path.read_text(encoding="utf-8", errors="replace") + except PermissionError as e: + logger.error(f"Permission denied reading raw file {path}: {e}") + raise HTTPException(status_code=403, detail=f"Permission denied: cannot read file {path}") + except UnicodeDecodeError: + # Binary file - try to read as binary and decode with errors='replace' + try: + raw = file_path.read_bytes().decode("utf-8", errors="replace") + except Exception as e: + logger.error(f"Error reading binary raw file {path}: {e}") + raise HTTPException(status_code=500, detail=f"Cannot read file: {str(e)}") + except Exception as e: + logger.error(f"Unexpected error reading raw file {path}: {e}") + raise HTTPException(status_code=500, detail=f"Error reading file: {str(e)}") + return {"vault": vault_name, "path": path, "raw": raw}