2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 06:47:20 +08:00

feat: 规范压缩解压

This commit is contained in:
耗子
2024-10-28 03:21:22 +08:00
parent 899ce1fa18
commit 12ecf0707e
7 changed files with 21 additions and 28 deletions

View File

@@ -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 {

View File

@@ -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"`
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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"))

View File

@@ -45,8 +45,8 @@ export default {
group: string
): Promise<AxiosResponse<any>> => request.post('/file/permission', { path, mode, owner, group }),
// 压缩文件
compress: (paths: string[], file: string): Promise<AxiosResponse<any>> =>
request.post('/file/compress', { paths, file }),
compress: (dir: string, paths: string[], file: string): Promise<AxiosResponse<any>> =>
request.post('/file/compress', { dir, paths, file }),
// 解压文件
unCompress: (file: string, path: string): Promise<AxiosResponse<any>> =>
request.post('/file/unCompress', { file, path }),

View File

@@ -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