2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-08 13:04:29 +08:00

refactor: 重构 tools.Mkdir 函数

This commit is contained in:
耗子
2023-11-12 03:09:41 +08:00
parent ac420debbb
commit 4c41ec036c
12 changed files with 59 additions and 31 deletions

View File

@@ -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 {

View File

@@ -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()

View File

@@ -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()

View File

@@ -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()

View File

@@ -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()

View File

@@ -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, "挂载目录必须为空")

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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)

View File

@@ -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">

View File

@@ -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 修改文件/目录权限

View File

@@ -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() {