Skip to content

Commit

Permalink
refactor: 重构 tools.Mkdir 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 11, 2023
1 parent ac420de commit 4c41ec0
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 31 deletions.
5 changes: 4 additions & 1 deletion app/console/commands/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion app/http/controllers/plugins/mysql57_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion app/http/controllers/plugins/mysql80_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion app/http/controllers/plugins/postgresql15_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion app/http/controllers/plugins/postgresql16_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion app/http/controllers/plugins/s3fs_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "挂载目录必须为空")
Expand Down
8 changes: 6 additions & 2 deletions app/http/controllers/setting_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions app/http/controllers/website_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
28 changes: 21 additions & 7 deletions app/services/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions app/services/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `<!DOCTYPE html>
<html lang="zh-CN">
Expand Down
13 changes: 2 additions & 11 deletions pkg/tools/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 修改文件/目录权限
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 4c41ec0

Please sign in to comment.