Skip to content

Commit

Permalink
feat: 优化压缩解压错误输出
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 27, 2024
1 parent 9075b43 commit c68ee09
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
33 changes: 15 additions & 18 deletions pkg/io/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io

import (
"errors"
"os/exec"
"github.com/TheTNB/panel/pkg/shell"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -41,26 +41,24 @@ func Compress(dir string, src []string, dst string) error {
return err
}

cmd := new(exec.Cmd)
switch format {
case Zip:
cmd = exec.Command("zip", append([]string{"-qr", "-o", dst}, src...)...)
_, err = shell.ExecfWithDir(dir, "zip -qr -o %s %s", dst, strings.Join(src, " "))
case TGz:
cmd = exec.Command("tar", append([]string{"-czf", dst}, src...)...)
_, err = shell.ExecfWithDir(dir, "tar -czf %s %s", dst, strings.Join(src, " "))
case Bz2:
cmd = exec.Command("tar", append([]string{"-cjf", dst}, src...)...)
_, err = shell.ExecfWithDir(dir, "tar -cjf %s %s", dst, strings.Join(src, " "))
case Tar:
cmd = exec.Command("tar", append([]string{"-cf", dst}, src...)...)
_, err = shell.ExecfWithDir(dir, "tar -cf %s %s", dst, strings.Join(src, " "))
case Xz:
cmd = exec.Command("tar", append([]string{"-cJf", dst}, src...)...)
_, err = shell.ExecfWithDir(dir, "tar -cJf %s %s", dst, strings.Join(src, " "))
case SevenZip:
cmd = exec.Command("7z", append([]string{"a", "-y", dst}, src...)...)
_, err = shell.ExecfWithDir(dir, "7z a -y %s %s", dst, strings.Join(src, " "))
default:
return errors.New("unsupported format")
}

cmd.Dir = dir
return cmd.Run()
return err
}

// UnCompress 解压文件
Expand All @@ -79,25 +77,24 @@ func UnCompress(src string, dst string) error {
return err
}

var cmd *exec.Cmd
switch format {
case Zip:
cmd = exec.Command("unzip", "-qo", src, "-d", dst)
_, err = shell.Execf("unzip -qo '%s' -d '%s'", src, dst)
case TGz:
cmd = exec.Command("tar", "-xzf", src, "-C", dst)
_, err = shell.Execf("tar -xzf '%s' -C '%s'", src, dst)
case Bz2:
cmd = exec.Command("tar", "-xjf", src, "-C", dst)
_, err = shell.Execf("tar -xjf '%s' -C '%s'", src, dst)
case Tar:
cmd = exec.Command("tar", "-xf", src, "-C", dst)
_, err = shell.Execf("tar -xf '%s' -C '%s'", src, dst)
case Xz:
cmd = exec.Command("tar", "-xJf", src, "-C", dst)
_, err = shell.Execf("tar -xJf '%s' -C '%s'", src, dst)
case SevenZip:
cmd = exec.Command("7z", "x", "-y", src, "-o"+dst)
_, err = shell.Execf("7z x -y '%s' -o'%s'", src, dst)
default:
return errors.New("unsupported format")
}

return cmd.Run()
return err
}

// formatArchiveByPath 根据文件后缀获取压缩格式
Expand Down
22 changes: 22 additions & 0 deletions pkg/shell/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ func ExecfWithPipe(ctx context.Context, shell string, args ...any) (out io.ReadC
return
}

// ExecfWithDir 在指定目录下执行 shell 命令
func ExecfWithDir(dir, shell string, args ...any) (string, error) {
if !preCheckArg(args) {
return "", errors.New("command contains illegal characters")
}

_ = os.Setenv("LC_ALL", "C")
cmd := exec.Command("bash", "-c", fmt.Sprintf(shell, args...))
cmd.Dir = dir

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Run()
if err != nil {
return strings.TrimSpace(stdout.String()), errors.New(strings.TrimSpace(stderr.String()))
}

return strings.TrimSpace(stdout.String()), err
}

func preCheckArg(args []any) bool {
illegals := []any{`&`, `|`, `;`, `$`, `'`, `"`, "`", `(`, `)`, "\n", "\r", `>`, `<`}
for arg := range slices.Values(args) {
Expand Down
8 changes: 5 additions & 3 deletions web/src/utils/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ const getIconByExt = (ext: string) => {
case 'ape':
return 'bi:file-earmark-music'
case 'zip':
case 'rar':
case '7z':
case 'bz2':
case 'tar':
case 'gz':
case 'tgz':
case 'xz':
case '7z':
return 'bi:file-earmark-zip'
case 'doc':
case 'docx':
Expand Down Expand Up @@ -288,7 +290,7 @@ const getFilename = (path: string) => {

const isCompress = (name: string) => {
const ext = getExt(name)
return ['zip', 'rar', '7z', 'tar', 'gz'].includes(ext)
return ['zip', 'bz2', 'tar', 'gz', 'tgz', 'xz', '7z'].includes(ext)
}

const formatPercent = (num: any) => {
Expand Down
7 changes: 4 additions & 3 deletions web/src/views/file/CompressModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ onMounted(() => {
v-model:value="format"
:options="[
{ label: '.zip', value: '.zip' },
{ label: '.gz', value: '.gz' },
{ label: '.xz', value: '.xz' },
{ label: '.bz2', value: '.bz2' },
{ label: '.tar', value: '.tar' },
{ label: '.gz', value: '.gz' },
{ label: '.tar.gz', value: '.tar.gz' },
{ label: '.tar.bz2', value: '.tar.bz2' }
{ label: '.tgz', value: '.tgz' },
{ label: '.xz', value: '.xz' },
{ label: '.7z', value: '.7z' }
]"
@update:value="ensureExtension"
/>
Expand Down

0 comments on commit c68ee09

Please sign in to comment.