From 1a9a19e1cb858c78a8932a044581e8487cb226a0 Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Sun, 21 Jan 2024 06:34:39 +0800 Subject: [PATCH] =?UTF-8?q?feat=E2=9C=A8:=20webui=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E6=A0=91=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4/=E6=96=B0?= =?UTF-8?q?=E5=A2=9E/=E9=87=8D=E5=91=BD=E5=90=8D=E6=96=87=E4=BB=B6/?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/web_ui/api/tabs/system/__init__.py | 65 +++++++++++++++++++++- plugins/web_ui/api/tabs/system/model.py | 32 +++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/plugins/web_ui/api/tabs/system/__init__.py b/plugins/web_ui/api/tabs/system/__init__.py index ef80d1bc..36755ff4 100644 --- a/plugins/web_ui/api/tabs/system/__init__.py +++ b/plugins/web_ui/api/tabs/system/__init__.py @@ -1,4 +1,5 @@ import os +import shutil from pathlib import Path from typing import List, Optional @@ -6,7 +7,7 @@ from fastapi import APIRouter from ....base_model import Result from ....utils import authentication, get_system_disk -from .model import DirFile +from .model import AddFile, DeleteFile, DirFile, RenameFile router = APIRouter(prefix="/system") @@ -23,4 +24,64 @@ async def _(path: Optional[str] = None) -> Result: @router.get("/get_resources_size", dependencies=[authentication()], description="获取文件列表") async def _(full_path: Optional[str] = None) -> Result: - return Result.ok(await get_system_disk(full_path)) \ No newline at end of file + return Result.ok(await get_system_disk(full_path)) + + +@router.post("/delete_file", dependencies=[authentication()], description="删除文件") +async def _(param: DeleteFile) -> Result: + path = Path(param.full_path) + if not path or not path.exists(): + return Result.warning_("文件不存在...") + try: + path.unlink() + return Result.ok('删除成功!') + except Exception as e: + return Result.warning_('删除失败: ' + str(e)) + +@router.post("/delete_folder", dependencies=[authentication()], description="删除文件夹") +async def _(param: DeleteFile) -> Result: + path = Path(param.full_path) + if not path or not path.exists() or path.is_file(): + return Result.warning_("文件夹不存在...") + try: + shutil.rmtree(path.absolute()) + return Result.ok('删除成功!') + except Exception as e: + return Result.warning_('删除失败: ' + str(e)) + + +@router.post("/rename_file", dependencies=[authentication()], description="重命名文件") +async def _(param: RenameFile) -> Result: + path = (Path(param.parent) / param.old_name) if param.parent else Path(param.old_name) + if not path or not path.exists(): + return Result.warning_("文件不存在...") + try: + path.rename(path.parent / param.name) + return Result.ok('重命名成功!') + except Exception as e: + return Result.warning_('重命名失败: ' + str(e)) + + +@router.post("/rename_folder", dependencies=[authentication()], description="重命名文件夹") +async def _(param: RenameFile) -> Result: + path = (Path(param.parent) / param.old_name) if param.parent else Path(param.old_name) + if not path or not path.exists() or path.is_file(): + return Result.warning_("文件夹不存在...") + try: + new_path = path.parent / param.name + shutil.move(path.absolute(), new_path.absolute()) + return Result.ok('重命名成功!') + except Exception as e: + return Result.warning_('重命名失败: ' + str(e)) + + +@router.post("/add_file", dependencies=[authentication()], description="新建文件") +async def _(param: AddFile) -> Result: + path = (Path(param.parent) / param.name) if param.parent else Path(param.name) + if path.exists(): + return Result.warning_("文件已存在...") + try: + path.open('w') + return Result.ok('新建文件成功!') + except Exception as e: + return Result.warning_('新建文件失败: ' + str(e)) \ No newline at end of file diff --git a/plugins/web_ui/api/tabs/system/model.py b/plugins/web_ui/api/tabs/system/model.py index 01d42218..af4f6209 100644 --- a/plugins/web_ui/api/tabs/system/model.py +++ b/plugins/web_ui/api/tabs/system/model.py @@ -19,3 +19,35 @@ class DirFile(BaseModel): """文件夹或文件名称""" parent: Optional[str] = None """父级""" + +class DeleteFile(BaseModel): + + """ + 删除文件 + """ + + full_path: str + """文件全路径""" + +class RenameFile(BaseModel): + + """ + 删除文件 + """ + parent: Optional[str] + """父路径""" + old_name: str + """旧名称""" + name: str + """新名称""" + + +class AddFile(BaseModel): + + """ + 新建文件 + """ + parent: Optional[str] + """父路径""" + name: str + """新名称"""