Skip to content

Commit

Permalink
Feat:POST /batch-workspaces - Import list of workspaces (limit:5) eac…
Browse files Browse the repository at this point in the history
…h as an ocrd zip. Returns a list of associated with the uploaded workspace.
  • Loading branch information
Faizan-hub committed Dec 6, 2024
1 parent 18b3675 commit 5c36436
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/server/operandi_server/routers/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from shutil import rmtree
from typing import List

from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status, UploadFile
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status, UploadFile, Form
from fastapi.responses import FileResponse
from fastapi.security import HTTPBasic, HTTPBasicCredentials

Expand Down Expand Up @@ -46,6 +46,12 @@ def __init__(self):
summary="Import workspace as an ocrd zip. Returns a `resource_id` associated with the uploaded workspace.",
response_model=WorkspaceRsrc, response_model_exclude_unset=True, response_model_exclude_none=True
)
self.router.add_api_route(
path="/batch-workspaces",
endpoint=self.upload_batch_workspaces, methods=["POST"], status_code=status.HTTP_201_CREATED,
summary="Import list of workspaces (limit:5) each as an ocrd zip. Returns a list of `resource_id` associated with the uploaded workspace.",
response_model=List[WorkspaceRsrc], response_model_exclude_unset=True, response_model_exclude_none=True
)
self.router.add_api_route(
path="/workspace",
endpoint=self.list_workspaces, methods=["GET"], status_code=status.HTTP_200_OK,
Expand Down Expand Up @@ -269,3 +275,67 @@ async def remove_file_group_from_workspace(
)
db_workspace = await db_update_workspace(find_workspace_id=workspace_id, file_groups=remaining_file_groups)
return WorkspaceRsrc.from_db_workspace(db_workspace)

async def upload_batch_workspaces(
self,
files: List[UploadFile] = Form(...),
details: str = Form("Batch upload of workspaces"),
auth: HTTPBasicCredentials = Depends(HTTPBasic()),
) -> List[WorkspaceRsrc]:
"""
Curl equivalent:
`curl -X POST SERVER_ADDR/batch-workspaces -F [email protected] -F [email protected] ...`
"""
py_user_action = await self.user_authenticator.user_login(auth)

if len(files) > 5:
message = "Batch upload exceeds the limit of 5 workspaces"
self.logger.error(message)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=message,
)

uploaded_workspaces = []
for file in files:
ws_id, ws_dir = create_resource_dir(SERVER_WORKSPACES_ROUTER)
bag_dest = f"{ws_dir}.zip"

try:
await receive_resource(file=file, resource_dst=bag_dest)
rmtree(ws_dir, ignore_errors=True)
validate_bag_with_handling(self.logger, bag_dst=bag_dest)
bag_info = extract_bag_info_with_handling(self.logger, bag_dst=bag_dest, ws_dir=ws_dir)
Path(bag_dest).unlink()

pages_amount = extract_pages_with_handling(self.logger, bag_info, ws_dir)
file_groups = extract_file_groups_with_handling(self.logger, bag_info, ws_dir)

db_workspace = await db_create_workspace(
user_id=py_user_action.user_id,
workspace_id=ws_id,
workspace_dir=ws_dir,
pages_amount=pages_amount,
file_groups=file_groups,
bag_info=bag_info,
state=StateWorkspace.READY,
details=details,
)

await db_increase_processing_stats_with_handling(
self.logger,
find_user_id=py_user_action.user_id,
pages_uploaded=pages_amount,
)

uploaded_workspaces.append(WorkspaceRsrc.from_db_workspace(db_workspace))

except Exception as error:
self.logger.error(f"Failed to process workspace {file.filename}, error: {error}")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Error processing file {file.filename}: {str(error)}",
)

return uploaded_workspaces

0 comments on commit 5c36436

Please sign in to comment.