@@ -58,21 +58,14 @@ async def install_zip(*, file: UploadFile) -> None:
58
58
raise errors .ForbiddenError (msg = '插件压缩包格式非法' )
59
59
with zipfile .ZipFile (file_bytes ) as zf :
60
60
# 校验压缩包
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 :
64
64
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
- )
72
65
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
76
69
):
77
70
raise errors .ForbiddenError (msg = '插件压缩包内缺少必要文件' )
78
71
@@ -86,8 +79,8 @@ async def install_zip(*, file: UploadFile) -> None:
86
79
# 解压(安装)
87
80
members = []
88
81
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 , '' )
91
84
if new_filename :
92
85
member .filename = new_filename
93
86
members .append (member )
@@ -182,7 +175,7 @@ async def build(*, plugin: str) -> io.BytesIO:
182
175
for file in files :
183
176
file_path = os .path .join (root , file )
184
177
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 ) )
186
179
187
180
bio .seek (0 )
188
181
return bio
0 commit comments