Skip to content

Commit cd05303

Browse files
committed
fix(config): update path handling #1278
1 parent bf9a5da commit cd05303

File tree

8 files changed

+34
-16
lines changed

8 files changed

+34
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ internal/**/*.gen.go
2020
.devcontainer/casdoor.pem
2121
.vscode/.i18n-gettext.secret
2222
.go/
23+
/.cunzhi-memory

api/config/get.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ import (
1414
)
1515

1616
func GetConfig(c *gin.Context) {
17-
relativePath := helper.UnescapeURL(c.Param("path"))
17+
path := helper.UnescapeURL(c.Query("path"))
18+
19+
var absPath string
20+
if filepath.IsAbs(path) {
21+
absPath = path
22+
} else {
23+
absPath = nginx.GetConfPath(path)
24+
}
1825

19-
absPath := nginx.GetConfPath(relativePath)
2026
if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) {
21-
c.JSON(http.StatusForbidden, gin.H{
22-
"message": "path is not under the nginx conf path",
23-
})
27+
cosy.ErrHandler(c, cosy.WrapErrorWithParams(config.ErrPathIsNotUnderTheNginxConfDir, absPath, nginx.GetConfPath()))
2428
return
2529
}
2630

@@ -48,7 +52,7 @@ func GetConfig(c *gin.Context) {
4852
Content: string(content),
4953
FilePath: absPath,
5054
ModifiedAt: stat.ModTime(),
51-
Dir: filepath.Dir(relativePath),
55+
Dir: filepath.Dir(absPath),
5256
SyncNodeIds: cfg.SyncNodeIds,
5357
SyncOverwrite: cfg.SyncOverwrite,
5458
})

api/config/modify.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,23 @@ type EditConfigJson struct {
2020
}
2121

2222
func EditConfig(c *gin.Context) {
23-
relativePath := helper.UnescapeURL(c.Param("path"))
24-
2523
var json struct {
2624
Content string `json:"content"`
25+
Path string `json:"path"`
2726
SyncOverwrite bool `json:"sync_overwrite"`
2827
SyncNodeIds []uint64 `json:"sync_node_ids"`
2928
}
3029
if !cosy.BindAndValid(c, &json) {
3130
return
3231
}
3332

34-
absPath := nginx.GetConfPath(relativePath)
33+
var absPath string
34+
if filepath.IsAbs(json.Path) {
35+
absPath = json.Path
36+
} else {
37+
absPath = nginx.GetConfPath(json.Path)
38+
}
39+
3540
if !helper.FileExists(absPath) {
3641
c.JSON(http.StatusNotFound, gin.H{
3742
"message": "file not found",
@@ -74,7 +79,7 @@ func EditConfig(c *gin.Context) {
7479
Content: content,
7580
FilePath: absPath,
7681
ModifiedAt: time.Now(),
77-
Dir: filepath.Dir(relativePath),
82+
Dir: filepath.Dir(absPath),
7883
SyncNodeIds: cfg.SyncNodeIds,
7984
SyncOverwrite: cfg.SyncOverwrite,
8085
})

api/config/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ func InitRouter(r *gin.RouterGroup) {
99
r.GET("config_base_path", GetBasePath)
1010

1111
r.GET("configs", GetConfigs)
12-
r.GET("configs/*path", GetConfig)
12+
r.GET("config", GetConfig)
1313
r.POST("configs", AddConfig)
14-
r.POST("configs/*path", EditConfig)
14+
r.POST("config", EditConfig)
1515

1616
o := r.Group("", middleware.RequireSecureSession())
1717
{

app/src/api/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { AxiosRequestConfig } from '@uozi-admin/request'
12
import type { GetListResponse } from '@/api/curd'
23
import type { ChatComplicationMessage } from '@/api/openai'
34
import { extendCurdApi, http, useCurdApi } from '@uozi-admin/request'
@@ -26,6 +27,14 @@ export interface ConfigBackup extends ModelBase {
2627
}
2728

2829
const config = extendCurdApi(useCurdApi<Config>('/configs'), {
30+
// eslint-disable-next-line ts/no-explicit-any
31+
getItem: (id: string | number, params?: Record<string, any>, config?: AxiosRequestConfig) => {
32+
return http.get<Config>('/config', { params: { path: id, ...params }, ...config })
33+
},
34+
// eslint-disable-next-line ts/no-explicit-any
35+
updateItem: (id: string | number, data: Record<string, any>, config?: AxiosRequestConfig) => {
36+
return http.post<Config>('/config', { path: decodeURIComponent(id as string), ...data }, config)
37+
},
2938
get_base_path: () => http.get('/config_base_path'),
3039
mkdir: (basePath: string, name: string) => http.post('/config_mkdir', { base_path: basePath, folder_name: name }),
3140
rename: (basePath: string, origName: string, newName: string, syncNodeIds?: number[]) => http.post('/config_rename', {

app/src/components/NgxConfigEditor/directive/DirectiveDocuments.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const { nginxDirectivesDocsMap } = storeToRefs(useDirectiveStore())
1717
nginxDirectivesDocsMap[props.directive].links.length)"
1818
>
1919
<div v-for="(link, idx) in nginxDirectivesDocsMap[props.directive]?.links" :key="idx" class="mb-2">
20-
<a :href="link">
20+
<a :href="link" target="_blank">
2121
{{ link }}
2222
</a>
2323
</div>

app/src/components/NgxConfigEditor/directive/DirectiveEditorItem.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ function save() {
4141
config.updateItem(directive.value.params, { content: content.value }).then(r => {
4242
content.value = r.content
4343
message.success($gettext('Saved successfully'))
44-
}).catch(r => {
45-
message.error($gettext('Save error %{msg}', { msg: r.message ?? '' }))
4644
})
4745
}
4846

internal/config/save.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/0xJacky/Nginx-UI/internal/nginx"
99
"github.com/0xJacky/Nginx-UI/model"
1010
"github.com/0xJacky/Nginx-UI/query"
11+
"github.com/uozi-tech/cosy"
1112
"gorm.io/gen/field"
1213
)
1314

@@ -24,7 +25,7 @@ func Save(absPath string, content string, cfg *model.Config) (err error) {
2425
}
2526

2627
if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) {
27-
return ErrPathIsNotUnderTheNginxConfDir
28+
return cosy.WrapErrorWithParams(ErrPathIsNotUnderTheNginxConfDir, absPath, nginx.GetConfPath())
2829
}
2930

3031
err = CheckAndCreateHistory(absPath, content)

0 commit comments

Comments
 (0)