Skip to content

Commit

Permalink
fix: 解决创建 PHP 网站首页无法访问的 BUG (#4577)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 authored Apr 18, 2024
1 parent bb02991 commit b7c3dc0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backend/app/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (f *FileService) Create(op request.FileCreate) error {
}
}
if op.IsDir {
return fo.CreateDir(op.Path, fs.FileMode(mode))
return fo.CreateDirWithMode(op.Path, fs.FileMode(mode))
}
if op.IsLink {
if !fo.Stat(op.LinkPath) {
Expand Down
30 changes: 22 additions & 8 deletions backend/utils/files/file_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,14 @@ func (f FileOp) GetContent(dst string) ([]byte, error) {
}

func (f FileOp) CreateDir(dst string, mode fs.FileMode) error {
return f.Fs.MkdirAll(dst, mode)
}

func (f FileOp) CreateDirWithMode(dst string, mode fs.FileMode) error {
if err := f.Fs.MkdirAll(dst, mode); err != nil {
return err
}
modStr := fmt.Sprintf("%o", mode)

modeInt, err := strconv.ParseInt(modStr, 10, 64)
if err != nil {
modeInt = 0755
}

return f.ChmodR(dst, modeInt, true)
return f.ChmodRWithMode(dst, mode, true)
}

func (f FileOp) CreateFile(dst string) error {
Expand Down Expand Up @@ -193,6 +190,23 @@ func (f FileOp) ChmodR(dst string, mode int64, sub bool) error {
return nil
}

func (f FileOp) ChmodRWithMode(dst string, mode fs.FileMode, sub bool) error {
cmdStr := fmt.Sprintf(`chmod %v "%s"`, fmt.Sprintf("%o", mode.Perm()), dst)
if sub {
cmdStr = fmt.Sprintf(`chmod -R %v "%s"`, fmt.Sprintf("%o", mode.Perm()), dst)
}
if cmd.HasNoPasswordSudo() {
cmdStr = fmt.Sprintf("sudo %s", cmdStr)
}
if msg, err := cmd.ExecWithTimeOut(cmdStr, 10*time.Second); err != nil {
if msg != "" {
return errors.New(msg)
}
return err
}
return nil
}

func (f FileOp) Rename(oldName string, newName string) error {
return f.Fs.Rename(oldName, newName)
}
Expand Down

0 comments on commit b7c3dc0

Please sign in to comment.