Skip to content

Commit 746af37

Browse files
haimaitpiexlMax(奇淼
authored andcommitted
解决打包插件解压报错的问题 (#1630)
#1628
1 parent 5ca47a9 commit 746af37

File tree

2 files changed

+23
-66
lines changed

2 files changed

+23
-66
lines changed

server/api/v1/system/sys_auto_code.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ func (autoApi *AutoCodeApi) InstallPlugin(c *gin.Context) {
304304
// @Router /autoCode/pubPlug [get]
305305
func (autoApi *AutoCodeApi) PubPlug(c *gin.Context) {
306306
plugName := c.Query("plugName")
307-
snake := strings.ToLower(plugName)
308-
zipPath, err := autoCodeService.PubPlug(snake)
307+
zipPath, err := autoCodeService.PubPlug(plugName)
309308
if err != nil {
310309
global.GVA_LOG.Error("打包失败!", zap.Error(err))
311310
response.FailWithMessage("打包失败"+err.Error(), c)

server/service/system/sys_auto_code.go

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -820,11 +820,11 @@ func (autoCodeService *AutoCodeService) PubPlug(plugName string) (zipPath string
820820
// 创建一个新的zip文件
821821

822822
// 判断目录是否存在
823-
webInfo, err := os.Stat(webPath)
823+
_, err = os.Stat(webPath)
824824
if err != nil {
825825
return "", errors.New("web路径不存在")
826826
}
827-
serverInfo, err := os.Stat(serverPath)
827+
_, err = os.Stat(serverPath)
828828
if err != nil {
829829
return "", errors.New("server路径不存在")
830830
}
@@ -842,72 +842,29 @@ func (autoCodeService *AutoCodeService) PubPlug(plugName string) (zipPath string
842842
zipWriter := zip.NewWriter(zipFile)
843843
defer zipWriter.Close()
844844

845-
// 创建一个新的文件头
846-
webHeader, err := zip.FileInfoHeader(webInfo)
845+
webHeaderName := filepath.Join(plugName, "web", "plugin", plugName)
846+
err = autoCodeService.doZip(zipWriter, webPath, webHeaderName)
847847
if err != nil {
848848
return
849849
}
850-
851-
// 创建一个新的文件头
852-
serverHeader, err := zip.FileInfoHeader(serverInfo)
850+
serverHeaderName := filepath.Join(plugName, "server", "plugin", plugName)
851+
err = autoCodeService.doZip(zipWriter, serverPath, serverHeaderName)
853852
if err != nil {
854853
return
855854
}
855+
return filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fileName), nil
856+
}
856857

857-
webHeader.Name = filepath.Join(plugName, "web", "plugin")
858-
serverHeader.Name = filepath.Join(plugName, "server", "plugin")
859-
860-
// 将文件添加到zip归档中
861-
_, err = zipWriter.CreateHeader(serverHeader)
862-
_, err = zipWriter.CreateHeader(webHeader)
863-
864-
// 遍历webPath目录并将所有非隐藏文件添加到zip归档中
865-
err = filepath.Walk(webPath, func(path string, info os.FileInfo, err error) error {
866-
if err != nil {
867-
return err
868-
}
869-
870-
// 跳过隐藏文件
871-
if strings.HasPrefix(info.Name(), ".") {
872-
return nil
873-
}
874-
875-
// 创建一个新的文件头
876-
header, err := zip.FileInfoHeader(info)
877-
if err != nil {
878-
return err
879-
}
880-
881-
// 将文件头的名称设置为文件的相对路径
882-
rel, _ := filepath.Rel(webPath, path)
883-
header.Name = filepath.Join(plugName, "web", "plugin", plugName, rel)
884-
885-
// 将文件添加到zip归档中
886-
writer, err := zipWriter.CreateHeader(header)
887-
if err != nil {
888-
return err
889-
}
890-
891-
if info.IsDir() {
892-
return nil
893-
}
858+
/*
859+
*
894860
895-
// 打开文件并将其内容复制到zip归档中
896-
file, err := os.Open(path)
897-
if err != nil {
898-
return err
899-
}
900-
defer file.Close()
901-
_, err = io.Copy(writer, file)
902-
if err != nil {
903-
return err
904-
}
905-
return nil
906-
})
907-
if err != nil {
908-
return
909-
}
861+
zipWriter zip写入器
862+
serverPath 存储的路径
863+
headerName 写有zip的路径
910864
865+
*
866+
*/
867+
func (autoCodeService *AutoCodeService) doZip(zipWriter *zip.Writer, serverPath, headerName string) (err error) {
911868
// 遍历serverPath目录并将所有非隐藏文件添加到zip归档中
912869
err = filepath.Walk(serverPath, func(path string, info os.FileInfo, err error) error {
913870
if err != nil {
@@ -927,7 +884,11 @@ func (autoCodeService *AutoCodeService) PubPlug(plugName string) (zipPath string
927884

928885
// 将文件头的名称设置为文件的相对路径
929886
rel, _ := filepath.Rel(serverPath, path)
930-
header.Name = filepath.Join(plugName, "server", "plugin", plugName, rel)
887+
header.Name = filepath.Join(headerName, rel)
888+
// 目录需要拼上一个 "/" ,否则会出现一个和目录一样的文件在压缩包中
889+
if info.IsDir() {
890+
header.Name += "/"
891+
}
931892
// 将文件添加到zip归档中
932893
writer, err := zipWriter.CreateHeader(header)
933894
if err != nil {
@@ -951,8 +912,5 @@ func (autoCodeService *AutoCodeService) PubPlug(plugName string) (zipPath string
951912

952913
return nil
953914
})
954-
if err != nil {
955-
return
956-
}
957-
return filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fileName), nil
915+
return err
958916
}

0 commit comments

Comments
 (0)