Skip to content

Commit 652ad27

Browse files
committed
feat: add support for stopping compression tasks and enhance compression functionality
1 parent 02bd500 commit 652ad27

22 files changed

Lines changed: 637 additions & 83 deletions

File tree

agent/app/api/v2/file.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,26 @@ func (b *BaseApi) CompressFile(c *gin.Context) {
250250
helper.Success(c)
251251
}
252252

253+
// @Tags File
254+
// @Summary Stop compress task
255+
// @Accept json
256+
// @Param request body request.FileCompressStopReq true "request"
257+
// @Success 200
258+
// @Security ApiKeyAuth
259+
// @Security Timestamp
260+
// @Router /files/compress/stop [post]
261+
func (b *BaseApi) StopCompressFile(c *gin.Context) {
262+
var req request.FileCompressStopReq
263+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
264+
return
265+
}
266+
if err := fileService.StopCompress(req.TaskID); err != nil {
267+
helper.InternalServer(c, err)
268+
return
269+
}
270+
helper.Success(c)
271+
}
272+
253273
// @Tags File
254274
// @Summary Decompress file
255275
// @Accept json

agent/app/dto/logs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
type SearchTaskLogReq struct {
88
Status string `json:"status"`
99
Type string `json:"type"`
10+
TaskID string `json:"taskID"`
1011
PageInfo
1112
}
1213

agent/app/dto/request/file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ type FileCompress struct {
7878
Name string `json:"name" validate:"required"`
7979
Replace bool `json:"replace"`
8080
Secret string `json:"secret"`
81+
TaskID string `json:"taskID"`
82+
}
83+
84+
type FileCompressStopReq struct {
85+
TaskID string `json:"taskID" validate:"required"`
8186
}
8287

