From 39c6df11c1d3585e832e4fe340921be0016e2af8 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Wed, 15 Jan 2020 20:10:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=9C=A8=E7=BA=BF=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/model/file.go | 13 +++++++------ backend/routes/spider.go | 2 +- backend/services/file.go | 13 +++++++++---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/backend/model/file.go b/backend/model/file.go index 1af3b851..a2ad34eb 100644 --- a/backend/model/file.go +++ b/backend/model/file.go @@ -20,12 +20,13 @@ type GridFs struct { } type File struct { - Name string `json:"name"` - Path string `json:"path"` - IsDir bool `json:"is_dir"` - Size int64 `json:"size"` - Children []File `json:"children"` - Label string `json:"label"` + Name string `json:"name"` + Path string `json:"path"` + RelativePath string `json:"relative_path"` + IsDir bool `json:"is_dir"` + Size int64 `json:"size"` + Children []File `json:"children"` + Label string `json:"label"` } func (f *GridFs) Remove() { diff --git a/backend/routes/spider.go b/backend/routes/spider.go index 2534a7da..4adfb707 100644 --- a/backend/routes/spider.go +++ b/backend/routes/spider.go @@ -708,7 +708,7 @@ func RenameSpiderFile(c *gin.Context) { // 原文件路径 filePath := path.Join(spider.Src, reqBody.Path) - newFilePath := path.Join(spider.Src, reqBody.NewPath) + newFilePath := path.Join(path.Join(path.Dir(filePath), reqBody.NewPath)) // 如果新文件已存在,则报错 if utils.Exists(newFilePath) { diff --git a/backend/services/file.go b/backend/services/file.go index 0b810209..d126fcab 100644 --- a/backend/services/file.go +++ b/backend/services/file.go @@ -6,9 +6,14 @@ import ( "os" "path" "runtime/debug" + "strings" ) func GetFileNodeTree(dstPath string, level int) (f model.File, err error) { + return getFileNodeTree(dstPath, level, dstPath) +} + +func getFileNodeTree(dstPath string, level int, rootPath string) (f model.File, err error) { dstF, err := os.Open(dstPath) if err != nil { log.Errorf(err.Error()) @@ -24,9 +29,9 @@ func GetFileNodeTree(dstPath string, level int) (f model.File, err error) { } if !fileInfo.IsDir() { //如果dstF是文件 return model.File{ - Label: fileInfo.Name(), + Label: fileInfo.Name(), Name: fileInfo.Name(), - Path: dstPath, + Path: strings.Replace(dstPath, rootPath, "", -1), IsDir: false, Size: fileInfo.Size(), Children: nil, @@ -41,13 +46,13 @@ func GetFileNodeTree(dstPath string, level int) (f model.File, err error) { f = model.File{ Label: path.Base(dstPath), Name: path.Base(dstPath), - Path: dstPath, + Path: strings.Replace(dstPath, rootPath, "", -1), IsDir: true, Size: 0, Children: nil, } for _, subFileInfo := range dir { - subFileNode, err := GetFileNodeTree(path.Join(dstPath, subFileInfo.Name()), level+1) + subFileNode, err := getFileNodeTree(path.Join(dstPath, subFileInfo.Name()), level+1, rootPath) if err != nil { log.Errorf(err.Error()) debug.PrintStack()