Skip to content

Commit 69a9b90

Browse files
authored
Optimize the zip plug-in file name parsing (#687)
1 parent b96402e commit 69a9b90

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

backend/app/admin/service/plugin_service.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io
44
import json
55
import os
6+
import re
67
import shutil
78
import zipfile
89

@@ -59,17 +60,18 @@ async def install_zip(*, file: UploadFile) -> None:
5960
with zipfile.ZipFile(file_bytes) as zf:
6061
# 校验压缩包
6162
plugin_namelist = zf.namelist()
62-
plugin_name = plugin_namelist[0].split('/')[0]
63-
if not plugin_namelist or plugin_name not in file.filename:
63+
zip_plugin_dir = plugin_namelist[0].split('/')[0]
64+
if not plugin_namelist:
6465
raise errors.RequestError(msg='插件压缩包内容非法')
6566
if (
6667
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
68+
or f'{zip_plugin_dir}/plugin.toml' not in plugin_namelist
69+
or f'{zip_plugin_dir}/README.md' not in plugin_namelist
6970
):
7071
raise errors.RequestError(msg='插件压缩包内缺少必要文件')
7172

7273
# 插件是否可安装
74+
plugin_name = re.match(r'^([a-zA-Z0-9_]+)', file.filename.split('.')[0].strip()).group()
7375
full_plugin_path = os.path.join(PLUGIN_DIR, plugin_name)
7476
if os.path.exists(full_plugin_path):
7577
raise errors.ConflictError(msg='此插件已安装')
@@ -79,14 +81,14 @@ async def install_zip(*, file: UploadFile) -> None:
7981
# 解压(安装)
8082
members = []
8183
for member in zf.infolist():
82-
if member.filename.startswith(plugin_name):
83-
new_filename = member.filename.replace(plugin_name, '')
84+
if member.filename.startswith(zip_plugin_dir):
85+
new_filename = member.filename.replace(zip_plugin_dir, '')
8486
if new_filename:
8587
member.filename = new_filename
8688
members.append(member)
87-
zf.extractall(os.path.join(PLUGIN_DIR, plugin_name), members)
89+
zf.extractall(full_plugin_path, members)
8890

89-
await install_requirements_async(plugin_name)
91+
await install_requirements_async(zip_plugin_dir)
9092
await redis_client.set(f'{settings.PLUGIN_REDIS_PREFIX}:changed', 'ture')
9193

9294
@staticmethod

0 commit comments

Comments
 (0)