From 0122b4c6ce253b21a0192815b2ab246afd5c759f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=98=AD?= <81747598+lan-yonghui@users.noreply.github.com> Date: Wed, 11 Sep 2024 16:20:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8E=8B=E7=BC=A9=E8=A7=A3=E5=8E=8B=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=E5=AD=97=E7=AC=A6=E9=97=AE=E9=A2=98=20(#6451)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/files/file_op.go | 3 +++ backend/utils/files/tar.go | 2 +- backend/utils/files/tar_gz.go | 24 +++++++++++------------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 913a65d37..e54ca6f3d 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -613,6 +613,9 @@ func (f FileOp) decompressWithSDK(srcFile string, dst string, cType CompressType func (f FileOp) Decompress(srcFile string, dst string, cType CompressType, secret string) error { if cType == Tar || cType == Zip || cType == TarGz { shellArchiver, err := NewShellArchiver(cType) + if !f.Stat(dst) { + _ = f.CreateDir(dst, 0755) + } if err == nil { if err = shellArchiver.Extract(srcFile, dst, secret); err == nil { return nil diff --git a/backend/utils/files/tar.go b/backend/utils/files/tar.go index 6aaa1f035..4ab276c97 100644 --- a/backend/utils/files/tar.go +++ b/backend/utils/files/tar.go @@ -18,7 +18,7 @@ func NewTarArchiver(compressType CompressType) ShellArchiver { } func (t TarArchiver) Extract(FilePath string, dstDir string, secret string) error { - return cmd.ExecCmd(fmt.Sprintf("%s %s %s -C %s", t.Cmd, t.getOptionStr("extract"), FilePath, dstDir)) + return cmd.ExecCmd(fmt.Sprintf("%s %s \"%s\" -C \"%s\"", t.Cmd, t.getOptionStr("extract"), FilePath, dstDir)) } func (t TarArchiver) Compress(sourcePaths []string, dstFile string, secret string) error { diff --git a/backend/utils/files/tar_gz.go b/backend/utils/files/tar_gz.go index 8a1691abf..1839639ef 100644 --- a/backend/utils/files/tar_gz.go +++ b/backend/utils/files/tar_gz.go @@ -20,11 +20,11 @@ func (t TarGzArchiver) Extract(filePath, dstDir string, secret string) error { var err error commands := "" if len(secret) != 0 { - extraCmd := "openssl enc -d -aes-256-cbc -k '" + secret + "' -in " + filePath + " | " - commands = fmt.Sprintf("%s tar -zxvf - -C %s", extraCmd, dstDir+" > /dev/null 2>&1") + extraCmd := fmt.Sprintf("openssl enc -d -aes-256-cbc -k '%s' -in '%s' | ", secret, filePath) + commands = fmt.Sprintf("%s tar -zxvf - -C '%s' > /dev/null 2>&1", extraCmd, dstDir) global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) } else { - commands = fmt.Sprintf("tar -zxvf %s %s", filePath+" -C ", dstDir+" > /dev/null 2>&1") + commands = fmt.Sprintf("tar -zxvf '%s' -C '%s' > /dev/null 2>&1", filePath, dstDir) global.LOG.Debug(commands) } if err = cmd.ExecCmd(commands); err != nil { @@ -34,27 +34,25 @@ func (t TarGzArchiver) Extract(filePath, dstDir string, secret string) error { } func (t TarGzArchiver) Compress(sourcePaths []string, dstFile string, secret string) error { - var err error - path := "" - itemDir := "" + var itemDirs []string for _, item := range sourcePaths { - itemDir += filepath.Base(item) + " " + itemDirs = append(itemDirs, fmt.Sprintf("\"%s\"", filepath.Base(item))) } - aheadDir := dstFile[:strings.LastIndex(dstFile, "/")] + itemDir := strings.Join(itemDirs, " ") + aheadDir := filepath.Dir(sourcePaths[0]) if len(aheadDir) == 0 { aheadDir = "/" } - path += fmt.Sprintf("- -C %s %s", aheadDir, itemDir) commands := "" if len(secret) != 0 { - extraCmd := "| openssl enc -aes-256-cbc -salt -k '" + secret + "' -out" - commands = fmt.Sprintf("tar -zcf %s %s %s", path, extraCmd, dstFile) + extraCmd := fmt.Sprintf("| openssl enc -aes-256-cbc -salt -k '%s' -out '%s'", secret, dstFile) + commands = fmt.Sprintf("tar -zcf - -C \"%s\" %s %s", aheadDir, itemDir, extraCmd) global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) } else { - commands = fmt.Sprintf("tar -zcf %s -C %s %s", dstFile, aheadDir, itemDir) + commands = fmt.Sprintf("tar -zcf \"%s\" -C \"%s\" %s", dstFile, aheadDir, itemDir) global.LOG.Debug(commands) } - if err = cmd.ExecCmd(commands); err != nil { + if err := cmd.ExecCmd(commands); err != nil { return err } return nil