8388
type FileDeCompress struct {

agent/app/dto/request/task.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

agent/app/service/file.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type IFileService interface {
5757
Delete(op request.FileDelete) error
5858
BatchDelete(op request.FileBatchDelete) error
5959
Compress(c request.FileCompress) error
60+
StopCompress(taskID string) error
6061
DeCompress(c request.FileDeCompress) error
6162
GetContent(op request.FileContentReq) (response.FileInfo, error)
6263
GetPreviewContent(op request.FileContentReq) (response.FileInfo, error)
@@ -425,7 +426,58 @@ func (f *FileService) Compress(c request.FileCompress) error {
425426
if !c.Replace && fo.Stat(filepath.Join(c.Dst, c.Name)) {
426427
return buserr.New("ErrFileIsExist")
427428
}
428-
return fo.Compress(c.Files, c.Dst, c.Name, files.CompressType(c.Type), c.Secret)
429+
if err := preflightCompressTool(files.CompressType(c.Type)); err != nil {
430+
return err
431+
}
432+
taskItem, err := task.NewTask(c.Name, task.TaskExec, task.TaskScopeTask, c.TaskID, 1)
433+
if err != nil {
434+
return err
435+
}
436+
go func() {
437+
taskItem.AddSubTask(c.Name, func(t *task.Task) error {
438+
t.LogStart(c.Name)
439+
compressType := files.CompressType(c.Type)
440+
dstFile := filepath.Join(c.Dst, c.Name)
441+
success := false
442+
defer func() {
443+
if !success {
444+
_ = os.Remove(dstFile)
445+
}
446+
}()
447+
if err := fo.Compress(t.TaskCtx, c.Files, c.Dst, c.Name, compressType, c.Secret, nil); err != nil {
448+
return err
449+
}
450+
info, err := os.Stat(dstFile)
451+
if err != nil {
452+
return err
453+
}
454+
if info.Size() == 0 {
455+
return fmt.Errorf("compressed file not generated: %s", dstFile)
456+
}
457+
success = true
458+
return nil
459+
}, nil)
460+
_ = taskItem.Execute()
461+
}()
462+
return nil
463+
}
464+
465+
func preflightCompressTool(compressType files.CompressType) error {
466+
switch compressType {
467+
case files.TarGz, files.Rar, files.X7z:
468+
_, err := files.NewShellArchiver(compressType)
469+
return err
470+
default:
471+
return nil
472+
}
473+
}
474+
475+
func (f *FileService) StopCompress(taskID string) error {
476+
if cancel, ok := global.TaskCtxMap[taskID]; ok {
477+
cancel()
478+
return nil
479+
}
480+
return buserr.New("TaskNotFound")
429481
}
430482

431483
func (f *FileService) DeCompress(c request.FileDeCompress) error {
@@ -694,7 +746,7 @@ func (f *FileService) FileDownload(d request.FileDownload) (string, error) {
694746
return "", err
695747
}
696748
fo := files.NewFileOp()
697-
if err := fo.Compress(d.Paths, tempPath, d.Name, files.CompressType(d.Type), ""); err != nil {
749+
if err := fo.Compress(context.Background(), d.Paths, tempPath, d.Name, files.CompressType(d.Type), "", nil); err != nil {
698750
return "", err
699751
}
700752
filePath = filepath.Join(tempPath, d.Name)

agent/app/service/task.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func (u *TaskLogService) Page(req dto.SearchTaskLogReq) (int64, []dto.TaskDTO, e
2121
opts := []repo.DBOption{
2222
repo.WithOrderDesc("created_at"),
2323
}
24+
if req.TaskID != "" {
25+
opts = append(opts, taskRepo.WithByID(req.TaskID))
26+
}
2427
if req.Status != "" {
2528
opts = append(opts, repo.WithByStatus(req.Status))
2629
}

agent/app/service/website_ca.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func (w WebsiteCAService) DownloadFile(id uint) (*os.File, error) {
443443
return nil, err
444444
}
445445
fileName := ca.Name + ".zip"
446-
if err = fileOp.Compress([]string{path.Join(dir, "ca.crt"), path.Join(dir, "ca.key")}, dir, fileName, files.SdkZip, ""); err != nil {
446+
if err = fileOp.Compress(context.Background(), []string{path.Join(dir, "ca.crt"), path.Join(dir, "ca.key")}, dir, fileName, files.SdkZip, "", nil); err != nil {
447447
return nil, err
448448
}
449449
return os.Open(path.Join(dir, fileName))

agent/app/service/website_ssl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ func (w WebsiteSSLService) DownloadFile(id uint) (*os.File, error) {
758758
return nil, err
759759
}
760760
fileName := websiteSSL.PrimaryDomain + ".zip"
761-
if err = fileOp.Compress([]string{path.Join(dir, "fullchain.pem"), path.Join(dir, "privkey.pem")}, dir, fileName, files.SdkZip, ""); err != nil {
761+
if err = fileOp.Compress(context.Background(), []string{path.Join(dir, "fullchain.pem"), path.Join(dir, "privkey.pem")}, dir, fileName, files.SdkZip, "", nil); err != nil {
762762
return nil, err
763763
}
764764
return os.Open(path.Join(dir, fileName))

agent/router/ro_file.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func (f *FileRouter) InitRouter(Router *gin.RouterGroup) {
2323
fileRouter.POST("/mode", baseApi.ChangeFileMode)
2424
fileRouter.POST("/owner", baseApi.ChangeFileOwner)
2525
fileRouter.POST("/compress", baseApi.CompressFile)
26+
fileRouter.POST("/compress/stop", baseApi.StopCompressFile)
2627
fileRouter.POST("/decompress", baseApi.DeCompressFile)
2728
fileRouter.POST("/content", baseApi.GetContent)
2829
fileRouter.POST("/preview", baseApi.PreviewContent)

agent/utils/files/archiver.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package files
22

33
import (
4+
"context"
5+
46
"github.com/1Panel-dev/1Panel/agent/buserr"
57
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
68
)
79

810
type ShellArchiver interface {
911
Extract(filePath, dstDir string, secret string) error
10-
Compress(sourcePaths []string, dstFile string, secret string) error
12+
Compress(ctx context.Context, sourcePaths []string, dstFile string, secret string) error
1113
}
1214

1315
func NewShellArchiver(compressType CompressType) (ShellArchiver, error) {
@@ -18,14 +20,17 @@ func NewShellArchiver(compressType CompressType) (ShellArchiver, error) {
1820
}
1921
return NewTarArchiver(compressType), nil
2022
case TarGz:
23+
if err := checkCmdAvailability("tar"); err != nil {
24+
return nil, err
25+
}
2126
return NewTarGzArchiver(), nil
2227
case Zip:
2328
if err := checkCmdAvailability("zip"); err != nil {
2429
return nil, err
2530
}
2631
return NewZipArchiver(), nil
2732
case Rar:
28-
if err := checkCmdAvailability("unrar"); err != nil {
33+
if err := checkCmdAvailability("rar"); err != nil {
2934
return nil, err
3035
}
3136
return NewRarArchiver(), nil
@@ -39,6 +44,38 @@ func NewShellArchiver(compressType CompressType) (ShellArchiver, error) {
3944
}
4045
}
4146

47+
func NewExtractShellArchiver(compressType CompressType) (ShellArchiver, error) {
48+
switch compressType {
49+
case Tar:
50+
if err := checkCmdAvailability("tar"); err != nil {
51+
return nil, err
52+
}
53+
return NewTarArchiver(compressType), nil
54+
case TarGz:
55+
if err := checkCmdAvailability("tar"); err != nil {
56+
return nil, err
57+
}
58+
return NewTarGzArchiver(), nil
59+
case Zip:
60+
if err := checkCmdAvailability("unzip"); err != nil {
61+
return nil, err
62+
}
63+
return NewZipArchiver(), nil
64+
case Rar:
65+
if err := checkCmdAvailability("unrar"); err != nil {
66+
return nil, err
67+
}
68+
return NewRarArchiver(), nil
69+
case X7z:
70+
if err := checkCmdAvailability("7z"); err != nil {
71+
return nil, err
72+
}
73+
return NewX7zArchiver(), nil
74+
default:
75+
return nil, buserr.New("unsupported decompress type")
76+
}
77+
}
78+
4279
func checkCmdAvailability(cmdStr string) error {
4380
if cmd.Which(cmdStr) {
4481
return nil

0 commit comments

Comments
 (0)