Skip to content

Commit 6138d3e

Browse files
committed
Add build plugin zip service
1 parent 5b58068 commit 6138d3e

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

backend/app/admin/api/v1/sys/plugin.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from fastapi import APIRouter, Depends, File, UploadFile
1010
from fastapi.params import Query
11+
from starlette.responses import StreamingResponse
1112

1213
from backend.common.exception import errors
1314
from backend.common.response.response_schema import ResponseModel, response_base
@@ -68,5 +69,29 @@ async def install_plugin(file: Annotated[UploadFile, File()]) -> ResponseModel:
6869
return response_base.success()
6970

7071

71-
@router.post('/zip', summary='打包插件')
72-
async def build_plugin_zip(plugin: Annotated[str, Query()]): ...
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

Comments
 (0)