feat: rename folder or file in configurations list

This commit is contained in:
Jacky 2024-07-25 21:30:11 +08:00
parent 5dd2c8f931
commit ace8d7a0fe
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
12 changed files with 309 additions and 5 deletions

36
api/config/folder.go Normal file
View file

@ -0,0 +1,36 @@
package config
import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/gin-gonic/gin"
"net/http"
"os"
)
func Mkdir(c *gin.Context) {
var json struct {
BasePath string `json:"base_path"`
FolderName string `json:"folder_name"`
}
if !api.BindAndValid(c, &json) {
return
}
fullPath := nginx.GetConfPath(json.BasePath, json.FolderName)
if !helper.IsUnderDirectory(fullPath, nginx.GetConfPath()) {
c.JSON(http.StatusForbidden, gin.H{
"message": "You are not allowed to create a folder " +
"outside of the nginx configuration directory",
})
return
}
err := os.Mkdir(fullPath, 0755)
if err != nil {
api.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
}

68
api/config/rename.go Normal file
View file

@ -0,0 +1,68 @@
package config
import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"net/http"
"os"
)
func Rename(c *gin.Context) {
var json struct {
BasePath string `json:"base_path"`
OrigName string `json:"orig_name"`
NewName string `json:"new_name"`
}
if !api.BindAndValid(c, &json) {
return
}
if json.OrigName == json.OrigName {
c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
return
}
origFullPath := nginx.GetConfPath(json.BasePath, json.OrigName)
newFullPath := nginx.GetConfPath(json.BasePath, json.NewName)
if !helper.IsUnderDirectory(origFullPath, nginx.GetConfPath()) ||
!helper.IsUnderDirectory(newFullPath, nginx.GetConfPath()) {
c.JSON(http.StatusForbidden, gin.H{
"message": "you are not allowed to rename a file " +
"outside of the nginx config path",
})
return
}
stat, err := os.Stat(origFullPath)
if err != nil {
api.ErrHandler(c, err)
return
}
if helper.FileExists(newFullPath) {
c.JSON(http.StatusNotAcceptable, gin.H{
"message": "target file already exists",
})
return
}
err = os.Rename(origFullPath, newFullPath)
if err != nil {
api.ErrHandler(c, err)
return
}
if !stat.IsDir() {
// update ChatGPT records
g := query.ChatGPTLog
_, _ = g.Where(g.Name.Eq(newFullPath)).Delete()
_, _ = g.Where(g.Name.Eq(origFullPath)).Update(g.Name, newFullPath)
}
c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
}

View file

@ -3,9 +3,12 @@ package config
import "github.com/gin-gonic/gin"
func InitRouter(r *gin.RouterGroup) {
r.GET("config_base_path", GetBasePath)
r.GET("configs", GetConfigs)
r.GET("config/*name", GetConfig)
r.POST("config", AddConfig)
r.POST("config/*name", EditConfig)
r.GET("config_base_path", GetBasePath)
r.POST("config_mkdir", Mkdir)
r.POST("config_rename", Rename)
}