2024-07-23 14:48:37 +08:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TarGzArchiver struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTarGzArchiver() ShellArchiver {
|
|
|
|
return &TarGzArchiver{}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
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")
|
|
|
|
global.LOG.Debug(commands)
|
|
|
|
}
|
|
|
|
if err = cmd.ExecCmd(commands); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TarGzArchiver) Compress(sourcePaths []string, dstFile string, secret string) error {
|
2024-10-16 14:29:27 +08:00
|
|
|
var itemDirs []string
|
2024-07-23 14:48:37 +08:00
|
|
|
for _, item := range sourcePaths {
|
2024-10-16 14:29:27 +08:00
|
|
|
itemDirs = append(itemDirs, fmt.Sprintf("\"%s\"", filepath.Base(item)))
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
2024-10-16 14:29:27 +08:00
|
|
|
itemDir := strings.Join(itemDirs, " ")
|
|
|
|
aheadDir := filepath.Dir(sourcePaths[0])
|
2024-07-23 14:48:37 +08:00
|
|
|
if len(aheadDir) == 0 {
|
|
|
|
aheadDir = "/"
|
|
|
|
}
|
|
|
|
commands := ""
|
|
|
|
if len(secret) != 0 {
|
2024-10-16 14:29:27 +08:00
|
|
|
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)
|
2024-07-23 14:48:37 +08:00
|
|
|
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))
|
|
|
|
} else {
|
2024-10-16 14:29:27 +08:00
|
|
|
commands = fmt.Sprintf("tar -zcf \"%s\" -C \"%s\" %s", dstFile, aheadDir, itemDir)
|
2024-07-23 14:48:37 +08:00
|
|
|
global.LOG.Debug(commands)
|
|
|
|
}
|
2024-10-16 14:29:27 +08:00
|
|
|
if err := cmd.ExecCmd(commands); err != nil {
|
2024-07-23 14:48:37 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|