Skip to content

Commit 19ef52d

Browse files
authored
Optimize install and build of plugin zip (#636)
* Optimiz install and build of plugin zip * fix import
1 parent 39c3339 commit 19ef52d

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

backend/app/admin/service/plugin_service.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,14 @@ async def install_zip(*, file: UploadFile) -> None:
5858
raise errors.ForbiddenError(msg='插件压缩包格式非法')
5959
with zipfile.ZipFile(file_bytes) as zf:
6060
# 校验压缩包
61-
plugin_dir = file.filename[:-4]
62-
members_in_plugin_dir = [name for name in zf.namelist() if name.startswith(plugin_dir)]
63-
if not members_in_plugin_dir:
61+
plugin_namelist = zf.namelist()
62+
plugin_name = plugin_namelist[0].split('/')[0]
63+
if not plugin_namelist or plugin_name not in file.filename:
6464
raise errors.ForbiddenError(msg='插件压缩包内容非法')
65-
plugin_name = (
66-
members_in_plugin_dir[0]
67-
.replace('/', '')
68-
.replace('-master', '')
69-
.replace('-main', '')
70-
.replace('-dev', '')
71-
)
7265
if (
73-
len(members_in_plugin_dir) <= 3
74-
or f'{plugin_dir}/plugin.toml' not in members_in_plugin_dir
75-
or f'{plugin_dir}/README.md' not in members_in_plugin_dir
66+
len(plugin_namelist) <= 3
67+
or f'{plugin_name}/plugin.toml' not in plugin_namelist
68+
or f'{plugin_name}/README.md' not in plugin_namelist
7669
):
7770
raise errors.ForbiddenError(msg='插件压缩包内缺少必要文件')
7871

@@ -86,8 +79,8 @@ async def install_zip(*, file: UploadFile) -> None:
8679
# 解压(安装)
8780
members = []
8881
for member in zf.infolist():
89-
if member.filename.startswith(plugin_dir):
90-
new_filename = member.filename.replace(plugin_dir, '')
82+
if member.filename.startswith(plugin_name):
83+
new_filename = member.filename.replace(plugin_name, '')
9184
if new_filename:
9285
member.filename = new_filename
9386
members.append(member)
@@ -182,7 +175,7 @@ async def build(*, plugin: str) -> io.BytesIO:
182175
for file in files:
183176
file_path = os.path.join(root, file)
184177
arcname = os.path.relpath(file_path, start=plugin_dir)
185-
zf.write(file_path, arcname)
178+
zf.write(file_path, os.path.join(plugin, arcname))
186179

187180
bio.seek(0)
188181
return bio

0 commit comments

Comments
 (0)