2024-06-04 08:48:31 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-06-05 06:31:26 +00:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2024-06-04 08:48:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TarGzArchiver struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTarGzArchiver() ShellArchiver {
|
|
|
|
return &TarGzArchiver{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TarGzArchiver) Extract(filePath, dstDir string, secret string) error {
|
|
|
|
var err error
|
|
|
|
commands := ""
|
2024-06-06 10:43:38 +00:00
|
|
|
if len(secret) != 0 {
|
2024-06-04 08:48:31 +00:00
|
|
|
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")
|
2024-06-05 06:31:26 +00:00
|
|
|
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))
|
|
|
|
} else {
|
2024-06-06 10:43:38 +00:00
|
|
|
commands = fmt.Sprintf("tar -zxvf %s %s", filePath+" -C ", dstDir+" > /dev/null 2>&1")
|
2024-06-05 06:31:26 +00:00
|
|
|
global.LOG.Debug(commands)
|
|
|
|
}
|
2024-06-04 08:48:31 +00:00
|
|
|
if err = cmd.ExecCmd(commands); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TarGzArchiver) Compress(sourcePaths []string, dstFile string, secret string) error {
|
|
|
|
var err error
|
|
|
|
path := ""
|
|
|
|
itemDir := ""
|
|
|
|
for _, item := range sourcePaths {
|
|
|
|
itemDir += filepath.Base(item) + " "
|
|
|
|
}
|
|
|
|
aheadDir := dstFile[:strings.LastIndex(dstFile, "/")]
|
|
|
|
if len(aheadDir) == 0 {
|
|
|
|
aheadDir = "/"
|
|
|
|
}
|
|
|
|
path += fmt.Sprintf("- -C %s %s", aheadDir, itemDir)
|
|
|
|
commands := ""
|
2024-06-06 10:43:38 +00:00
|
|
|
if len(secret) != 0 {
|
2024-06-04 08:48:31 +00:00
|
|
|
extraCmd := "| openssl enc -aes-256-cbc -salt -k " + secret + " -out"
|
|
|
|
commands = fmt.Sprintf("tar -zcf %s %s %s", path, extraCmd, dstFile)
|
2024-06-05 06:31:26 +00:00
|
|
|
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))
|
|
|
|
} else {
|
2024-06-06 10:43:38 +00:00
|
|
|
commands = fmt.Sprintf("tar -zcf %s -C %s %s", dstFile, aheadDir, itemDir)
|
2024-06-05 06:31:26 +00:00
|
|
|
global.LOG.Debug(commands)
|
2024-06-04 08:48:31 +00:00
|
|
|
}
|
|
|
|
if err = cmd.ExecCmd(commands); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|