diff --git a/internal/data/backup.go b/internal/data/backup.go index 715bcdd2..ae01a300 100644 --- a/internal/data/backup.go +++ b/internal/data/backup.go @@ -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(filepath.Dir(target), []string{target}, to); err != nil { + if err := io.Compress(filepath.Dir(target), []string{filepath.Base(target)}, to); err != nil { return err } @@ -272,7 +272,7 @@ func (r *backupRepo) createMySQL(to string, name string) error { return err } - if err = io.Compress(filepath.Dir(backup), []string{backup}, backup+".zip"); err != nil { + if err = io.Compress(filepath.Dir(backup), []string{filepath.Base(backup)}, backup+".zip"); err != nil { return err } if err = io.Remove(backup); err != nil { @@ -309,7 +309,7 @@ func (r *backupRepo) createPostgres(to string, name string) error { return err } - if err = io.Compress(filepath.Dir(backup), []string{backup}, backup+".zip"); err != nil { + if err = io.Compress(filepath.Dir(backup), []string{filepath.Base(backup)}, backup+".zip"); err != nil { return err } if err = io.Remove(backup); err != nil { diff --git a/internal/http/request/file.go b/internal/http/request/file.go index dfe5a5d7..e2d63388 100644 --- a/internal/http/request/file.go +++ b/internal/http/request/file.go @@ -50,6 +50,7 @@ type FilePermission struct { } type FileCompress struct { + Dir string `form:"dir" json:"dir" validate:"required"` Paths []string `form:"paths" json:"paths" validate:"min=1,dive,required"` File string `form:"file" json:"file" validate:"required"` } diff --git a/internal/service/file.go b/internal/service/file.go index 80b33a28..ff264b3d 100644 --- a/internal/service/file.go +++ b/internal/service/file.go @@ -317,7 +317,7 @@ func (s *FileService) Compress(w http.ResponseWriter, r *http.Request) { return } - if err = io.Compress(filepath.Dir(req.Paths[0]), req.Paths, req.File); err != nil { + if err = io.Compress(req.Dir, req.Paths, req.File); err != nil { Error(w, http.StatusInternalServerError, "%v", err) return } diff --git a/pkg/io/compress.go b/pkg/io/compress.go index fcdc1747..fda163dc 100644 --- a/pkg/io/compress.go +++ b/pkg/io/compress.go @@ -26,15 +26,6 @@ func Compress(dir string, src []string, dst string) error { if len(src) == 0 { src = append(src, ".") } - // 去掉路径前缀,减少压缩包内文件夹层级 - for i, s := range src { - if strings.HasPrefix(s, dir) { - s = strings.TrimPrefix(s, dir) - if s != "" && s[0] == '/' { - src[i] = strings.TrimPrefix(s, "/") - } - } - } format, err := formatArchiveByPath(dst) if err != nil { diff --git a/pkg/io/io_test.go b/pkg/io/io_test.go index 07074cfe..258b720f 100644 --- a/pkg/io/io_test.go +++ b/pkg/io/io_test.go @@ -53,14 +53,14 @@ func (s *IOTestSuite) TestWriteAppendAppendsToFile() { } func (s *IOTestSuite) TestCompress() { - src := []string{"compress_test1.txt", "compress_test2.txt"} - err := Write(src[0], "File 1", 0644) - s.NoError(err) - err = Write(src[1], "File 2", 0644) - s.NoError(err) - abs, err := filepath.Abs("testdata") s.NoError(err) + src := []string{"compress_test1.txt", "compress_test2.txt"} + err = Write(filepath.Join(abs, src[0]), "File 1", 0644) + s.NoError(err) + err = Write(filepath.Join(abs, src[1]), "File 2", 0644) + s.NoError(err) + err = Compress(abs, src, filepath.Join(abs, "compress_test.zip")) s.NoError(err) err = Compress(abs, src, filepath.Join(abs, "compress_test.bz2")) @@ -82,14 +82,14 @@ func (s *IOTestSuite) TestCompress() { } func (s *IOTestSuite) TestUnCompress() { - src := []string{"uncompress_test1.txt", "uncompress_test2.txt"} - err := Write(src[0], "File 1", 0644) - s.NoError(err) - err = Write(src[1], "File 2", 0644) - s.NoError(err) - abs, err := filepath.Abs("testdata") s.NoError(err) + src := []string{"uncompress_test1.txt", "uncompress_test2.txt"} + err = Write(filepath.Join(abs, src[0]), "File 1", 0644) + s.NoError(err) + err = Write(filepath.Join(abs, src[1]), "File 2", 0644) + s.NoError(err) + err = Compress(abs, src, filepath.Join(abs, "uncompress_test.zip")) s.NoError(err) err = Compress(abs, src, filepath.Join(abs, "uncompress_test.bz2")) diff --git a/web/src/api/panel/file/index.ts b/web/src/api/panel/file/index.ts index 0849d908..3033a9bc 100644 --- a/web/src/api/panel/file/index.ts +++ b/web/src/api/panel/file/index.ts @@ -45,8 +45,8 @@ export default { group: string ): Promise> => request.post('/file/permission', { path, mode, owner, group }), // 压缩文件 - compress: (paths: string[], file: string): Promise> => - request.post('/file/compress', { paths, file }), + compress: (dir: string, paths: string[], file: string): Promise> => + request.post('/file/compress', { dir, paths, file }), // 解压文件 unCompress: (file: string, path: string): Promise> => request.post('/file/unCompress', { file, path }), diff --git a/web/src/views/file/CompressModal.vue b/web/src/views/file/CompressModal.vue index 6e27f410..3d833a83 100644 --- a/web/src/views/file/CompressModal.vue +++ b/web/src/views/file/CompressModal.vue @@ -30,8 +30,9 @@ const handleArchive = async () => { const message = window.$message.loading('正在压缩中...', { duration: 0 }) + const paths = selected.value.map((item) => item.replace(path.value, '').replace(/^\//, '')) await api - .compress(selected.value, file.value) + .compress(path.value, paths, file.value) .then(() => { window.$message.success('压缩成功') show.value = false