From 005588794e346e711b2b6c2ff7c00ef031ea1ea0 Mon Sep 17 00:00:00 2001 From: Michael Woods Date: Fri, 26 Dec 2025 00:05:33 -0500 Subject: [PATCH] Downloader text worked. --- packetserver/http/routers/objects.py | 46 ++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/packetserver/http/routers/objects.py b/packetserver/http/routers/objects.py index a79dd9a..b29af46 100644 --- a/packetserver/http/routers/objects.py +++ b/packetserver/http/routers/objects.py @@ -1,5 +1,5 @@ from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form -from fastapi.responses import JSONResponse +from fastapi.responses import PlainTextResponse, Response from typing import List, Optional from datetime import datetime from uuid import UUID @@ -380,4 +380,46 @@ async def delete_object( logging.error(f"Object delete failed for {username} on {uuid}: {e}\n{traceback.format_exc()}") raise HTTPException(status_code=500, detail="Failed to delete object") - return None \ No newline at end of file + return None + +@router.get("/objects/{uuid}/text", response_class=PlainTextResponse) +async def get_object_text( + uuid: UUID, + db: DbDependency, + current_user: HttpUser = Depends(get_current_http_user) +): + username = current_user.username + + try: + with db.transaction() as conn: + root = conn.root() + + obj = Object.get_object_by_uuid(uuid, root) + if not obj: + raise HTTPException(status_code=404, detail="Object not found") + + # Authorization check + if obj.private: + user = User.get_user_by_username(username, root) + if not user or user.uuid != obj.owner: + raise HTTPException(status_code=403, detail="Not authorized to access this private object") + + # Only allow text objects + if obj.binary: + raise HTTPException( + status_code=400, + detail="This endpoint is for text objects only. Use /download or /binary for binary content." + ) + + # Safe to return as str since binary=False guarantees valid UTF-8 + content = obj.data # will be str + + logging.info(f"User {username} downloaded text object {uuid} ({obj.name})") + + except HTTPException: + raise + except Exception as e: + logging.error(f"Text download failed for {username} on {uuid}: {e}\n{traceback.format_exc()}") + raise HTTPException(status_code=500, detail="Failed to retrieve text object") + + return PlainTextResponse(content=content, media_type="text/plain; charset=utf-8") \ No newline at end of file