You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.8 KiB
85 lines
1.8 KiB
package core
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"scagnet/util"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// 传入的参数
|
|
type Args struct {
|
|
FilePath string
|
|
}
|
|
|
|
// 返回的参数
|
|
type Reply struct {
|
|
Redata string
|
|
}
|
|
|
|
// 返回的json数据
|
|
type FileRpc string
|
|
|
|
// 返回的文件json结构体
|
|
type FloopJson struct {
|
|
Path string `json:"path"`
|
|
Size int64 `json:"size"`
|
|
Hash string `json:"hash"`
|
|
}
|
|
|
|
// 文件列表
|
|
type FlpList struct {
|
|
Flist []FloopJson `json:"list"`
|
|
}
|
|
|
|
// 获取文件路径
|
|
func (f *FileRpc) GetFilePath(args *Args, replay *Reply) error {
|
|
rootDir := args.FilePath
|
|
logger := util.NewProductionLogger()
|
|
defer logger.Sync()
|
|
// 遍历目录下的文件
|
|
var allFiles []string // 存储所有文件路径
|
|
var flplist FlpList // 文件列表
|
|
|
|
// 递归遍历目录及其子目录
|
|
err := filepath.WalkDir(rootDir, func(path string, d os.DirEntry, err error) error {
|
|
// 处理遍历过程中的错误(如权限不足)
|
|
if err != nil {
|
|
logger.Error("访问路径出错", zap.Error(err))
|
|
return filepath.SkipDir // 跳过当前目录,继续遍历其他目录
|
|
}
|
|
|
|
// 只收集文件(排除目录)
|
|
if !d.IsDir() {
|
|
allFiles = append(allFiles, path)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
logger.Error("遍历目录出错", zap.Error(err))
|
|
return err
|
|
}
|
|
// 打印所有文件路径
|
|
for _, file := range allFiles {
|
|
// 打印所有文件路径
|
|
// fmt.Printf("%d. %s\n", i+1, file)
|
|
d, _ := os.Stat(file)
|
|
// 加入到列表
|
|
hash := util.CalacHash(file)
|
|
flplist.Flist = append(flplist.Flist, FloopJson{Path: file, Size: d.Size(), Hash: hash})
|
|
}
|
|
|
|
// 将文件列表转换为json字符串
|
|
jsonStr, err := json.Marshal(flplist)
|
|
if err != nil {
|
|
logger.Error("转换为json字符串出错", zap.Error(err))
|
|
return err
|
|
}
|
|
// fmt.Printf("jsonStr: %s\n", jsonStr)
|
|
replay.Redata = string(jsonStr)
|
|
|
|
return nil
|
|
}
|
|
|