From 8bb07cf4f5a300e9bb7b59290db10926d487d564 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:08:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=A1=E5=88=92=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=E6=B8=85=E7=90=86=20(#2592?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #2564 --- backend/app/service/cronjob_helper.go | 9 + backend/app/service/setting.go | 1 + backend/app/service/setting_clean.go | 235 ++++++++++++++----- frontend/src/views/cronjob/operate/index.vue | 1 + 4 files changed, 183 insertions(+), 63 deletions(-) diff --git a/backend/app/service/cronjob_helper.go b/backend/app/service/cronjob_helper.go index c41c39f4b..b0212b45a 100644 --- a/backend/app/service/cronjob_helper.go +++ b/backend/app/service/cronjob_helper.go @@ -64,6 +64,11 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) { if err != nil { global.LOG.Errorf("cut website log file failed, err: %v", err) } + case "clean": + messageItem := "" + messageItem, err = u.handleSystemClean() + message = []byte(messageItem) + u.HandleRmExpired("LOCAL", "", "", cronjob, nil) } if err != nil { @@ -584,3 +589,7 @@ func (u *CronjobService) handleSnapshot(cronjob *model.Cronjob, startTime time.T u.HandleRmExpired(backup.Type, backup.BackupPath, "", cronjob, client) return message, path, nil } + +func (u *CronjobService) handleSystemClean() (string, error) { + return NewISettingService().SystemCleanForCronjob() +} diff --git a/backend/app/service/setting.go b/backend/app/service/setting.go index 7decd5caf..4f3ae4ef4 100644 --- a/backend/app/service/setting.go +++ b/backend/app/service/setting.go @@ -41,6 +41,7 @@ type ISettingService interface { SystemScan() dto.CleanData SystemClean(req []dto.Clean) + SystemCleanForCronjob() (string, error) } func NewISettingService() ISettingService { diff --git a/backend/app/service/setting_clean.go b/backend/app/service/setting_clean.go index 67bd52c4f..be557615d 100644 --- a/backend/app/service/setting_clean.go +++ b/backend/app/service/setting_clean.go @@ -11,10 +11,28 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/cmd" + "github.com/1Panel-dev/1Panel/backend/utils/common" fileUtils "github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/google/uuid" ) +const ( + upgradePath = "1panel/tmp/upgrade" + snapshotTmpPath = "1panel/tmp/system" + rollbackPath = "1panel/tmp" + cachePath = "1panel/cache" + oldOriginalPath = "original" + oldAppBackupPath = "1panel/resource/apps_bak" + oldDownloadPath = "1panel/tmp/download" + oldUpgradePath = "1panel/tmp" + tmpUploadPath = "1panel/tmp/upload" + uploadPath = "1panel/uploads" + downloadPath = "1panel/download" + logPath = "1panel/log" + dockerLogPath = "1panel/tmp/docker_logs" + taskPath = "1panel/task" +) + func (u *SettingService) SystemScan() dto.CleanData { var ( SystemClean dto.CleanData @@ -34,7 +52,7 @@ func (u *SettingService) SystemScan() dto.CleanData { Children: loadTreeWithDir(true, "1panel_original", originalPath, fileOp), }) - upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade") + upgradePath := path.Join(global.CONF.System.BaseDir, upgradePath) upgradeSize, _ := fileOp.GetDirSize(upgradePath) treeData = append(treeData, dto.CleanTree{ ID: uuid.NewString(), @@ -76,7 +94,7 @@ func (u *SettingService) SystemScan() dto.CleanData { Children: rollBackTree, }) - cachePath := path.Join(global.CONF.System.BaseDir, "1panel/cache") + cachePath := path.Join(global.CONF.System.BaseDir, cachePath) cacheSize, _ := fileOp.GetDirSize(cachePath) treeData = append(treeData, dto.CleanTree{ ID: uuid.NewString(), @@ -125,94 +143,94 @@ func (u *SettingService) SystemClean(req []dto.Clean) { dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel_original", item.Name)) case "upgrade": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, upgradePath, item.Name)) case "snapshot": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/system", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, snapshotTmpPath, item.Name)) dropFileOrDir(path.Join(global.CONF.System.Backup, "system", item.Name)) case "snapshot_tmp": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/system", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, snapshotTmpPath, item.Name)) case "snapshot_local": dropFileOrDir(path.Join(global.CONF.System.Backup, "system", item.Name)) case "rollback": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/app")) - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/database")) - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/website")) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "app")) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "database")) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "website")) case "rollback_app": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/app", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "app", item.Name)) case "rollback_database": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/database", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "database", item.Name)) case "rollback_website": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/website", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "website", item.Name)) case "cache": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/cache", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, cachePath, item.Name)) restart = true case "unused": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "original")) - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/resource/apps_bak")) - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/download")) - files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp")) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldOriginalPath)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldAppBackupPath)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldDownloadPath)) + files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath)) if len(files) == 0 { continue } for _, file := range files { if strings.HasPrefix(file.Name(), "upgrade_") { - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp", file.Name())) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath, file.Name())) } } case "old_original": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "original", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldOriginalPath, item.Name)) case "old_apps_bak": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/resource/apps_bak", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldAppBackupPath, item.Name)) case "old_download": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/download", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldDownloadPath, item.Name)) case "old_upgrade": if len(item.Name) == 0 { - files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp")) + files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath)) if len(files) == 0 { continue } for _, file := range files { if strings.HasPrefix(file.Name(), "upgrade_") { - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp", file.Name())) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath, file.Name())) } } } else { - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath, item.Name)) } case "upload": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, item.Name)) if len(item.Name) == 0 { - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/upload")) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, tmpUploadPath)) } case "upload_tmp": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/upload", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, tmpUploadPath, item.Name)) case "upload_app": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads/app", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, "app", item.Name)) case "upload_database": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads/database", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, "database", item.Name)) case "upload_website": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads/website", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, "website", item.Name)) case "upload_directory": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads/directory", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, "directory", item.Name)) case "download": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, item.Name)) case "download_app": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download/app", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, "app", item.Name)) case "download_database": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download/database", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, "database", item.Name)) case "download_website": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download/website", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, "website", item.Name)) case "download_directory": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download/directory", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, "directory", item.Name)) case "system_log": if len(item.Name) == 0 { - files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, "1panel/log")) + files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, logPath)) if len(files) == 0 { continue } @@ -220,16 +238,16 @@ func (u *SettingService) SystemClean(req []dto.Clean) { if file.Name() == "1Panel.log" { continue } - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/log", file.Name())) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, logPath, file.Name())) } } else { - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/log", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, logPath, item.Name)) } case "docker_log": - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/docker_logs", item.Name)) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, dockerLogPath, item.Name)) case "task_log": - pathItem := path.Join(global.CONF.System.BaseDir, "1panel/task", item.Name) - dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/task", item.Name)) + pathItem := path.Join(global.CONF.System.BaseDir, taskPath, item.Name) + dropFileOrDir(path.Join(global.CONF.System.BaseDir, taskPath, item.Name)) if len(item.Name) == 0 { files, _ := os.ReadDir(pathItem) if len(files) == 0 { @@ -258,9 +276,70 @@ func (u *SettingService) SystemClean(req []dto.Clean) { } } +func (u *SettingService) SystemCleanForCronjob() (string, error) { + logs := "" + size := int64(0) + fileCount := 0 + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, "1panel_original"), &logs, &size, &fileCount) + + upgradePath := path.Join(global.CONF.System.BaseDir, upgradePath) + upgradeFiles, _ := os.ReadDir(upgradePath) + if len(upgradeFiles) != 0 { + sort.Slice(upgradeFiles, func(i, j int) bool { + return upgradeFiles[i].Name() > upgradeFiles[j].Name() + }) + for i := 0; i < len(upgradeFiles); i++ { + if i != 0 { + dropFileOrDirWithLog(path.Join(upgradePath, upgradeFiles[i].Name()), &logs, &size, &fileCount) + } + } + } + + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, snapshotTmpPath), &logs, &size, &fileCount) + dropFileOrDirWithLog(path.Join(global.CONF.System.Backup, "system"), &logs, &size, &fileCount) + + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, rollbackPath, "app"), &logs, &size, &fileCount) + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, rollbackPath, "website"), &logs, &size, &fileCount) + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, rollbackPath, "database"), &logs, &size, &fileCount) + + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, oldOriginalPath), &logs, &size, &fileCount) + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, oldAppBackupPath), &logs, &size, &fileCount) + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, oldDownloadPath), &logs, &size, &fileCount) + oldUpgradePath := path.Join(global.CONF.System.BaseDir, oldUpgradePath) + oldUpgradeFiles, _ := os.ReadDir(oldUpgradePath) + if len(oldUpgradeFiles) != 0 { + for i := 0; i < len(oldUpgradeFiles); i++ { + dropFileOrDirWithLog(path.Join(oldUpgradePath, oldUpgradeFiles[i].Name()), &logs, &size, &fileCount) + } + } + + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, tmpUploadPath), &logs, &size, &fileCount) + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, uploadPath), &logs, &size, &fileCount) + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, downloadPath), &logs, &size, &fileCount) + + logPath := path.Join(global.CONF.System.BaseDir, logPath) + logFiles, _ := os.ReadDir(logPath) + if len(logFiles) != 0 { + for i := 0; i < len(logFiles); i++ { + if logFiles[i].Name() != "1Panel.log" { + dropFileOrDirWithLog(path.Join(logPath, logFiles[i].Name()), &logs, &size, &fileCount) + } + } + } + timeNow := time.Now().Format("2006-01-02 15:04:05") + logs += fmt.Sprintf("%s: total clean: %s, total count: %d", timeNow, common.LoadSizeUnit2F(float64(size)), fileCount) + dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, dockerLogPath), &logs, &size, &fileCount) + + _ = settingRepo.Update("LastCleanTime", timeNow) + _ = settingRepo.Update("LastCleanSize", fmt.Sprintf("%v", size)) + _ = settingRepo.Update("LastCleanData", fmt.Sprintf("%v", fileCount)) + + return logs, nil +} + func loadSnapshotTree(fileOp fileUtils.FileOp) []dto.CleanTree { var treeData []dto.CleanTree - path1 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/system") + path1 := path.Join(global.CONF.System.BaseDir, snapshotTmpPath) list1 := loadTreeWithAllFile(true, path1, "snapshot_tmp", path1, fileOp) if len(list1) != 0 { size, _ := fileOp.GetDirSize(path1) @@ -278,17 +357,17 @@ func loadSnapshotTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadRollBackTree(fileOp fileUtils.FileOp) []dto.CleanTree { var treeData []dto.CleanTree - path1 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/app") + path1 := path.Join(global.CONF.System.BaseDir, rollbackPath, "app") list1 := loadTreeWithAllFile(true, path1, "rollback_app", path1, fileOp) size1, _ := fileOp.GetDirSize(path1) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_app", Size: uint64(size1), Children: list1, Type: "rollback_app", IsRecommend: true}) - path2 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/website") + path2 := path.Join(global.CONF.System.BaseDir, rollbackPath, "website") list2 := loadTreeWithAllFile(true, path2, "rollback_website", path2, fileOp) size2, _ := fileOp.GetDirSize(path2) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_website", Size: uint64(size2), Children: list2, Type: "rollback_website", IsRecommend: true}) - path3 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/database") + path3 := path.Join(global.CONF.System.BaseDir, rollbackPath, "database") list3 := loadTreeWithAllFile(true, path3, "rollback_database", path3, fileOp) size3, _ := fileOp.GetDirSize(path3) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_database", Size: uint64(size3), Children: list3, Type: "rollback_database", IsRecommend: true}) @@ -298,28 +377,28 @@ func loadRollBackTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadUnusedFile(fileOp fileUtils.FileOp) []dto.CleanTree { var treeData []dto.CleanTree - path1 := path.Join(global.CONF.System.BaseDir, "original") + path1 := path.Join(global.CONF.System.BaseDir, oldOriginalPath) list1 := loadTreeWithAllFile(true, path1, "old_original", path1, fileOp) if len(list1) != 0 { size, _ := fileOp.GetDirSize(path1) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_original", Size: uint64(size), Children: list1, Type: "old_original"}) } - path2 := path.Join(global.CONF.System.BaseDir, "1panel/resource/apps_bak") + path2 := path.Join(global.CONF.System.BaseDir, oldAppBackupPath) list2 := loadTreeWithAllFile(true, path2, "old_apps_bak", path2, fileOp) if len(list2) != 0 { size, _ := fileOp.GetDirSize(path2) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_apps_bak", Size: uint64(size), Children: list2, Type: "old_apps_bak"}) } - path3 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/download") + path3 := path.Join(global.CONF.System.BaseDir, oldDownloadPath) list3 := loadTreeWithAllFile(true, path3, "old_download", path3, fileOp) if len(list3) != 0 { size, _ := fileOp.GetDirSize(path3) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_download", Size: uint64(size), Children: list3, Type: "old_download"}) } - path4 := path.Join(global.CONF.System.BaseDir, "1panel/tmp") + path4 := path.Join(global.CONF.System.BaseDir, oldUpgradePath) list4 := loadTreeWithDir(true, "old_upgrade", path4, fileOp) itemSize := uint64(0) for _, item := range list4 { @@ -334,32 +413,32 @@ func loadUnusedFile(fileOp fileUtils.FileOp) []dto.CleanTree { func loadUploadTree(fileOp fileUtils.FileOp) []dto.CleanTree { var treeData []dto.CleanTree - path0 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upload") + path0 := path.Join(global.CONF.System.BaseDir, tmpUploadPath) list0 := loadTreeWithAllFile(true, path0, "upload_tmp", path0, fileOp) size0, _ := fileOp.GetDirSize(path0) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_tmp", Size: uint64(size0), Children: list0, Type: "upload_tmp", IsRecommend: true}) - path1 := path.Join(global.CONF.System.BaseDir, "1panel/uploads/app") + path1 := path.Join(global.CONF.System.BaseDir, uploadPath, "app") list1 := loadTreeWithAllFile(true, path1, "upload_app", path1, fileOp) size1, _ := fileOp.GetDirSize(path1) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_app", Size: uint64(size1), Children: list1, Type: "upload_app", IsRecommend: true}) - path2 := path.Join(global.CONF.System.BaseDir, "1panel/uploads/website") + path2 := path.Join(global.CONF.System.BaseDir, uploadPath, "website") list2 := loadTreeWithAllFile(true, path2, "upload_website", path2, fileOp) size2, _ := fileOp.GetDirSize(path2) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_website", Size: uint64(size2), Children: list2, Type: "upload_website", IsRecommend: true}) - path3 := path.Join(global.CONF.System.BaseDir, "1panel/uploads/database") + path3 := path.Join(global.CONF.System.BaseDir, uploadPath, "database") list3 := loadTreeWithAllFile(true, path3, "upload_database", path3, fileOp) size3, _ := fileOp.GetDirSize(path3) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_database", Size: uint64(size3), Children: list3, Type: "upload_database", IsRecommend: true}) - path4 := path.Join(global.CONF.System.BaseDir, "1panel/uploads/directory") + path4 := path.Join(global.CONF.System.BaseDir, uploadPath, "directory") list4 := loadTreeWithAllFile(true, path4, "upload_directory", path4, fileOp) size4, _ := fileOp.GetDirSize(path4) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_directory", Size: uint64(size4), Children: list4, Type: "upload_directory", IsRecommend: true}) - path5 := path.Join(global.CONF.System.BaseDir, "1panel/uploads") + path5 := path.Join(global.CONF.System.BaseDir, uploadPath) uploadTreeData := loadTreeWithAllFile(true, path5, "upload", path5, fileOp) treeData = append(treeData, uploadTreeData...) @@ -368,27 +447,27 @@ func loadUploadTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadDownloadTree(fileOp fileUtils.FileOp) []dto.CleanTree { var treeData []dto.CleanTree - path1 := path.Join(global.CONF.System.BaseDir, "1panel/download/app") + path1 := path.Join(global.CONF.System.BaseDir, downloadPath, "app") list1 := loadTreeWithAllFile(true, path1, "download_app", path1, fileOp) size1, _ := fileOp.GetDirSize(path1) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_app", Size: uint64(size1), Children: list1, Type: "download_app", IsRecommend: true}) - path2 := path.Join(global.CONF.System.BaseDir, "1panel/download/website") + path2 := path.Join(global.CONF.System.BaseDir, downloadPath, "website") list2 := loadTreeWithAllFile(true, path2, "download_website", path2, fileOp) size2, _ := fileOp.GetDirSize(path2) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_website", Size: uint64(size2), Children: list2, Type: "download_website", IsRecommend: true}) - path3 := path.Join(global.CONF.System.BaseDir, "1panel/download/database") + path3 := path.Join(global.CONF.System.BaseDir, downloadPath, "database") list3 := loadTreeWithAllFile(true, path3, "download_database", path3, fileOp) size3, _ := fileOp.GetDirSize(path3) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_database", Size: uint64(size3), Children: list3, Type: "download_database", IsRecommend: true}) - path4 := path.Join(global.CONF.System.BaseDir, "1panel/download/directory") + path4 := path.Join(global.CONF.System.BaseDir, downloadPath, "directory") list4 := loadTreeWithAllFile(true, path4, "download_directory", path4, fileOp) size4, _ := fileOp.GetDirSize(path4) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_directory", Size: uint64(size4), Children: list4, Type: "download_directory", IsRecommend: true}) - path5 := path.Join(global.CONF.System.BaseDir, "1panel/download") + path5 := path.Join(global.CONF.System.BaseDir, downloadPath) uploadTreeData := loadTreeWithAllFile(true, path5, "download", path5, fileOp) treeData = append(treeData, uploadTreeData...) @@ -397,7 +476,7 @@ func loadDownloadTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadLogTree(fileOp fileUtils.FileOp) []dto.CleanTree { var treeData []dto.CleanTree - path1 := path.Join(global.CONF.System.BaseDir, "1panel/log") + path1 := path.Join(global.CONF.System.BaseDir, logPath) list1 := loadTreeWithAllFile(true, path1, "system_log", path1, fileOp) size := uint64(0) for _, file := range list1 { @@ -405,12 +484,12 @@ func loadLogTree(fileOp fileUtils.FileOp) []dto.CleanTree { } treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "system_log", Size: uint64(size), Children: list1, Type: "system_log", IsRecommend: true}) - path2 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/docker_logs") + path2 := path.Join(global.CONF.System.BaseDir, dockerLogPath) list2 := loadTreeWithAllFile(true, path2, "docker_log", path2, fileOp) size2, _ := fileOp.GetDirSize(path2) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "docker_log", Size: uint64(size2), Children: list2, Type: "docker_log", IsRecommend: true}) - path3 := path.Join(global.CONF.System.BaseDir, "1panel/task") + path3 := path.Join(global.CONF.System.BaseDir, taskPath) list3 := loadTreeWithAllFile(false, path3, "task_log", path3, fileOp) size3, _ := fileOp.GetDirSize(path3) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "task_log", Size: uint64(size3), Children: list3, Type: "task_log"}) @@ -506,3 +585,33 @@ func dropFileOrDir(itemPath string) { global.LOG.Errorf("drop file %s failed, err %v", itemPath, err) } } + +func dropFileOrDirWithLog(itemPath string, log *string, size *int64, count *int) { + itemSize := int64(0) + itemCount := 0 + scanFile(itemPath, &itemSize, &itemCount) + *size += itemSize + *count += itemCount + if err := os.RemoveAll(itemPath); err != nil { + global.LOG.Errorf("drop file %s failed, err %v", itemPath, err) + *log += fmt.Sprintf("- drop file %s failed, err: %v \n\n", itemPath, err) + return + } + *log += fmt.Sprintf("+ drop file %s successful!, size: %s, count: %d \n\n", itemPath, common.LoadSizeUnit2F(float64(itemSize)), itemCount) +} + +func scanFile(pathItem string, size *int64, count *int) { + files, _ := os.ReadDir(pathItem) + for _, f := range files { + if f.IsDir() { + scanFile(path.Join(pathItem, f.Name()), size, count) + } else { + fileInfo, err := f.Info() + if err != nil { + continue + } + *count++ + *size += fileInfo.Size() + } + } +} diff --git a/frontend/src/views/cronjob/operate/index.vue b/frontend/src/views/cronjob/operate/index.vue index ad9afd2e9..1616d2474 100644 --- a/frontend/src/views/cronjob/operate/index.vue +++ b/frontend/src/views/cronjob/operate/index.vue @@ -27,6 +27,7 @@ + {{ $t('cronjob.' + dialogData.rowData!.type) }}