3
3
import io
4
4
import json
5
5
import os
6
+ import re
6
7
import shutil
7
8
import zipfile
8
9
@@ -59,17 +60,18 @@ async def install_zip(*, file: UploadFile) -> None:
59
60
with zipfile .ZipFile (file_bytes ) as zf :
60
61
# 校验压缩包
61
62
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 :
64
65
raise errors .RequestError (msg = '插件压缩包内容非法' )
65
66
if (
66
67
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
69
70
):
70
71
raise errors .RequestError (msg = '插件压缩包内缺少必要文件' )
71
72
72
73
# 插件是否可安装
74
+ plugin_name = re .match (r'^([a-zA-Z0-9_]+)' , file .filename .split ('.' )[0 ].strip ()).group ()
73
75
full_plugin_path = os .path .join (PLUGIN_DIR , plugin_name )
74
76
if os .path .exists (full_plugin_path ):
75
77
raise errors .ConflictError (msg = '此插件已安装' )
@@ -79,14 +81,14 @@ async def install_zip(*, file: UploadFile) -> None:
79
81
# 解压(安装)
80
82
members = []
81
83
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 , '' )
84
86
if new_filename :
85
87
member .filename = new_filename
86
88
members .append (member )
87
- zf .extractall (os . path . join ( PLUGIN_DIR , plugin_name ) , members )
89
+ zf .extractall (full_plugin_path , members )
88
90
89
- await install_requirements_async (plugin_name )
91
+ await install_requirements_async (zip_plugin_dir )
90
92
await redis_client .set (f'{ settings .PLUGIN_REDIS_PREFIX } :changed' , 'ture' )
91
93
92
94
@staticmethod
0 commit comments