From 4c41ec036cc282670b108e5a4cb0e27101251c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sun, 12 Nov 2023 03:09:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20tools.Mkdir=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/console/commands/panel.go | 5 +++- .../controllers/plugins/mysql57_controller.go | 4 ++- .../controllers/plugins/mysql80_controller.go | 4 ++- .../plugins/postgresql15_controller.go | 4 ++- .../plugins/postgresql16_controller.go | 4 ++- .../controllers/plugins/s3fs_controller.go | 4 ++- app/http/controllers/setting_controller.go | 8 ++++-- app/http/controllers/website_controller.go | 8 ++++-- app/services/backup.go | 28 ++++++++++++++----- app/services/website.go | 6 ++-- pkg/tools/system.go | 13 ++------- pkg/tools/system_test.go | 2 +- 12 files changed, 59 insertions(+), 31 deletions(-) diff --git a/app/console/commands/panel.go b/app/console/commands/panel.go index 283d8185..ffa9dc32 100644 --- a/app/console/commands/panel.go +++ b/app/console/commands/panel.go @@ -228,7 +228,10 @@ func (receiver *Panel) Handle(ctx console.Context) error { color.Greenln(hr) if !tools.Exists(path) { - tools.Mkdir(path, 0644) + if err := tools.Mkdir(path, 0644); err != nil { + color.Redln("|-创建备份目录失败: " + err.Error()) + return nil + } } switch backupType { diff --git a/app/http/controllers/plugins/mysql57_controller.go b/app/http/controllers/plugins/mysql57_controller.go index 49f8778e..6a457018 100644 --- a/app/http/controllers/plugins/mysql57_controller.go +++ b/app/http/controllers/plugins/mysql57_controller.go @@ -501,7 +501,9 @@ func (r *Mysql57Controller) UploadBackup(ctx http.Context) http.Response { backupPath := r.setting.Get(models.SettingKeyBackupPath) + "/mysql" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err = tools.Mkdir(backupPath, 0644); err != nil { + return nil + } } name := file.GetClientOriginalName() diff --git a/app/http/controllers/plugins/mysql80_controller.go b/app/http/controllers/plugins/mysql80_controller.go index 84f365e8..729fd78d 100644 --- a/app/http/controllers/plugins/mysql80_controller.go +++ b/app/http/controllers/plugins/mysql80_controller.go @@ -501,7 +501,9 @@ func (r *Mysql80Controller) UploadBackup(ctx http.Context) http.Response { backupPath := r.setting.Get(models.SettingKeyBackupPath) + "/mysql" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err = tools.Mkdir(backupPath, 0644); err != nil { + return nil + } } name := file.GetClientOriginalName() diff --git a/app/http/controllers/plugins/postgresql15_controller.go b/app/http/controllers/plugins/postgresql15_controller.go index 8c635070..7738051a 100644 --- a/app/http/controllers/plugins/postgresql15_controller.go +++ b/app/http/controllers/plugins/postgresql15_controller.go @@ -411,7 +411,9 @@ func (r *Postgresql15Controller) UploadBackup(ctx http.Context) http.Response { backupPath := r.setting.Get(models.SettingKeyBackupPath) + "/postgresql" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err = tools.Mkdir(backupPath, 0644); err != nil { + return nil + } } name := file.GetClientOriginalName() diff --git a/app/http/controllers/plugins/postgresql16_controller.go b/app/http/controllers/plugins/postgresql16_controller.go index 17f108c3..a5810f90 100644 --- a/app/http/controllers/plugins/postgresql16_controller.go +++ b/app/http/controllers/plugins/postgresql16_controller.go @@ -411,7 +411,9 @@ func (r *Postgresql16Controller) UploadBackup(ctx http.Context) http.Response { backupPath := r.setting.Get(models.SettingKeyBackupPath) + "/postgresql" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err = tools.Mkdir(backupPath, 0644); err != nil { + return nil + } } name := file.GetClientOriginalName() diff --git a/app/http/controllers/plugins/s3fs_controller.go b/app/http/controllers/plugins/s3fs_controller.go index dc1c43d1..a74f2f10 100644 --- a/app/http/controllers/plugins/s3fs_controller.go +++ b/app/http/controllers/plugins/s3fs_controller.go @@ -95,7 +95,9 @@ func (r *S3fsController) Add(ctx http.Context) http.Response { // 检查挂载目录是否存在且为空 if !tools.Exists(path) { - tools.Mkdir(path, 0755) + if err = tools.Mkdir(path, 0755); err != nil { + return controllers.Error(ctx, http.StatusUnprocessableEntity, "挂载目录创建失败") + } } if !tools.Empty(path) { return controllers.Error(ctx, http.StatusUnprocessableEntity, "挂载目录必须为空") diff --git a/app/http/controllers/setting_controller.go b/app/http/controllers/setting_controller.go index e7f7cb3e..3dda19dd 100644 --- a/app/http/controllers/setting_controller.go +++ b/app/http/controllers/setting_controller.go @@ -86,7 +86,9 @@ func (r *SettingController) Update(ctx http.Context) http.Response { } if !tools.Exists(updateRequest.BackupPath) { - tools.Mkdir(updateRequest.BackupPath, 0644) + if err = tools.Mkdir(updateRequest.BackupPath, 0644); err != nil { + return ErrorSystem(ctx) + } } err = r.setting.Set(models.SettingKeyBackupPath, updateRequest.BackupPath) if err != nil { @@ -96,7 +98,9 @@ func (r *SettingController) Update(ctx http.Context) http.Response { return ErrorSystem(ctx) } if !tools.Exists(updateRequest.WebsitePath) { - tools.Mkdir(updateRequest.WebsitePath, 0755) + if err = tools.Mkdir(updateRequest.WebsitePath, 0755); err != nil { + return ErrorSystem(ctx) + } tools.Chown(updateRequest.WebsitePath, "www", "www") } err = r.setting.Set(models.SettingKeyWebsitePath, updateRequest.WebsitePath) diff --git a/app/http/controllers/website_controller.go b/app/http/controllers/website_controller.go index b1e0e1a9..80a9868e 100644 --- a/app/http/controllers/website_controller.go +++ b/app/http/controllers/website_controller.go @@ -417,7 +417,9 @@ func (c *WebsiteController) UploadBackup(ctx http.Context) http.Response { backupPath := c.setting.Get(models.SettingKeyBackupPath) + "/website" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err = tools.Mkdir(backupPath, 0644); err != nil { + return nil + } } name := file.GetClientOriginalName() @@ -487,7 +489,9 @@ func (c *WebsiteController) DeleteBackup(ctx http.Context) http.Response { backupPath := c.setting.Get(models.SettingKeyBackupPath) + "/website" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err := tools.Mkdir(backupPath, 0644); err != nil { + return nil + } } if !tools.Remove(backupPath + "/" + deleteBackupRequest.Name) { diff --git a/app/services/backup.go b/app/services/backup.go index 1b6dc141..bf6fbb08 100644 --- a/app/services/backup.go +++ b/app/services/backup.go @@ -49,7 +49,9 @@ func (s *BackupImpl) WebsiteList() ([]BackupFile, error) { backupPath += "/website" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err := tools.Mkdir(backupPath, 0644); err != nil { + return []BackupFile{}, err + } } files, err := os.ReadDir(backupPath) @@ -80,7 +82,9 @@ func (s *BackupImpl) WebSiteBackup(website models.Website) error { backupPath += "/website" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err := tools.Mkdir(backupPath, 0644); err != nil { + return err + } } backupFile := backupPath + "/" + website.Name + "_" + carbon.Now().ToShortDateTimeString() + ".zip" @@ -98,7 +102,9 @@ func (s *BackupImpl) WebsiteRestore(website models.Website, backupFile string) e backupPath += "/website" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err := tools.Mkdir(backupPath, 0644); err != nil { + return err + } } backupFile = backupPath + "/" + backupFile @@ -123,7 +129,9 @@ func (s *BackupImpl) MysqlList() ([]BackupFile, error) { backupPath += "/mysql" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err := tools.Mkdir(backupPath, 0644); err != nil { + return []BackupFile{}, err + } } files, err := os.ReadDir(backupPath) @@ -151,7 +159,9 @@ func (s *BackupImpl) MysqlBackup(database string) error { rootPassword := s.setting.Get(models.SettingKeyMysqlRootPassword) backupFile := database + "_" + carbon.Now().ToShortDateTimeString() + ".sql" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err := tools.Mkdir(backupPath, 0644); err != nil { + return err + } } err := os.Setenv("MYSQL_PWD", rootPassword) if err != nil { @@ -225,7 +235,9 @@ func (s *BackupImpl) PostgresqlList() ([]BackupFile, error) { backupPath += "/postgresql" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err := tools.Mkdir(backupPath, 0644); err != nil { + return []BackupFile{}, err + } } files, err := os.ReadDir(backupPath) @@ -252,7 +264,9 @@ func (s *BackupImpl) PostgresqlBackup(database string) error { backupPath := s.setting.Get(models.SettingKeyBackupPath) + "/postgresql" backupFile := database + "_" + carbon.Now().ToShortDateTimeString() + ".sql" if !tools.Exists(backupPath) { - tools.Mkdir(backupPath, 0644) + if err := tools.Mkdir(backupPath, 0644); err != nil { + return err + } } tools.Exec(`su - postgres -c "pg_dump ` + database + `" > ` + backupPath + "/" + backupFile) diff --git a/app/services/website.go b/app/services/website.go index 06d2bf26..f3bbe750 100644 --- a/app/services/website.go +++ b/app/services/website.go @@ -122,10 +122,12 @@ func (r *WebsiteImpl) Add(website PanelWebsite) (models.Website, error) { Remark: website.Remark, } if err := facades.Orm().Query().Create(&w); err != nil { - return w, err + return models.Website{}, err } - tools.Mkdir(website.Path, 0755) + if err := tools.Mkdir(website.Path, 0755); err != nil { + return models.Website{}, err + } index := ` diff --git a/pkg/tools/system.go b/pkg/tools/system.go index 7650ff80..e034e4d5 100644 --- a/pkg/tools/system.go +++ b/pkg/tools/system.go @@ -101,17 +101,8 @@ func ExecAsync(shell string) error { } // Mkdir 创建目录 -func Mkdir(path string, permission os.FileMode) bool { - if err := os.MkdirAll(path, permission); err != nil { - facades.Log().With(map[string]any{ - "path": path, - "permission": permission, - "error": err.Error(), - }).Tags("面板", "工具函数").Info("创建目录失败") - return false - } - - return true +func Mkdir(path string, permission os.FileMode) error { + return os.MkdirAll(path, permission) } // Chmod 修改文件/目录权限 diff --git a/pkg/tools/system_test.go b/pkg/tools/system_test.go index 99cd49c5..6e0679fa 100644 --- a/pkg/tools/system_test.go +++ b/pkg/tools/system_test.go @@ -68,7 +68,7 @@ func (s *SystemHelperTestSuite) TestMkdir() { dirPath := "/tmp/testdir" defer os.RemoveAll(dirPath) - s.True(Mkdir(dirPath, 0755)) + s.Nil(Mkdir(dirPath, 0755)) } func (s *SystemHelperTestSuite) TestChmod() {