itcloud/backend/src/app/api/v1/batch.py

142 lines
3.5 KiB
Python
Raw Normal View History

2025-12-30 23:18:13 +01:00
"""Batch operations API routes."""
from fastapi import APIRouter, status
from fastapi.responses import Response
from app.api.dependencies import CurrentUser, DatabaseSession, S3ClientDep
from app.api.schemas import (
BatchDeleteRequest,
BatchDeleteResponse,
BatchDownloadRequest,
BatchMoveRequest,
BatchMoveResponse,
)
from app.services.batch_operations_service import BatchOperationsService
router = APIRouter(prefix="/batch", tags=["batch"])
@router.post("/delete", response_model=BatchDeleteResponse)
async def batch_delete(
request: BatchDeleteRequest,
current_user: CurrentUser,
session: DatabaseSession,
s3_client: S3ClientDep,
):
"""
Delete multiple assets.
Args:
request: Batch delete request
current_user: Current authenticated user
session: Database session
s3_client: S3 client
Returns:
Deletion statistics
"""
batch_service = BatchOperationsService(session, s3_client)
result = await batch_service.delete_assets_batch(
user_id=current_user.id,
asset_ids=request.asset_ids,
)
return result
@router.post("/move", response_model=BatchMoveResponse)
async def batch_move(
request: BatchMoveRequest,
current_user: CurrentUser,
session: DatabaseSession,
s3_client: S3ClientDep,
):
"""
Move multiple assets to a folder.
Args:
request: Batch move request
current_user: Current authenticated user
session: Database session
s3_client: S3 client
Returns:
Move statistics
"""
batch_service = BatchOperationsService(session, s3_client)
result = await batch_service.move_assets_batch(
user_id=current_user.id,
asset_ids=request.asset_ids,
target_folder_id=request.folder_id,
)
return result
@router.post("/download")
async def batch_download(
request: BatchDownloadRequest,
current_user: CurrentUser,
session: DatabaseSession,
s3_client: S3ClientDep,
):
"""
Download multiple assets as a ZIP archive.
Args:
request: Batch download request
current_user: Current authenticated user
session: Database session
s3_client: S3 client
Returns:
ZIP file response
"""
batch_service = BatchOperationsService(session, s3_client)
zip_data, filename = await batch_service.download_assets_batch(
user_id=current_user.id,
asset_ids=request.asset_ids,
)
return Response(
content=zip_data,
media_type="application/zip",
headers={
"Content-Disposition": f'attachment; filename="{filename}"',
"Content-Length": str(len(zip_data)),
},
)
@router.get("/folders/{folder_id}/download")
async def download_folder(
folder_id: str,
current_user: CurrentUser,
session: DatabaseSession,
s3_client: S3ClientDep,
):
"""
Download all assets in a folder as a ZIP archive.
Args:
folder_id: Folder ID
current_user: Current authenticated user
session: Database session
s3_client: S3 client
Returns:
ZIP file response
"""
batch_service = BatchOperationsService(session, s3_client)
zip_data, filename = await batch_service.download_folder(
user_id=current_user.id,
folder_id=folder_id,
)
return Response(
content=zip_data,
media_type="application/zip",
headers={
"Content-Disposition": f'attachment; filename="{filename}"',
"Content-Length": str(len(zip_data)),
},
)