mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 11:27:17 +08:00
fix: 尝试修复压缩
This commit is contained in:
@@ -134,7 +134,7 @@ func (r *backupRepo) CutoffLog(path, target string) error {
|
||||
}
|
||||
|
||||
to := filepath.Join(path, fmt.Sprintf("%s_%s.zip", time.Now().Format("20060102150405"), filepath.Base(target)))
|
||||
if err := io.Compress([]string{target}, to, io.Zip); err != nil {
|
||||
if err := io.Compress(filepath.Dir(target), []string{target}, to); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -226,18 +226,9 @@ func (r *backupRepo) createWebsite(to string, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var paths []string
|
||||
dirs, err := io.ReadDir(website.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, item := range dirs {
|
||||
paths = append(paths, filepath.Join(website.Path, item.Name()))
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
backup := filepath.Join(to, fmt.Sprintf("%s_%s.zip", website.Name, time.Now().Format("20060102150405")))
|
||||
if err = io.Compress(paths, backup, io.Zip); err != nil {
|
||||
if err = io.Compress(website.Path, nil, backup); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -281,7 +272,7 @@ func (r *backupRepo) createMySQL(to string, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = io.Compress([]string{backup}, backup+".zip", io.Zip); err != nil {
|
||||
if err = io.Compress(filepath.Dir(backup), []string{backup}, backup+".zip"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = io.Remove(backup); err != nil {
|
||||
@@ -318,7 +309,7 @@ func (r *backupRepo) createPostgres(to string, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = io.Compress([]string{backup}, backup+".zip", io.Zip); err != nil {
|
||||
if err = io.Compress(filepath.Dir(backup), []string{backup}, backup+".zip"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = io.Remove(backup); err != nil {
|
||||
@@ -341,11 +332,11 @@ func (r *backupRepo) createPanel(to string) error {
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
if err := io.Compress([]string{
|
||||
filepath.Join(app.Root, "panel"),
|
||||
if err := io.Compress(filepath.Dir(filepath.Join(app.Root, "panel")), []string{
|
||||
".",
|
||||
"/usr/local/sbin/panel-cli",
|
||||
"/usr/local/etc/panel/config.yml",
|
||||
}, backup, io.Zip); err != nil {
|
||||
}, backup); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := io.Chmod(backup, 0600); err != nil {
|
||||
@@ -369,15 +360,11 @@ func (r *backupRepo) restoreWebsite(backup, target string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
format, err := io.FormatArchiveByPath(backup)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = io.Remove(website.Path); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = io.UnCompress(backup, website.Path, format); err != nil {
|
||||
if err = io.UnCompress(backup, website.Path); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = io.Chmod(website.Path, 0755); err != nil {
|
||||
@@ -522,11 +509,7 @@ func (r *backupRepo) autoUnCompressSQL(backup string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
format, err := io.FormatArchiveByPath(backup)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = io.UnCompress(backup, temp, format); err != nil {
|
||||
if err = io.UnCompress(backup, temp); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ func (r *settingRepo) GetPanelSetting(ctx context.Context) (*request.PanelSettin
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cert, err := io.Read(filepath.Join(app.Root, "panel/storage/cert.pem"))
|
||||
crt, err := io.Read(filepath.Join(app.Root, "panel/storage/cert.pem"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -129,7 +129,7 @@ func (r *settingRepo) GetPanelSetting(ctx context.Context) (*request.PanelSettin
|
||||
Email: user.Email,
|
||||
Port: app.Conf.Int("http.port"),
|
||||
HTTPS: app.Conf.Bool("http.tls"),
|
||||
Cert: cert,
|
||||
Cert: crt,
|
||||
Key: key,
|
||||
}, nil
|
||||
}
|
||||
@@ -297,7 +297,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
}
|
||||
return fmt.Errorf("备份面板失败:%w", err)
|
||||
}
|
||||
if err := io.Compress([]string{filepath.Join(app.Root, "panel/storage")}, "/tmp/panel-storage.zip", io.Zip); err != nil {
|
||||
if err := io.Compress(filepath.Join(app.Root, "panel/storage"), nil, "/tmp/panel-storage.zip"); err != nil {
|
||||
if app.IsCli {
|
||||
fmt.Println("|-备份面板数据失败:", err)
|
||||
}
|
||||
@@ -317,7 +317,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
if app.IsCli {
|
||||
fmt.Println("|-解压新版本...")
|
||||
}
|
||||
if err := io.UnCompress(filepath.Join("/tmp", name), filepath.Join(app.Root, "panel"), io.Zip); err != nil {
|
||||
if err := io.UnCompress(filepath.Join("/tmp", name), filepath.Join(app.Root, "panel")); err != nil {
|
||||
return fmt.Errorf("解压失败:%w", err)
|
||||
}
|
||||
if !io.Exists(filepath.Join(app.Root, "panel", "web")) {
|
||||
@@ -327,7 +327,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
if app.IsCli {
|
||||
fmt.Println("|-恢复面板数据...")
|
||||
}
|
||||
if err := io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel"), io.Zip); err != nil {
|
||||
if err := io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel")); err != nil {
|
||||
return fmt.Errorf("恢复面板数据失败:%w", err)
|
||||
}
|
||||
if !io.Exists(filepath.Join(app.Root, "panel/storage/app.db")) {
|
||||
@@ -428,7 +428,7 @@ func (r *settingRepo) FixPanel() error {
|
||||
if err = io.Remove("/tmp/panel-fix"); err != nil {
|
||||
return fmt.Errorf("清理临时目录失败:%w", err)
|
||||
}
|
||||
if err = io.UnCompress(latest.Path, "/tmp/panel-fix", io.Zip); err != nil {
|
||||
if err = io.UnCompress(latest.Path, "/tmp/panel-fix"); err != nil {
|
||||
return fmt.Errorf("解压备份文件失败:%w", err)
|
||||
}
|
||||
|
||||
@@ -460,7 +460,7 @@ func (r *settingRepo) FixPanel() error {
|
||||
fmt.Println("|-恢复面板数据...")
|
||||
}
|
||||
if io.Exists("/tmp/panel-storage.zip") {
|
||||
if err = io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel"), io.Zip); err != nil {
|
||||
if err = io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel")); err != nil {
|
||||
return fmt.Errorf("恢复面板数据失败:%w", err)
|
||||
}
|
||||
if err = io.Remove("/tmp/panel-storage.zip"); err != nil {
|
||||
|
||||
@@ -317,13 +317,7 @@ func (s *FileService) Compress(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
format, err := io.FormatArchiveByPath(req.File)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = io.Compress(req.Paths, req.File, format); err != nil {
|
||||
if err = io.Compress(filepath.Dir(req.Paths[0]), req.Paths, req.File); err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
@@ -339,13 +333,7 @@ func (s *FileService) UnCompress(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
format, err := io.FormatArchiveByPath(req.File)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = io.UnCompress(req.File, req.Path, format); err != nil {
|
||||
if err = io.UnCompress(req.File, req.Path); err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -283,7 +283,7 @@ func (s *FileService) Compress(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = io.Compress(req.Paths, req.File, io.Zip); err != nil {
|
||||
if err = io.Compress(filepath.Dir(req.Paths[0]), req.Paths, req.File); err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
@@ -299,7 +299,7 @@ func (s *FileService) UnCompress(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = io.UnCompress(req.File, req.Path, io.Zip); err != nil {
|
||||
if err = io.UnCompress(req.File, req.Path); err != nil {
|
||||
Error(w, http.StatusInternalServerError, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -20,20 +20,24 @@ const (
|
||||
)
|
||||
|
||||
// Compress 压缩文件
|
||||
func Compress(src []string, dst string, format FormatArchive) error {
|
||||
func Compress(dir string, src []string, dst string) error {
|
||||
if len(src) == 0 {
|
||||
return errors.New("source is empty")
|
||||
src = append(src, ".")
|
||||
}
|
||||
// 去掉路径前缀,减少压缩包内文件夹层级
|
||||
for i, s := range src {
|
||||
src[i] = strings.TrimPrefix(s, dir)
|
||||
if src[i] == "" {
|
||||
src[i] = "."
|
||||
}
|
||||
}
|
||||
|
||||
cmd := new(exec.Cmd)
|
||||
cmd.Dir = filepath.Dir(src[0])
|
||||
cmd.Dir = dir
|
||||
|
||||
// 取相对路径,避免压缩包内多一层目录
|
||||
for i, item := range src {
|
||||
if !strings.HasPrefix(item, cmd.Dir) {
|
||||
continue
|
||||
}
|
||||
src[i] = filepath.Base(item)
|
||||
format, err := formatArchiveByPath(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch format {
|
||||
@@ -59,9 +63,14 @@ func Compress(src []string, dst string, format FormatArchive) error {
|
||||
}
|
||||
|
||||
// UnCompress 解压文件
|
||||
func UnCompress(src string, dst string, format FormatArchive) error {
|
||||
func UnCompress(src string, dst string) error {
|
||||
var cmd *exec.Cmd
|
||||
|
||||
format, err := formatArchiveByPath(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch format {
|
||||
case Zip:
|
||||
cmd = exec.Command("unzip", "-qo", src, "-d", dst)
|
||||
@@ -76,7 +85,7 @@ func UnCompress(src string, dst string, format FormatArchive) error {
|
||||
case Xz:
|
||||
cmd = exec.Command("tar", "-xJf", src, "-C", dst)
|
||||
case SevenZip:
|
||||
cmd = exec.Command("7z", "x", "-y", src, "-o", dst)
|
||||
cmd = exec.Command("7z", "x", "-y", src, "-o"+dst)
|
||||
default:
|
||||
return errors.New("unsupported format")
|
||||
}
|
||||
@@ -84,8 +93,8 @@ func UnCompress(src string, dst string, format FormatArchive) error {
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
// FormatArchiveByPath 根据文件后缀获取压缩格式
|
||||
func FormatArchiveByPath(path string) (FormatArchive, error) {
|
||||
// formatArchiveByPath 根据文件后缀获取压缩格式
|
||||
func formatArchiveByPath(path string) (FormatArchive, error) {
|
||||
switch filepath.Ext(path) {
|
||||
case ".zip":
|
||||
return Zip, nil
|
||||
|
||||
Reference in New Issue
Block a user