fix: fail to create config #724 #720

This commit is contained in:
Jacky 2024-11-15 19:30:03 +08:00
parent 56e82d9c5a
commit a2c0e804a4
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
5 changed files with 19 additions and 25 deletions

View file

@ -18,7 +18,7 @@ import (
func AddConfig(c *gin.Context) {
var json struct {
Name string `json:"name" binding:"required"`
NewFilepath string `json:"new_filepath" binding:"required"`
BaseDir string `json:"base_dir"`
Content string `json:"content"`
Overwrite bool `json:"overwrite"`
SyncNodeIds []uint64 `json:"sync_node_ids"`
@ -30,10 +30,11 @@ func AddConfig(c *gin.Context) {
name := json.Name
content := json.Content
path := json.NewFilepath
dir := nginx.GetConfPath(json.BaseDir)
path := filepath.Join(dir, json.Name)
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
c.JSON(http.StatusForbidden, gin.H{
"message": "new filepath is not under the nginx conf path",
"message": "filepath is not under the nginx conf path",
})
return
}
@ -46,7 +47,6 @@ func AddConfig(c *gin.Context) {
}
// check if the dir exists, if not, use mkdirAll to create the dir
dir := filepath.Dir(path)
if !helper.FileExists(dir) {
err := os.MkdirAll(dir, 0755)
if err != nil {
@ -89,7 +89,7 @@ func AddConfig(c *gin.Context) {
return
}
err = config.SyncToRemoteServer(cfg, json.NewFilepath)
err = config.SyncToRemoteServer(cfg, path)
if err != nil {
api.ErrHandler(c, err)
return

View file

@ -22,7 +22,6 @@ type EditConfigJson struct {
func EditConfig(c *gin.Context) {
relativePath := c.Param("path")
var json struct {
Name string `json:"name" binding:"required"`
Content string `json:"content"`
SyncOverwrite bool `json:"sync_overwrite"`
SyncNodeIds []uint64 `json:"sync_node_ids"`

View file

@ -8,9 +8,9 @@ import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy/logger"
"net/http"
"os"
"path/filepath"
"strings"
)
@ -21,10 +21,11 @@ func Rename(c *gin.Context) {
NewName string `json:"new_name"`
SyncNodeIds []uint64 `json:"sync_node_ids" gorm:"serializer:json"`
}
if !api.BindAndValid(c, &json) {
return
}
logger.Debug(json)
if json.OrigName == json.NewName {
c.JSON(http.StatusOK, gin.H{
"message": "ok",
@ -97,6 +98,6 @@ func Rename(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"path": strings.TrimLeft(newFullPath, nginx.GetConfPath()),
"path": strings.TrimLeft(filepath.Join(json.BasePath, json.NewName), "/"),
})
}