From bc92c0f344ceb5618fad9a16c5df8c489f282eb6 Mon Sep 17 00:00:00 2001 From: Jacky Date: Mon, 19 Aug 2024 17:43:18 +0800 Subject: [PATCH] fix: sync config cannot be overwritten #508 --- api/config/modify.go | 234 +++++++++++++++++++++--------------------- app/auto-imports.d.ts | 78 -------------- 2 files changed, 117 insertions(+), 195 deletions(-) diff --git a/api/config/modify.go b/api/config/modify.go index 5631a260..0ce85649 100644 --- a/api/config/modify.go +++ b/api/config/modify.go @@ -1,141 +1,141 @@ package config import ( - "github.com/0xJacky/Nginx-UI/api" - "github.com/0xJacky/Nginx-UI/internal/config" - "github.com/0xJacky/Nginx-UI/internal/helper" - "github.com/0xJacky/Nginx-UI/internal/nginx" - "github.com/0xJacky/Nginx-UI/model" - "github.com/0xJacky/Nginx-UI/query" - "github.com/gin-gonic/gin" - "github.com/sashabaranov/go-openai" - "net/http" - "os" - "time" + "github.com/0xJacky/Nginx-UI/api" + "github.com/0xJacky/Nginx-UI/internal/config" + "github.com/0xJacky/Nginx-UI/internal/helper" + "github.com/0xJacky/Nginx-UI/internal/nginx" + "github.com/0xJacky/Nginx-UI/model" + "github.com/0xJacky/Nginx-UI/query" + "github.com/gin-gonic/gin" + "github.com/sashabaranov/go-openai" + "net/http" + "os" + "time" ) type EditConfigJson struct { - Content string `json:"content" binding:"required"` + Content string `json:"content" binding:"required"` } func EditConfig(c *gin.Context) { - name := c.Param("name") - var json struct { - Name string `json:"name" binding:"required"` - Filepath string `json:"filepath" binding:"required"` - NewFilepath string `json:"new_filepath" binding:"required"` - Content string `json:"content"` - Overwrite bool `json:"overwrite"` - SyncNodeIds []int `json:"sync_node_ids"` - } - if !api.BindAndValid(c, &json) { - return - } + name := c.Param("name") + var json struct { + Name string `json:"name" binding:"required"` + Filepath string `json:"filepath" binding:"required"` + NewFilepath string `json:"new_filepath" binding:"required"` + Content string `json:"content"` + SyncOverwrite bool `json:"sync_overwrite"` + SyncNodeIds []int `json:"sync_node_ids"` + } + if !api.BindAndValid(c, &json) { + return + } - path := json.Filepath - if !helper.IsUnderDirectory(path, nginx.GetConfPath()) { - c.JSON(http.StatusForbidden, gin.H{ - "message": "filepath is not under the nginx conf path", - }) - return - } + path := json.Filepath + if !helper.IsUnderDirectory(path, nginx.GetConfPath()) { + c.JSON(http.StatusForbidden, gin.H{ + "message": "filepath is not under the nginx conf path", + }) + return + } - if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) { - c.JSON(http.StatusForbidden, gin.H{ - "message": "new filepath is not under the nginx conf path", - }) - return - } + if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) { + c.JSON(http.StatusForbidden, gin.H{ + "message": "new filepath is not under the nginx conf path", + }) + return + } - if !helper.FileExists(path) { - c.JSON(http.StatusNotFound, gin.H{ - "message": "file not found", - }) - return - } + if !helper.FileExists(path) { + c.JSON(http.StatusNotFound, gin.H{ + "message": "file not found", + }) + return + } - content := json.Content - origContent, err := os.ReadFile(path) - if err != nil { - api.ErrHandler(c, err) - return - } + content := json.Content + origContent, err := os.ReadFile(path) + if err != nil { + api.ErrHandler(c, err) + return + } - if content != "" && content != string(origContent) { - err = os.WriteFile(path, []byte(content), 0644) - if err != nil { - api.ErrHandler(c, err) - return - } - } + if content != "" && content != string(origContent) { + err = os.WriteFile(path, []byte(content), 0644) + if err != nil { + api.ErrHandler(c, err) + return + } + } - q := query.Config - cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate() - if err != nil { - api.ErrHandler(c, err) - return - } + q := query.Config + cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate() + if err != nil { + api.ErrHandler(c, err) + return + } - _, err = q.Where(q.Filepath.Eq(json.Filepath)).Updates(&model.Config{ - Name: json.Name, - Filepath: json.NewFilepath, - SyncNodeIds: json.SyncNodeIds, - SyncOverwrite: json.Overwrite, - }) + _, err = q.Where(q.Filepath.Eq(json.Filepath)).Updates(&model.Config{ + Name: json.Name, + Filepath: json.NewFilepath, + SyncNodeIds: json.SyncNodeIds, + SyncOverwrite: json.SyncOverwrite, + }) - if err != nil { - api.ErrHandler(c, err) - return - } - g := query.ChatGPTLog - // handle rename - if path != json.NewFilepath { - if helper.FileExists(json.NewFilepath) { - c.JSON(http.StatusNotAcceptable, gin.H{ - "message": "File exists", - }) - return - } - err := os.Rename(json.Filepath, json.NewFilepath) - if err != nil { - api.ErrHandler(c, err) - return - } + if err != nil { + api.ErrHandler(c, err) + return + } + g := query.ChatGPTLog + // handle rename + if path != json.NewFilepath { + if helper.FileExists(json.NewFilepath) { + c.JSON(http.StatusNotAcceptable, gin.H{ + "message": "File exists", + }) + return + } + err := os.Rename(json.Filepath, json.NewFilepath) + if err != nil { + api.ErrHandler(c, err) + return + } - // update ChatGPT record - _, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete() - _, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath) - } + // update ChatGPT record + _, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete() + _, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath) + } - err = config.SyncToRemoteServer(cfg, json.NewFilepath) - if err != nil { - api.ErrHandler(c, err) - return - } + err = config.SyncToRemoteServer(cfg, json.NewFilepath) + if err != nil { + api.ErrHandler(c, err) + return + } - output := nginx.Reload() - if nginx.GetLogLevel(output) >= nginx.Warn { - c.JSON(http.StatusInternalServerError, gin.H{ - "message": output, - }) - return - } + output := nginx.Reload() + if nginx.GetLogLevel(output) >= nginx.Warn { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": output, + }) + return + } - chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate() - if err != nil { - api.ErrHandler(c, err) - return - } + chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate() + if err != nil { + api.ErrHandler(c, err) + return + } - if chatgpt.Content == nil { - chatgpt.Content = make([]openai.ChatCompletionMessage, 0) - } + if chatgpt.Content == nil { + chatgpt.Content = make([]openai.ChatCompletionMessage, 0) + } - c.JSON(http.StatusOK, config.Config{ - Name: name, - Content: content, - ChatGPTMessages: chatgpt.Content, - FilePath: json.NewFilepath, - ModifiedAt: time.Now(), - }) + c.JSON(http.StatusOK, config.Config{ + Name: name, + Content: content, + ChatGPTMessages: chatgpt.Content, + FilePath: json.NewFilepath, + ModifiedAt: time.Now(), + }) } diff --git a/app/auto-imports.d.ts b/app/auto-imports.d.ts index ef98aa91..eddba1ce 100644 --- a/app/auto-imports.d.ts +++ b/app/auto-imports.d.ts @@ -165,81 +165,3 @@ declare module 'vue' { readonly watchSyncEffect: UnwrapRef } } -declare module '@vue/runtime-core' { - interface GlobalComponents {} - interface ComponentCustomProperties { - readonly $gettext: UnwrapRef - readonly $ngettext: UnwrapRef - readonly $npgettext: UnwrapRef - readonly $pgettext: UnwrapRef - readonly EffectScope: UnwrapRef - readonly acceptHMRUpdate: UnwrapRef - readonly computed: UnwrapRef - readonly createApp: UnwrapRef - readonly createPinia: UnwrapRef - readonly customRef: UnwrapRef - readonly defineAsyncComponent: UnwrapRef - readonly defineComponent: UnwrapRef - readonly defineStore: UnwrapRef - readonly effectScope: UnwrapRef - readonly getActivePinia: UnwrapRef - readonly getCurrentInstance: UnwrapRef - readonly getCurrentScope: UnwrapRef - readonly h: UnwrapRef - readonly inject: UnwrapRef - readonly isProxy: UnwrapRef - readonly isReactive: UnwrapRef - readonly isReadonly: UnwrapRef - readonly isRef: UnwrapRef - readonly mapActions: UnwrapRef - readonly mapGetters: UnwrapRef - readonly mapState: UnwrapRef - readonly mapStores: UnwrapRef - readonly mapWritableState: UnwrapRef - readonly markRaw: UnwrapRef - readonly nextTick: UnwrapRef - readonly onActivated: UnwrapRef - readonly onBeforeMount: UnwrapRef - readonly onBeforeRouteLeave: UnwrapRef - readonly onBeforeRouteUpdate: UnwrapRef - readonly onBeforeUnmount: UnwrapRef - readonly onBeforeUpdate: UnwrapRef - readonly onDeactivated: UnwrapRef - readonly onErrorCaptured: UnwrapRef - readonly onMounted: UnwrapRef - readonly onRenderTracked: UnwrapRef - readonly onRenderTriggered: UnwrapRef - readonly onScopeDispose: UnwrapRef - readonly onServerPrefetch: UnwrapRef - readonly onUnmounted: UnwrapRef - readonly onUpdated: UnwrapRef - readonly provide: UnwrapRef - readonly reactive: UnwrapRef - readonly readonly: UnwrapRef - readonly ref: UnwrapRef - readonly resolveComponent: UnwrapRef - readonly setActivePinia: UnwrapRef - readonly setMapStoreSuffix: UnwrapRef - readonly shallowReactive: UnwrapRef - readonly shallowReadonly: UnwrapRef - readonly shallowRef: UnwrapRef - readonly storeToRefs: UnwrapRef - readonly toRaw: UnwrapRef - readonly toRef: UnwrapRef - readonly toRefs: UnwrapRef - readonly toValue: UnwrapRef - readonly triggerRef: UnwrapRef - readonly unref: UnwrapRef - readonly useAttrs: UnwrapRef - readonly useCssModule: UnwrapRef - readonly useCssVars: UnwrapRef - readonly useLink: UnwrapRef - readonly useRoute: UnwrapRef - readonly useRouter: UnwrapRef - readonly useSlots: UnwrapRef - readonly watch: UnwrapRef - readonly watchEffect: UnwrapRef - readonly watchPostEffect: UnwrapRef - readonly watchSyncEffect: UnwrapRef - } -}