Object upload works now. Added a json text object uploader.
This commit is contained in:
@@ -127,3 +127,62 @@ async def upload_object(
|
|||||||
created_at=new_object.created_at,
|
created_at=new_object.created_at,
|
||||||
modified_at=new_object.modified_at
|
modified_at=new_object.modified_at
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class TextObjectCreate(BaseModel):
|
||||||
|
text: str
|
||||||
|
name: Optional[str] = None
|
||||||
|
private: bool = True
|
||||||
|
|
||||||
|
@router.post("/objects/text", response_model=ObjectSummary)
|
||||||
|
async def create_text_object(
|
||||||
|
payload: TextObjectCreate,
|
||||||
|
db: DbDependency,
|
||||||
|
current_user: HttpUser = Depends(get_current_http_user)
|
||||||
|
):
|
||||||
|
username = current_user.username
|
||||||
|
|
||||||
|
if not payload.text:
|
||||||
|
raise HTTPException(status_code=400, detail="Text content cannot be empty")
|
||||||
|
|
||||||
|
obj_name = (payload.name or "text_object.txt").strip()
|
||||||
|
if len(obj_name) > 300:
|
||||||
|
raise HTTPException(status_code=400, detail="Object name too long (max 300 chars)")
|
||||||
|
if not obj_name:
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid object name")
|
||||||
|
|
||||||
|
try:
|
||||||
|
with db.transaction() as conn:
|
||||||
|
root = conn.root()
|
||||||
|
user = User.get_user_by_username(username, root)
|
||||||
|
if not user:
|
||||||
|
raise HTTPException(status_code=404, detail="User not found")
|
||||||
|
|
||||||
|
# Create object with str data → forces binary=False
|
||||||
|
new_object = Object(name=obj_name, data=payload.text)
|
||||||
|
new_object.private = payload.private
|
||||||
|
|
||||||
|
obj_uuid = new_object.write_new(db, username=username)
|
||||||
|
|
||||||
|
logging.info(f"User {username} created text object {obj_uuid} ({obj_name}, {len(payload.text)} chars)")
|
||||||
|
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Text object creation failed for {username}: {e}\n{format_exc()}")
|
||||||
|
raise HTTPException(status_code=500, detail="Failed to create text object")
|
||||||
|
|
||||||
|
# Build summary
|
||||||
|
content_type, _ = mimetypes.guess_type(new_object.name)
|
||||||
|
if content_type is None:
|
||||||
|
content_type = "text/plain" # always text here
|
||||||
|
|
||||||
|
return ObjectSummary(
|
||||||
|
uuid=obj_uuid,
|
||||||
|
name=new_object.name,
|
||||||
|
binary=new_object.binary, # should be False
|
||||||
|
size=new_object.size,
|
||||||
|
content_type=content_type,
|
||||||
|
private=new_object.private,
|
||||||
|
created_at=new_object.created_at,
|
||||||
|
modified_at=new_object.modified_at
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user