Skip to content

Commit 4179fad

Browse files
committed
fix: fail to save sync_overwrite flag #518
1 parent e93455a commit 4179fad

File tree

2 files changed

+149
-139
lines changed

2 files changed

+149
-139
lines changed

api/config/modify.go

Lines changed: 133 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,143 @@
11
package config
22

33
import (
4-
"github.com/0xJacky/Nginx-UI/api"
5-
"github.com/0xJacky/Nginx-UI/internal/config"
6-
"github.com/0xJacky/Nginx-UI/internal/helper"
7-
"github.com/0xJacky/Nginx-UI/internal/nginx"
8-
"github.com/0xJacky/Nginx-UI/model"
9-
"github.com/0xJacky/Nginx-UI/query"
10-
"github.com/gin-gonic/gin"
11-
"github.com/sashabaranov/go-openai"
12-
"net/http"
13-
"os"
14-
"time"
4+
"github.com/0xJacky/Nginx-UI/api"
5+
"github.com/0xJacky/Nginx-UI/internal/config"
6+
"github.com/0xJacky/Nginx-UI/internal/helper"
7+
"github.com/0xJacky/Nginx-UI/internal/nginx"
8+
"github.com/0xJacky/Nginx-UI/model"
9+
"github.com/0xJacky/Nginx-UI/query"
10+
"github.com/gin-gonic/gin"
11+
"github.com/sashabaranov/go-openai"
12+
"net/http"
13+
"os"
14+
"time"
1515
)
1616

1717
type EditConfigJson struct {
18-
Content string `json:"content" binding:"required"`
18+
Content string `json:"content" binding:"required"`
1919
}
2020

