|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import io |
| 4 | +import os.path |
| 5 | +import zipfile |
| 6 | + |
| 7 | +from typing import Annotated |
| 8 | + |
| 9 | +from fastapi import APIRouter, Depends, File, UploadFile |
| 10 | +from fastapi.params import Query |
| 11 | +from starlette.responses import StreamingResponse |
| 12 | + |
| 13 | +from backend.common.exception import errors |
| 14 | +from backend.common.response.response_schema import ResponseModel, response_base |
| 15 | +from backend.common.security.permission import RequestPermission |
| 16 | +from backend.common.security.rbac import DependsRBAC |
| 17 | +from backend.core.path_conf import PLUGIN_DIR |
| 18 | +from backend.plugin.tools import install_requirements_async |
| 19 | + |
| 20 | +router = APIRouter() |
| 21 | + |
| 22 | + |
| 23 | +@router.post( |
| 24 | + '/install', |
| 25 | + summary='安装插件', |
| 26 | + description='需使用插件 zip 压缩包进行安装', |
| 27 | + dependencies=[ |
| 28 | + Depends(RequestPermission('sys:plugin:install')), |
| 29 | + DependsRBAC, |
| 30 | + ], |
| 31 | +) |
| 32 | +async def install_plugin(file: Annotated[UploadFile, File()]) -> ResponseModel: |
| 33 | + contents = await file.read() |
| 34 | + file_bytes = io.BytesIO(contents) |
| 35 | + if not zipfile.is_zipfile(file_bytes): |
| 36 | + raise errors.ForbiddenError(msg='插件压缩包格式非法') |
| 37 | + with zipfile.ZipFile(file_bytes) as zf: |
| 38 | + # 校验压缩包 |
| 39 | + plugin_dir_in_zip = f'{file.filename[:-4]}/backend/plugin/' |
| 40 | + members_in_plugin_dir = [name for name in zf.namelist() if name.startswith(plugin_dir_in_zip)] |
| 41 | + if not members_in_plugin_dir: |
| 42 | + raise errors.ForbiddenError(msg='插件压缩包内容非法') |
| 43 | + plugin_name = members_in_plugin_dir[1].replace(plugin_dir_in_zip, '').replace('/', '') |
| 44 | + if ( |
| 45 | + len(members_in_plugin_dir) <= 3 |
| 46 | + or f'{plugin_dir_in_zip}{plugin_name}/plugin.toml' not in members_in_plugin_dir |
| 47 | + or f'{plugin_dir_in_zip}{plugin_name}/README.md' not in members_in_plugin_dir |
| 48 | + ): |
| 49 | + raise errors.ForbiddenError(msg='插件压缩包内缺少必要文件') |
| 50 | + |
| 51 | + # 插件是否可安装 |
| 52 | + full_plugin_path = os.path.join(PLUGIN_DIR, plugin_name) |
| 53 | + if os.path.exists(full_plugin_path): |
| 54 | + raise errors.ForbiddenError(msg='此插件已安装') |
| 55 | + os.makedirs(full_plugin_path) |
| 56 | + |
| 57 | + # 解压安装 |
| 58 | + members = [] |
| 59 | + for member in zf.infolist(): |
| 60 | + if member.filename.startswith(plugin_dir_in_zip): |
| 61 | + member.filename = member.filename.replace(plugin_dir_in_zip, '') |
| 62 | + if not member.filename: |
| 63 | + continue |
| 64 | + members.append(member) |
| 65 | + zf.extractall(PLUGIN_DIR, members) |
| 66 | + if os.path.exists(os.path.join(full_plugin_path, 'requirements.txt')): |
| 67 | + await install_requirements_async(False) |
| 68 | + |
| 69 | + return response_base.success() |
| 70 | + |
| 71 | + |
| 72 | +@router.post( |
| 73 | + '/zip', |
| 74 | + summary='打包插件', |
| 75 | + dependencies=[ |
| 76 | + Depends(RequestPermission('sys:plugin:zip')), |
| 77 | + DependsRBAC, |
| 78 | + ], |
| 79 | +) |
| 80 | +async def build_plugin_zip(plugin: Annotated[str, Query()]): |
| 81 | + plugin_dir = os.path.join(PLUGIN_DIR, plugin) |
| 82 | + if not os.path.exists(plugin_dir): |
| 83 | + raise errors.ForbiddenError(msg='插件不存在') |
| 84 | + bio = io.BytesIO() |
| 85 | + with zipfile.ZipFile(bio, 'w') as zf: |
| 86 | + for root, dirs, files in os.walk(plugin_dir): |
| 87 | + dirs[:] = [d for d in dirs if d != '__pycache__'] |
| 88 | + for file in files: |
| 89 | + file_path = os.path.join(root, file) |
| 90 | + arcname = os.path.relpath(file_path, start=plugin_dir) |
| 91 | + zf.write(file_path, arcname) |
| 92 | + bio.seek(0) |
| 93 | + return StreamingResponse( |
| 94 | + bio, |
| 95 | + media_type='application/x-zip-compressed', |
| 96 | + headers={'Content-Disposition': f'attachment; filename={plugin}.zip'}, |
| 97 | + ) |
0 commit comments