Skip to content

Commit

Permalink
feat: add task deletion endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin.zhang committed Apr 10, 2024
1 parent a880b2b commit d57434e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/controllers/v1/video.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import glob
import shutil

from fastapi import Request, Depends, Path, BackgroundTasks, UploadFile
from fastapi.params import File
from loguru import logger
Expand All @@ -9,7 +11,7 @@
from app.controllers.v1.base import new_router
from app.models.exception import HttpException
from app.models.schema import TaskVideoRequest, TaskQueryResponse, TaskResponse, TaskQueryRequest, \
BgmUploadResponse, BgmRetrieveResponse
BgmUploadResponse, BgmRetrieveResponse, TaskDeletionResponse
from app.services import task as tm
from app.services import state as sm
from app.utils import utils
Expand Down Expand Up @@ -75,6 +77,23 @@ def file_to_uri(file):
raise HttpException(task_id=task_id, status_code=404, message=f"{request_id}: task not found")


@router.delete("/tasks/{task_id}", response_model=TaskDeletionResponse, summary="Delete a generated short video task")
def create_video(request: Request, task_id: str = Path(..., description="Task ID")):
request_id = base.get_task_id(request)
task = sm.state.get_task(task_id)
if task:
tasks_dir = utils.task_dir()
current_task_dir = os.path.join(tasks_dir, task_id)
if os.path.exists(current_task_dir):
shutil.rmtree(current_task_dir)

sm.state.delete_task(task_id)
logger.success(f"video deleted: {utils.to_json(task)}")
return utils.get_response(200, task)

raise HttpException(task_id=task_id, status_code=404, message=f"{request_id}: task not found")


@router.get("/musics", response_model=BgmRetrieveResponse, summary="Retrieve local BGM files")
def get_bgm_list(request: Request):
suffix = "*.mp3"
Expand Down
20 changes: 20 additions & 0 deletions app/models/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ class Config:
}


class TaskDeletionResponse(BaseResponse):
class Config:
json_schema_extra = {
"example": {
"status": 200,
"message": "success",
"data": {
"state": 1,
"progress": 100,
"videos": [
"http://127.0.0.1:8080/tasks/6c85c8cc-a77a-42b9-bc30-947815aa0558/final-1.mp4"
],
"combined_videos": [
"http://127.0.0.1:8080/tasks/6c85c8cc-a77a-42b9-bc30-947815aa0558/combined-1.mp4"
]
}
},
}


class VideoScriptResponse(BaseResponse):
class Config:
json_schema_extra = {
Expand Down
7 changes: 7 additions & 0 deletions app/services/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def update_task(self, task_id: str, state: int = const.TASK_STATE_PROCESSING, pr
def get_task(self, task_id: str):
return self._tasks.get(task_id, None)

def delete_task(self, task_id: str):
if task_id in self._tasks:
del self._tasks[task_id]


# Redis state management
class RedisState(BaseState):
Expand Down Expand Up @@ -67,6 +71,9 @@ def get_task(self, task_id: str):
task = {key.decode('utf-8'): self._convert_to_original_type(value) for key, value in task_data.items()}
return task

def delete_task(self, task_id: str):
self._redis.delete(task_id)

@staticmethod
def _convert_to_original_type(value):
"""
Expand Down

0 comments on commit d57434e

Please sign in to comment.