소스 검색

fix(config): update path handling #1278

0xJacky 1 주 전
부모
커밋
cd05303163

+ 1 - 0
.gitignore

@@ -20,3 +20,4 @@ internal/**/*.gen.go
 .devcontainer/casdoor.pem
 .vscode/.i18n-gettext.secret
 .go/
+/.cunzhi-memory

+ 10 - 6
api/config/get.go

@@ -14,13 +14,17 @@ import (
 )
 
 func GetConfig(c *gin.Context) {
-	relativePath := helper.UnescapeURL(c.Param("path"))
+	path := helper.UnescapeURL(c.Query("path"))
+
+	var absPath string
+	if filepath.IsAbs(path) {
+		absPath = path
+	} else {
+		absPath = nginx.GetConfPath(path)
+	}
 
-	absPath := nginx.GetConfPath(relativePath)
 	if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) {
-		c.JSON(http.StatusForbidden, gin.H{
-			"message": "path is not under the nginx conf path",
-		})
+		cosy.ErrHandler(c, cosy.WrapErrorWithParams(config.ErrPathIsNotUnderTheNginxConfDir, absPath, nginx.GetConfPath()))
 		return
 	}
 
@@ -48,7 +52,7 @@ func GetConfig(c *gin.Context) {
 		Content:       string(content),
 		FilePath:      absPath,
 		ModifiedAt:    stat.ModTime(),
-		Dir:           filepath.Dir(relativePath),
+		Dir:           filepath.Dir(absPath),
 		SyncNodeIds:   cfg.SyncNodeIds,
 		SyncOverwrite: cfg.SyncOverwrite,
 	})

+ 9 - 4
api/config/modify.go

@@ -20,10 +20,9 @@ type EditConfigJson struct {
 }
 
 func EditConfig(c *gin.Context) {
-	relativePath := helper.UnescapeURL(c.Param("path"))
-
 	var json struct {
 		Content       string   `json:"content"`
+		Path          string   `json:"path"`
 		SyncOverwrite bool     `json:"sync_overwrite"`
 		SyncNodeIds   []uint64 `json:"sync_node_ids"`
 	}
@@ -31,7 +30,13 @@ func EditConfig(c *gin.Context) {
 		return
 	}
 
-	absPath := nginx.GetConfPath(relativePath)
+	var absPath string
+	if filepath.IsAbs(json.Path) {
+		absPath = json.Path
+	} else {
+		absPath = nginx.GetConfPath(json.Path)
+	}
+
 	if !helper.FileExists(absPath) {
 		c.JSON(http.StatusNotFound, gin.H{
 			"message": "file not found",
@@ -74,7 +79,7 @@ func EditConfig(c *gin.Context) {
 		Content:       content,
 		FilePath:      absPath,
 		ModifiedAt:    time.Now(),
-		Dir:           filepath.Dir(relativePath),
+		Dir:           filepath.Dir(absPath),
 		SyncNodeIds:   cfg.SyncNodeIds,
 		SyncOverwrite: cfg.SyncOverwrite,
 	})

+ 2 - 2
api/config/router.go

@@ -9,9 +9,9 @@ func InitRouter(r *gin.RouterGroup) {
 	r.GET("config_base_path", GetBasePath)
 
 	r.GET("configs", GetConfigs)
-	r.GET("configs/*path", GetConfig)
+	r.GET("config", GetConfig)
 	r.POST("configs", AddConfig)
-	r.POST("configs/*path", EditConfig)
+	r.POST("config", EditConfig)
 
 	o := r.Group("", middleware.RequireSecureSession())
 	{

+ 9 - 0
app/src/api/config.ts

@@ -1,3 +1,4 @@
+import type { AxiosRequestConfig } from '@uozi-admin/request'
 import type { GetListResponse } from '@/api/curd'
 import type { ChatComplicationMessage } from '@/api/openai'
 import { extendCurdApi, http, useCurdApi } from '@uozi-admin/request'
@@ -26,6 +27,14 @@ export interface ConfigBackup extends ModelBase {
 }
 
 const config = extendCurdApi(useCurdApi<Config>('/configs'), {
+  // eslint-disable-next-line ts/no-explicit-any
+  getItem: (id: string | number, params?: Record<string, any>, config?: AxiosRequestConfig) => {
+    return http.get<Config>('/config', { params: { path: id, ...params }, ...config })
+  },
+  // eslint-disable-next-line ts/no-explicit-any
+  updateItem: (id: string | number, data: Record<string, any>, config?: AxiosRequestConfig) => {
+    return http.post<Config>('/config', { path: decodeURIComponent(id as string), ...data }, config)
+  },
   get_base_path: () => http.get('/config_base_path'),
   mkdir: (basePath: string, name: string) => http.post('/config_mkdir', { base_path: basePath, folder_name: name }),
   rename: (basePath: string, origName: string, newName: string, syncNodeIds?: number[]) => http.post('/config_rename', {

+ 1 - 1
app/src/components/NgxConfigEditor/directive/DirectiveDocuments.vue

@@ -17,7 +17,7 @@ const { nginxDirectivesDocsMap } = storeToRefs(useDirectiveStore())
                 nginxDirectivesDocsMap[props.directive].links.length)"
   >
     <div v-for="(link, idx) in nginxDirectivesDocsMap[props.directive]?.links" :key="idx" class="mb-2">
-      <a :href="link">
+      <a :href="link" target="_blank">
         {{ link }}
       </a>
     </div>

+ 0 - 2
app/src/components/NgxConfigEditor/directive/DirectiveEditorItem.vue

@@ -41,8 +41,6 @@ function save() {
   config.updateItem(directive.value.params, { content: content.value }).then(r => {
     content.value = r.content
     message.success($gettext('Saved successfully'))
-  }).catch(r => {
-    message.error($gettext('Save error %{msg}', { msg: r.message ?? '' }))
   })
 }
 

+ 2 - 1
internal/config/save.go

@@ -8,6 +8,7 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/query"
+	"github.com/uozi-tech/cosy"
 	"gorm.io/gen/field"
 )
 
@@ -24,7 +25,7 @@ func Save(absPath string, content string, cfg *model.Config) (err error) {
 	}
 
 	if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) {
-		return ErrPathIsNotUnderTheNginxConfDir
+		return cosy.WrapErrorWithParams(ErrPathIsNotUnderTheNginxConfDir, absPath, nginx.GetConfPath())
 	}
 
 	err = CheckAndCreateHistory(absPath, content)