mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
fix: fail to save sync_overwrite flag #518
This commit is contained in:
parent
e93455a302
commit
4179fad640
2 changed files with 135 additions and 125 deletions
|
@ -1,141 +1,143 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/0xJacky/Nginx-UI/api"
|
"github.com/0xJacky/Nginx-UI/api"
|
||||||
"github.com/0xJacky/Nginx-UI/internal/config"
|
"github.com/0xJacky/Nginx-UI/internal/config"
|
||||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||||
"github.com/0xJacky/Nginx-UI/model"
|
"github.com/0xJacky/Nginx-UI/model"
|
||||||
"github.com/0xJacky/Nginx-UI/query"
|
"github.com/0xJacky/Nginx-UI/query"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EditConfigJson struct {
|
type EditConfigJson struct {
|
||||||
Content string `json:"content" binding:"required"`
|
Content string `json:"content" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func EditConfig(c *gin.Context) {
|
func EditConfig(c *gin.Context) {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
var json struct {
|
var json struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Filepath string `json:"filepath" binding:"required"`
|
Filepath string `json:"filepath" binding:"required"`
|
||||||
NewFilepath string `json:"new_filepath" binding:"required"`
|
NewFilepath string `json:"new_filepath" binding:"required"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
SyncOverwrite bool `json:"sync_overwrite"`
|
SyncOverwrite bool `json:"sync_overwrite"`
|
||||||
SyncNodeIds []int `json:"sync_node_ids"`
|
SyncNodeIds []int `json:"sync_node_ids"`
|
||||||
}
|
}
|
||||||
if !api.BindAndValid(c, &json) {
|
if !api.BindAndValid(c, &json) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
path := json.Filepath
|
path := json.Filepath
|
||||||
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
|
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
|
||||||
c.JSON(http.StatusForbidden, gin.H{
|
c.JSON(http.StatusForbidden, gin.H{
|
||||||
"message": "filepath is not under the nginx conf path",
|
"message": "filepath is not under the nginx conf path",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) {
|
if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) {
|
||||||
c.JSON(http.StatusForbidden, gin.H{
|
c.JSON(http.StatusForbidden, gin.H{
|
||||||
"message": "new filepath is not under the nginx conf path",
|
"message": "new filepath is not under the nginx conf path",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !helper.FileExists(path) {
|
if !helper.FileExists(path) {
|
||||||
c.JSON(http.StatusNotFound, gin.H{
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
"message": "file not found",
|
"message": "file not found",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content := json.Content
|
content := json.Content
|
||||||
origContent, err := os.ReadFile(path)
|
origContent, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if content != "" && content != string(origContent) {
|
if content != "" && content != string(origContent) {
|
||||||
err = os.WriteFile(path, []byte(content), 0644)
|
err = os.WriteFile(path, []byte(content), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
q := query.Config
|
q := query.Config
|
||||||
cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate()
|
cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = q.Where(q.Filepath.Eq(json.Filepath)).Updates(&model.Config{
|
_, err = q.Where(q.Filepath.Eq(json.Filepath)).
|
||||||
Name: json.Name,
|
Select(q.Name, q.Filepath, q.SyncNodeIds, q.SyncOverwrite).
|
||||||
Filepath: json.NewFilepath,
|
Updates(&model.Config{
|
||||||
SyncNodeIds: json.SyncNodeIds,
|
Name: json.Name,
|
||||||
SyncOverwrite: json.SyncOverwrite,
|
Filepath: json.NewFilepath,
|
||||||
})
|
SyncNodeIds: json.SyncNodeIds,
|
||||||
|
SyncOverwrite: json.SyncOverwrite,
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
g := query.ChatGPTLog
|
g := query.ChatGPTLog
|
||||||
// handle rename
|
// handle rename
|
||||||
if path != json.NewFilepath {
|
if path != json.NewFilepath {
|
||||||
if helper.FileExists(json.NewFilepath) {
|
if helper.FileExists(json.NewFilepath) {
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
"message": "File exists",
|
"message": "File exists",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := os.Rename(json.Filepath, json.NewFilepath)
|
err := os.Rename(json.Filepath, json.NewFilepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// update ChatGPT record
|
// update ChatGPT record
|
||||||
_, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
|
_, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
|
||||||
_, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
|
_, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = config.SyncToRemoteServer(cfg, json.NewFilepath)
|
err = config.SyncToRemoteServer(cfg, json.NewFilepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
output := nginx.Reload()
|
output := nginx.Reload()
|
||||||
if nginx.GetLogLevel(output) >= nginx.Warn {
|
if nginx.GetLogLevel(output) >= nginx.Warn {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"message": output,
|
"message": output,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
|
chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if chatgpt.Content == nil {
|
if chatgpt.Content == nil {
|
||||||
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
|
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, config.Config{
|
c.JSON(http.StatusOK, config.Config{
|
||||||
Name: name,
|
Name: name,
|
||||||
Content: content,
|
Content: content,
|
||||||
ChatGPTMessages: chatgpt.Content,
|
ChatGPTMessages: chatgpt.Content,
|
||||||
FilePath: json.NewFilepath,
|
FilePath: json.NewFilepath,
|
||||||
ModifiedAt: time.Now(),
|
ModifiedAt: time.Now(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,10 @@ func newConfig(db *gorm.DB, opts ...gen.DOOption) config {
|
||||||
_config.CreatedAt = field.NewTime(tableName, "created_at")
|
_config.CreatedAt = field.NewTime(tableName, "created_at")
|
||||||
_config.UpdatedAt = field.NewTime(tableName, "updated_at")
|
_config.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||||
_config.DeletedAt = field.NewField(tableName, "deleted_at")
|
_config.DeletedAt = field.NewField(tableName, "deleted_at")
|
||||||
|
_config.Name = field.NewString(tableName, "name")
|
||||||
_config.Filepath = field.NewString(tableName, "filepath")
|
_config.Filepath = field.NewString(tableName, "filepath")
|
||||||
_config.SyncNodeIds = field.NewField(tableName, "sync_node_ids")
|
_config.SyncNodeIds = field.NewField(tableName, "sync_node_ids")
|
||||||
|
_config.SyncOverwrite = field.NewBool(tableName, "sync_overwrite")
|
||||||
|
|
||||||
_config.fillFieldMap()
|
_config.fillFieldMap()
|
||||||
|
|
||||||
|
@ -43,13 +45,15 @@ func newConfig(db *gorm.DB, opts ...gen.DOOption) config {
|
||||||
type config struct {
|
type config struct {
|
||||||
configDo
|
configDo
|
||||||
|
|
||||||
ALL field.Asterisk
|
ALL field.Asterisk
|
||||||
ID field.Int
|
ID field.Int
|
||||||
CreatedAt field.Time
|
CreatedAt field.Time
|
||||||
UpdatedAt field.Time
|
UpdatedAt field.Time
|
||||||
DeletedAt field.Field
|
DeletedAt field.Field
|
||||||
Filepath field.String
|
Name field.String
|
||||||
SyncNodeIds field.Field
|
Filepath field.String
|
||||||
|
SyncNodeIds field.Field
|
||||||
|
SyncOverwrite field.Bool
|
||||||
|
|
||||||
fieldMap map[string]field.Expr
|
fieldMap map[string]field.Expr
|
||||||
}
|
}
|
||||||
|
@ -70,8 +74,10 @@ func (c *config) updateTableName(table string) *config {
|
||||||
c.CreatedAt = field.NewTime(table, "created_at")
|
c.CreatedAt = field.NewTime(table, "created_at")
|
||||||
c.UpdatedAt = field.NewTime(table, "updated_at")
|
c.UpdatedAt = field.NewTime(table, "updated_at")
|
||||||
c.DeletedAt = field.NewField(table, "deleted_at")
|
c.DeletedAt = field.NewField(table, "deleted_at")
|
||||||
|
c.Name = field.NewString(table, "name")
|
||||||
c.Filepath = field.NewString(table, "filepath")
|
c.Filepath = field.NewString(table, "filepath")
|
||||||
c.SyncNodeIds = field.NewField(table, "sync_node_ids")
|
c.SyncNodeIds = field.NewField(table, "sync_node_ids")
|
||||||
|
c.SyncOverwrite = field.NewBool(table, "sync_overwrite")
|
||||||
|
|
||||||
c.fillFieldMap()
|
c.fillFieldMap()
|
||||||
|
|
||||||
|
@ -88,13 +94,15 @@ func (c *config) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *config) fillFieldMap() {
|
func (c *config) fillFieldMap() {
|
||||||
c.fieldMap = make(map[string]field.Expr, 6)
|
c.fieldMap = make(map[string]field.Expr, 8)
|
||||||
c.fieldMap["id"] = c.ID
|
c.fieldMap["id"] = c.ID
|
||||||
c.fieldMap["created_at"] = c.CreatedAt
|
c.fieldMap["created_at"] = c.CreatedAt
|
||||||
c.fieldMap["updated_at"] = c.UpdatedAt
|
c.fieldMap["updated_at"] = c.UpdatedAt
|
||||||
c.fieldMap["deleted_at"] = c.DeletedAt
|
c.fieldMap["deleted_at"] = c.DeletedAt
|
||||||
|
c.fieldMap["name"] = c.Name
|
||||||
c.fieldMap["filepath"] = c.Filepath
|
c.fieldMap["filepath"] = c.Filepath
|
||||||
c.fieldMap["sync_node_ids"] = c.SyncNodeIds
|
c.fieldMap["sync_node_ids"] = c.SyncNodeIds
|
||||||
|
c.fieldMap["sync_overwrite"] = c.SyncOverwrite
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c config) clone(db *gorm.DB) config {
|
func (c config) clone(db *gorm.DB) config {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue