Skip to content

Commit bc92c0f

Browse files
committed
fix: sync config cannot be overwritten 0xJacky#508
1 parent c700a29 commit bc92c0f

File tree

2 files changed

+131
-209
lines changed

2 files changed

+131
-209
lines changed

api/config/modify.go

Lines changed: 131 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,141 @@
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-
Overwrite bool `json:"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.Overwrite,
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)).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+
})
141141
}

app/auto-imports.d.ts

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -165,81 +165,3 @@ declare module 'vue' {
165165
readonly watchSyncEffect: UnwrapRef<typeof import('vue')['watchSyncEffect']>
166166
}
167167
}
168-
declare module '@vue/runtime-core' {
169-
interface GlobalComponents {}
170-
interface ComponentCustomProperties {
171-
readonly $gettext: UnwrapRef<typeof import('@/gettext')['$gettext']>
172-
readonly $ngettext: UnwrapRef<typeof import('@/gettext')['$ngettext']>
173-
readonly $npgettext: UnwrapRef<typeof import('@/gettext')['$npgettext']>
174-
readonly $pgettext: UnwrapRef<typeof import('@/gettext')['$pgettext']>
175-
readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
176-
readonly acceptHMRUpdate: UnwrapRef<typeof import('pinia')['acceptHMRUpdate']>
177-
readonly computed: UnwrapRef<typeof import('vue')['computed']>
178-
readonly createApp: UnwrapRef<typeof import('vue')['createApp']>
179-
readonly createPinia: UnwrapRef<typeof import('pinia')['createPinia']>
180-
readonly customRef: UnwrapRef<typeof import('vue')['customRef']>
181-
readonly defineAsyncComponent: UnwrapRef<typeof import('vue')['defineAsyncComponent']>
182-
readonly defineComponent: UnwrapRef<typeof import('vue')['defineComponent']>
183-
readonly defineStore: UnwrapRef<typeof import('pinia')['defineStore']>
184-
readonly effectScope: UnwrapRef<typeof import('vue')['effectScope']>
185-
readonly getActivePinia: UnwrapRef<typeof import('pinia')['getActivePinia']>
186-
readonly getCurrentInstance: UnwrapRef<typeof import('vue')['getCurrentInstance']>
187-
readonly getCurrentScope: UnwrapRef<typeof import('vue')['getCurrentScope']>
188-
readonly h: UnwrapRef<typeof import('vue')['h']>
189-
readonly inject: UnwrapRef<typeof import('vue')['inject']>
190-
readonly isProxy: UnwrapRef<typeof import('vue')['isProxy']>
191-
readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']>
192-
readonly isReadonly: UnwrapRef<typeof import('vue')['isReadonly']>
193-
readonly isRef: UnwrapRef<typeof import('vue')['isRef']>
194-
readonly mapActions: UnwrapRef<typeof import('pinia')['mapActions']>
195-
readonly mapGetters: UnwrapRef<typeof import('pinia')['mapGetters']>
196-
readonly mapState: UnwrapRef<typeof import('pinia')['mapState']>
197-
readonly mapStores: UnwrapRef<typeof import('pinia')['mapStores']>
198-
readonly mapWritableState: UnwrapRef<typeof import('pinia')['mapWritableState']>
199-
readonly markRaw: UnwrapRef<typeof import('vue')['markRaw']>
200-
readonly nextTick: UnwrapRef<typeof import('vue')['nextTick']>
201-
readonly onActivated: UnwrapRef<typeof import('vue')['onActivated']>
202-
readonly onBeforeMount: UnwrapRef<typeof import('vue')['onBeforeMount']>
203-
readonly onBeforeRouteLeave: UnwrapRef<typeof import('vue-router')['onBeforeRouteLeave']>
204-
readonly onBeforeRouteUpdate: UnwrapRef<typeof import('vue-router')['onBeforeRouteUpdate']>
205-
readonly onBeforeUnmount: UnwrapRef<typeof import('vue')['onBeforeUnmount']>
206-
readonly onBeforeUpdate: UnwrapRef<typeof import('vue')['onBeforeUpdate']>
207-
readonly onDeactivated: UnwrapRef<typeof import('vue')['onDeactivated']>
208-
readonly onErrorCaptured: UnwrapRef<typeof import('vue')['onErrorCaptured']>
209-
readonly onMounted: UnwrapRef<typeof import('vue')['onMounted']>
210-
readonly onRenderTracked: UnwrapRef<typeof import('vue')['onRenderTracked']>
211-
readonly onRenderTriggered: UnwrapRef<typeof import('vue')['onRenderTriggered']>
212-
readonly onScopeDispose: UnwrapRef<typeof import('vue')['onScopeDispose']>
213-
readonly onServerPrefetch: UnwrapRef<typeof import('vue')['onServerPrefetch']>
214-
readonly onUnmounted: UnwrapRef<typeof import('vue')['onUnmounted']>
215-
readonly onUpdated: UnwrapRef<typeof import('vue')['onUpdated']>
216-
readonly provide: UnwrapRef<typeof import('vue')['provide']>
217-
readonly reactive: UnwrapRef<typeof import('vue')['reactive']>
218-
readonly readonly: UnwrapRef<typeof import('vue')['readonly']>
219-
readonly ref: UnwrapRef<typeof import('vue')['ref']>
220-
readonly resolveComponent: UnwrapRef<typeof import('vue')['resolveComponent']>
221-
readonly setActivePinia: UnwrapRef<typeof import('pinia')['setActivePinia']>
222-
readonly setMapStoreSuffix: UnwrapRef<typeof import('pinia')['setMapStoreSuffix']>
223-
readonly shallowReactive: UnwrapRef<typeof import('vue')['shallowReactive']>
224-
readonly shallowReadonly: UnwrapRef<typeof import('vue')['shallowReadonly']>
225-
readonly shallowRef: UnwrapRef<typeof import('vue')['shallowRef']>
226-
readonly storeToRefs: UnwrapRef<typeof import('pinia')['storeToRefs']>
227-
readonly toRaw: UnwrapRef<typeof import('vue')['toRaw']>
228-
readonly toRef: UnwrapRef<typeof import('vue')['toRef']>
229-
readonly toRefs: UnwrapRef<typeof import('vue')['toRefs']>
230-
readonly toValue: UnwrapRef<typeof import('vue')['toValue']>
231-
readonly triggerRef: UnwrapRef<typeof import('vue')['triggerRef']>
232-
readonly unref: UnwrapRef<typeof import('vue')['unref']>
233-
readonly useAttrs: UnwrapRef<typeof import('vue')['useAttrs']>
234-
readonly useCssModule: UnwrapRef<typeof import('vue')['useCssModule']>
235-
readonly useCssVars: UnwrapRef<typeof import('vue')['useCssVars']>
236-
readonly useLink: UnwrapRef<typeof import('vue-router')['useLink']>
237-
readonly useRoute: UnwrapRef<typeof import('vue-router')['useRoute']>
238-
readonly useRouter: UnwrapRef<typeof import('vue-router')['useRouter']>
239-
readonly useSlots: UnwrapRef<typeof import('vue')['useSlots']>
240-
readonly watch: UnwrapRef<typeof import('vue')['watch']>
241-
readonly watchEffect: UnwrapRef<typeof import('vue')['watchEffect']>
242-
readonly watchPostEffect: UnwrapRef<typeof import('vue')['watchPostEffect']>
243-
readonly watchSyncEffect: UnwrapRef<typeof import('vue')['watchSyncEffect']>
244-
}
245-
}

0 commit comments

Comments
 (0)