2121
func EditConfig(c *gin.Context) {
22-
name := c.Param("name")
23-
var json struct {
24-
Name string `json:"name" binding:"required"`
25-
Filepath string `json:"filepath" binding:"required"`
26-
NewFilepath string `json:"new_filepath" binding:"required"`
27-
Content string `json:"content"`
28-
SyncOverwrite bool `json:"sync_overwrite"`
29-
SyncNodeIds []int `json:"sync_node_ids"`
30-
}
31-
if !api.BindAndValid(c, &json) {
32-
return
33-
}
34-
35-
path := json.Filepath
36-
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
37-
c.JSON(http.StatusForbidden, gin.H{
38-
"message": "filepath is not under the nginx conf path",
39-
})
40-
return
41-
}
42-
43-
if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) {
44-
c.JSON(http.StatusForbidden, gin.H{
45-
"message": "new filepath is not under the nginx conf path",
46-
})
47-
return
48-
}
49-
50-
if !helper.FileExists(path) {
51-
c.JSON(http.StatusNotFound, gin.H{
52-
"message": "file not found",
53-
})
54-
return
55-
}
56-
57-
content := json.Content
58-
origContent, err := os.ReadFile(path)
59-
if err != nil {
60-
api.ErrHandler(c, err)
61-
return
62-
}
63-
64-
if content != "" && content != string(origContent) {
65-
err = os.WriteFile(path, []byte(content), 0644)
66-
if err != nil {
67-
api.ErrHandler(c, err)
68-
return
69-
}
70-
}
71-
72-
q := query.Config
73-
cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate()
74-
if err != nil {
75-
api.ErrHandler(c, err)
76-
return
77-
}
78-
79-
_, err = q.Where(q.Filepath.Eq(json.Filepath)).Updates(&model.Config{
80-
Name: json.Name,
81-
Filepath: json.NewFilepath,
82-
SyncNodeIds: json.SyncNodeIds,
83-
SyncOverwrite: json.SyncOverwrite,
84-
})
85-
86-
if err != nil {
87-
api.ErrHandler(c, err)
88-
return
89-
}
90-
g := query.ChatGPTLog
91-
// handle rename
92-
if path != json.NewFilepath {
93-
if helper.FileExists(json.NewFilepath) {
94-
c.JSON(http.StatusNotAcceptable, gin.H{
95-
"message": "File exists",
96-
})
97-
return
98-
}
99-
err := os.Rename(json.Filepath, json.NewFilepath)
100-
if err != nil {
101-
api.ErrHandler(c, err)
102-
return
103-
}
104-
105-
// update ChatGPT record
106-
_, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
107-
_, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
108-
}
109-
110-
err = config.SyncToRemoteServer(cfg, json.NewFilepath)
111-
if err != nil {
112-
api.ErrHandler(c, err)
113-
return
114-
}
115-
116-
output := nginx.Reload()
117-
if nginx.GetLogLevel(output) >= nginx.Warn {
118-
c.JSON(http.StatusInternalServerError, gin.H{
119-
"message": output,
120-
})
121-
return
122-
}
123-
124-
chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
125-
if err != nil {
126-
api.ErrHandler(c, err)
127-
return
128-
}
129-
130-
if chatgpt.Content == nil {
131-
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
132-
}
133-
134-
c.JSON(http.StatusOK, config.Config{
135-
Name: name,
136-
Content: content,
137-
ChatGPTMessages: chatgpt.Content,
138-
FilePath: json.NewFilepath,
139-
ModifiedAt: time.Now(),
140-
})
22+
name := c.Param("name")
23+
var json struct {
24+
Name string `json:"name" binding:"required"`
25+
Filepath string `json:"filepath" binding:"required"`
26+
NewFilepath string `json:"new_filepath" binding:"required"`
27+
Content string `json:"content"`
28+
SyncOverwrite bool `json:"sync_overwrite"`
29+
SyncNodeIds []int `json:"sync_node_ids"`
30+
}
31+
if !api.BindAndValid(c, &json) {
32+
return
33+
}
34+
35+
path := json.Filepath
36+
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
37+
c.JSON(http.StatusForbidden, gin.H{
38+
"message": "filepath is not under the nginx conf path",
39+
})
40+
return
41+
}
42+
43+
if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) {
44+
c.JSON(http.StatusForbidden, gin.H{
45+
"message": "new filepath is not under the nginx conf path",
46+
})
47+
return
48+
}
49+
50+
if !helper.FileExists(path) {
51+
c.JSON(http.StatusNotFound, gin.H{
52+
"message": "file not found",
53+
})
54+
return
55+
}
56+
57+
content := json.Content
58+
origContent, err := os.ReadFile(path)
59+
if err != nil {
60+
api.ErrHandler(c, err)
61+
return
62+
}
63+
64+
if content != "" && content != string(origContent) {
65+
err = os.WriteFile(path, []byte(content), 0644)
66+
if err != nil {
67+
api.ErrHandler(c, err)
68+
return
69+
}
70+
}
71+
72+
q := query.Config
73+
cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate()
74+
if err != nil {
75+
api.ErrHandler(c, err)
76+
return
77+
}
78+
79+
_, err = q.Where(q.Filepath.Eq(json.Filepath)).
80+
Select(q.Name, q.Filepath, q.SyncNodeIds, q.SyncOverwrite).
81+
Updates(&model.Config{
82+
Name: json.Name,
83+
Filepath: json.NewFilepath,
84+
SyncNodeIds: json.SyncNodeIds,
85+
SyncOverwrite: json.SyncOverwrite,
86+
})
87+
88+
if err != nil {
89+
api.ErrHandler(c, err)
90+
return
91+
}
92+
g := query.ChatGPTLog
93+
// handle rename
94+
if path != json.NewFilepath {
95+
if helper.FileExists(json.NewFilepath) {
96+
c.JSON(http.StatusNotAcceptable, gin.H{
97+
"message": "File exists",
98+
})
99+
return
100+
}
101+
err := os.Rename(json.Filepath, json.NewFilepath)
102+
if err != nil {
103+
api.ErrHandler(c, err)
104+
return
105+
}
106+
107+
// update ChatGPT record
108+
_, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
109+
_, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
110+
}
111+
112+
err = config.SyncToRemoteServer(cfg, json.NewFilepath)
113+
if err != nil {
114+
api.ErrHandler(c, err)
115+
return
116+
}
117+
118+
output := nginx.Reload()
119+
if nginx.GetLogLevel(output) >= nginx.Warn {
120+
c.JSON(http.StatusInternalServerError, gin.H{
121+
"message": output,
122+
})
123+
return
124+
}
125+
126+
chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
127+
if err != nil {
128+
api.ErrHandler(c, err)
129+
return
130+
}
131+
132+
if chatgpt.Content == nil {
133+
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
134+
}
135+
136+
c.JSON(http.StatusOK, config.Config{
137+
Name: name,
138+
Content: content,
139+
ChatGPTMessages: chatgpt.Content,
140+
FilePath: json.NewFilepath,
141+
ModifiedAt: time.Now(),
142+
})
141143
}

query/configs.gen.go

Lines changed: 16 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)