diff --git a/app/api/scripts.py b/app/api/scripts.py index 4ac36a2..a81b637 100644 --- a/app/api/scripts.py +++ b/app/api/scripts.py @@ -205,3 +205,20 @@ async def reorder_user(user: UserReorderIn = Body(...)) -> OutBase: code=500, status="error", message=f"{type(e).__name__}: {str(e)}" ) return OutBase() + + +@router.post( + "/user/infrastructure", + summary="导入基建配置文件", + response_model=OutBase, + status_code=200, +) +async def import_infrastructure(user: UserSetIn = Body(...)) -> OutBase: + + try: + await Config.set_infrastructure(user.scriptId, user.userId, user.jsonFile) + except Exception as e: + return OutBase( + code=500, status="error", message=f"{type(e).__name__}: {str(e)}" + ) + return OutBase() diff --git a/app/core/config.py b/app/core/config.py index 75ed6eb..a7643bc 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -829,6 +829,26 @@ class AppConfig(GlobalConfig): await script_config.UserData.setOrder([uuid.UUID(_) for _ in index_list]) await self.ScriptConfig.save() + async def set_infrastructure( + self, script_id: str, user_id: str, jsonFile: str + ) -> None: + + logger.info(f"{script_id} - {user_id} 设置基建配置:{jsonFile}") + + json_path = Path(jsonFile) + + if not json_path.exists(): + raise FileNotFoundError(f"File not found: {json_path}") + + (Path.cwd() / f"data/{script_id}/{user_id}/Infrastructure").mkdir( + parents=True, exist_ok=True + ) + shutil.copy( + json_path, + Path.cwd() + / f"data/{script_id}/{user_id}/Infrastructure/infrastructure.json", + ) + async def add_plan( self, script: Literal["MaaPlan"] ) -> tuple[uuid.UUID, ConfigBase]: diff --git a/app/core/task_manager.py b/app/core/task_manager.py index 01ba921..ae8a9dc 100644 --- a/app/core/task_manager.py +++ b/app/core/task_manager.py @@ -230,12 +230,14 @@ class _TaskManager: logger.info(f"中止任务:{task_id}") - uid = uuid.UUID(task_id) - - if uid not in self.task_dict: - raise ValueError(f"The task {uid} is not running.") - - self.task_dict[uid].cancel() + if task_id == "ALL": + for task in self.task_dict.values(): + task.cancel() + else: + uid = uuid.UUID(task_id) + if uid not in self.task_dict: + raise ValueError(f"The task {uid} is not running.") + self.task_dict[uid].cancel() async def remove_task( self, task: asyncio.Task, mode: str, task_id: uuid.UUID diff --git a/app/models/schema.py b/app/models/schema.py index e208d41..a0385ed 100644 --- a/app/models/schema.py +++ b/app/models/schema.py @@ -514,6 +514,11 @@ class UserReorderIn(UserInBase): indexList: List[str] = Field(..., description="用户ID列表,按新顺序排列") +class UserSetIn(UserInBase): + userId: str = Field(..., description="用户ID") + jsonFile: str = Field(..., description="JSON文件路径, 用于导入自定义基建文件") + + class PlanCreateIn(BaseModel): type: Literal["MaaPlan"] diff --git a/main.py b/main.py index 9ba2681..66c97ae 100644 --- a/main.py +++ b/main.py @@ -55,7 +55,7 @@ def main(): @asynccontextmanager async def lifespan(app: FastAPI): - from app.core import Config, MainTimer + from app.core import Config, MainTimer, TaskManager from app.services import System await Config.init_config() @@ -67,6 +67,7 @@ def main(): yield + await TaskManager.stop_task("ALL") main_timer.cancel() try: await main_timer diff --git a/res/icons/AUTO_MAS.ico b/res/icons/AUTO_MAS.ico new file mode 100644 index 0000000..8339571 Binary files /dev/null and b/res/icons/AUTO_MAS.ico differ