Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions agent/app/api/v2/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,26 @@ func (b *BaseApi) CompressFile(c *gin.Context) {
helper.Success(c)
}

// @Tags File
// @Summary Stop compress task
// @Accept json
// @Param request body request.FileCompressStopReq true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /files/compress/stop [post]
func (b *BaseApi) StopCompressFile(c *gin.Context) {
var req request.FileCompressStopReq
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := fileService.StopCompress(req.TaskID); err != nil {
helper.InternalServer(c, err)
return
}
helper.Success(c)
}

// @Tags File
// @Summary Decompress file
// @Accept json
Expand Down
1 change: 1 addition & 0 deletions agent/app/dto/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
type SearchTaskLogReq struct {
Status string `json:"status"`
Type string `json:"type"`
TaskID string `json:"taskID"`
PageInfo
}

Expand Down
5 changes: 5 additions & 0 deletions agent/app/dto/request/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ type FileCompress struct {
Name string `json:"name" validate:"required"`
Replace bool `json:"replace"`
Secret string `json:"secret"`
TaskID string `json:"taskID"`
}

type FileCompressStopReq struct {
TaskID string `json:"taskID" validate:"required"`
}

type FileDeCompress struct {
Expand Down
1 change: 0 additions & 1 deletion agent/app/dto/request/task.go

This file was deleted.

56 changes: 54 additions & 2 deletions agent/app/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type IFileService interface {
Delete(op request.FileDelete) error
BatchDelete(op request.FileBatchDelete) error
Compress(c request.FileCompress) error
StopCompress(taskID string) error
DeCompress(c request.FileDeCompress) error
GetContent(op request.FileContentReq) (response.FileInfo, error)
GetPreviewContent(op request.FileContentReq) (response.FileInfo, error)
Expand Down Expand Up @@ -425,7 +426,58 @@ func (f *FileService) Compress(c request.FileCompress) error {
if !c.Replace && fo.Stat(filepath.Join(c.Dst, c.Name)) {
return buserr.New("ErrFileIsExist")
}
return fo.Compress(c.Files, c.Dst, c.Name, files.CompressType(c.Type), c.Secret)
if err := preflightCompressTool(files.CompressType(c.Type)); err != nil {
return err
}
taskItem, err := task.NewTask(c.Name, task.TaskExec, task.TaskScopeTask, c.TaskID, 1)
if err != nil {
return err
}
go func() {
taskItem.AddSubTask(c.Name, func(t *task.Task) error {
t.LogStart(c.Name)
compressType := files.CompressType(c.Type)
dstFile := filepath.Join(c.Dst, c.Name)
success := false
defer func() {
if !success {
_ = os.Remove(dstFile)
}
}()
if err := fo.Compress(t.TaskCtx, c.Files, c.Dst, c.Name, compressType, c.Secret, nil); err != nil {
return err
}
info, err := os.Stat(dstFile)
if err != nil {
return err
}
if info.Size() == 0 {
return fmt.Errorf("compressed file not generated: %s", dstFile)
}
success = true
return nil
}, nil)
_ = taskItem.Execute()
}()
return nil
}

func preflightCompressTool(compressType files.CompressType) error {
switch compressType {
case files.TarGz, files.Rar, files.X7z:
_, err := files.NewShellArchiver(compressType)
return err
default:
return nil
}
}

func (f *FileService) StopCompress(taskID string) error {
if cancel, ok := global.TaskCtxMap[taskID]; ok {
cancel()
return nil
}
return buserr.New("TaskNotFound")
}

func (f *FileService) DeCompress(c request.FileDeCompress) error {
Expand Down Expand Up @@ -694,7 +746,7 @@ func (f *FileService) FileDownload(d request.FileDownload) (string, error) {
return "", err
}
fo := files.NewFileOp()
if err := fo.Compress(d.Paths, tempPath, d.Name, files.CompressType(d.Type), ""); err != nil {
if err := fo.Compress(context.Background(), d.Paths, tempPath, d.Name, files.CompressType(d.Type), "", nil); err != nil {
return "", err
}
filePath = filepath.Join(tempPath, d.Name)
Expand Down
3 changes: 3 additions & 0 deletions agent/app/service/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func (u *TaskLogService) Page(req dto.SearchTaskLogReq) (int64, []dto.TaskDTO, e
opts := []repo.DBOption{
repo.WithOrderDesc("created_at"),
}
if req.TaskID != "" {
opts = append(opts, taskRepo.WithByID(req.TaskID))
}
if req.Status != "" {
opts = append(opts, repo.WithByStatus(req.Status))
}
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/website_ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (w WebsiteCAService) DownloadFile(id uint) (*os.File, error) {
return nil, err
}
fileName := ca.Name + ".zip"
if err = fileOp.Compress([]string{path.Join(dir, "ca.crt"), path.Join(dir, "ca.key")}, dir, fileName, files.SdkZip, ""); err != nil {
if err = fileOp.Compress(context.Background(), []string{path.Join(dir, "ca.crt"), path.Join(dir, "ca.key")}, dir, fileName, files.SdkZip, "", nil); err != nil {
return nil, err
}
return os.Open(path.Join(dir, fileName))
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/website_ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ func (w WebsiteSSLService) DownloadFile(id uint) (*os.File, error) {
return nil, err
}
fileName := websiteSSL.PrimaryDomain + ".zip"
if err = fileOp.Compress([]string{path.Join(dir, "fullchain.pem"), path.Join(dir, "privkey.pem")}, dir, fileName, files.SdkZip, ""); err != nil {
if err = fileOp.Compress(context.Background(), []string{path.Join(dir, "fullchain.pem"), path.Join(dir, "privkey.pem")}, dir, fileName, files.SdkZip, "", nil); err != nil {
return nil, err
}
return os.Open(path.Join(dir, fileName))
Expand Down
1 change: 1 addition & 0 deletions agent/router/ro_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (f *FileRouter) InitRouter(Router *gin.RouterGroup) {
fileRouter.POST("/mode", baseApi.ChangeFileMode)
fileRouter.POST("/owner", baseApi.ChangeFileOwner)
fileRouter.POST("/compress", baseApi.CompressFile)
fileRouter.POST("/compress/stop", baseApi.StopCompressFile)
fileRouter.POST("/decompress", baseApi.DeCompressFile)
fileRouter.POST("/content", baseApi.GetContent)
fileRouter.POST("/preview", baseApi.PreviewContent)
Expand Down
41 changes: 39 additions & 2 deletions agent/utils/files/archiver.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package files

import (
"context"

"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
)

type ShellArchiver interface {
Extract(filePath, dstDir string, secret string) error
Compress(sourcePaths []string, dstFile string, secret string) error
Compress(ctx context.Context, sourcePaths []string, dstFile string, secret string) error
}

func NewShellArchiver(compressType CompressType) (ShellArchiver, error) {
Expand All @@ -18,14 +20,17 @@ func NewShellArchiver(compressType CompressType) (ShellArchiver, error) {
}
return NewTarArchiver(compressType), nil
case TarGz:
if err := checkCmdAvailability("tar"); err != nil {
return nil, err
}
return NewTarGzArchiver(), nil
case Zip:
if err := checkCmdAvailability("zip"); err != nil {
return nil, err
}
return NewZipArchiver(), nil
case Rar:
if err := checkCmdAvailability("unrar"); err != nil {
if err := checkCmdAvailability("rar"); err != nil {
return nil, err
}
return NewRarArchiver(), nil
Expand All @@ -39,6 +44,38 @@ func NewShellArchiver(compressType CompressType) (ShellArchiver, error) {
}
}

func NewExtractShellArchiver(compressType CompressType) (ShellArchiver, error) {
switch compressType {
case Tar:
if err := checkCmdAvailability("tar"); err != nil {
return nil, err
}
return NewTarArchiver(compressType), nil
case TarGz:
if err := checkCmdAvailability("tar"); err != nil {
return nil, err
}
return NewTarGzArchiver(), nil
case Zip:
if err := checkCmdAvailability("unzip"); err != nil {
return nil, err
}
return NewZipArchiver(), nil
case Rar:
if err := checkCmdAvailability("unrar"); err != nil {
return nil, err
}
return NewRarArchiver(), nil
case X7z:
if err := checkCmdAvailability("7z"); err != nil {
return nil, err
}
return NewX7zArchiver(), nil
default:
return nil, buserr.New("unsupported decompress type")
}
}

func checkCmdAvailability(cmdStr string) error {
if cmd.Which(cmdStr) {
return nil
Expand Down
Loading
Loading