Downloader text worked.
This commit is contained in:
@@ -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
|
||||
@@ -381,3 +381,45 @@ async def delete_object(
|
||||
raise HTTPException(status_code=500, detail="Failed to delete object")
|
||||
|
||||
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")
|
||||
Reference in New Issue
Block a user