package controller import ( "fmt" "github.com/gin-gonic/gin" "hongze/hongze_yb/controller/response" "io/ioutil" "log" "os/exec" "strings" "time" ) type PahtInfo struct { CurrPath string `json:"curr_path" description:"当前路径"` FileDirList []FileDirList `json:"file_dir_list" description:"当前路径下的文件/目录"` } func GetPathInfo(c *gin.Context) { cd := exec.Command("pwd") str, err := cd.Output() if err != nil { response.Fail("获取目录信息失败:"+err.Error(), c) } fmt.Println(string(str)) path := strings.Replace(string(str), "\n", "", -1) fileDirList, err := getPathList(path) if err != nil { response.Fail("获取信息失败:"+err.Error(), c) } response.OkData("获取成功", PahtInfo{ CurrPath: path, FileDirList: fileDirList, }, c) } type FileDirList struct { Name string `json:"name" description:"文件/目录名称"` FileType string `json:"file_type" description:"文件类型,枚举值:file、dir"` Size int64 `json:"size" description:"文件大小"` ModifyTime time.Time `json:"modify_time" description:"最后一次修改时间"` } func getPathList(dirname string) (fileDirList []FileDirList, err error) { fileInfos, err := ioutil.ReadDir(dirname) if err != nil { log.Fatal(err) } for _, fi := range fileInfos { fileDirInfo := FileDirList{ Name: fi.Name(), FileType: "file", Size: fi.Size(), ModifyTime: fi.ModTime(), } if fi.IsDir() { fileDirInfo.FileType = "dir" } fileDirList = append(fileDirList, fileDirInfo) } return }