file.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "io/ioutil"
  7. "log"
  8. "os/exec"
  9. "strings"
  10. "time"
  11. )
  12. type PahtInfo struct {
  13. CurrPath string `json:"curr_path" description:"当前路径"`
  14. FileDirList []FileDirList `json:"file_dir_list" description:"当前路径下的文件/目录"`
  15. }
  16. func GetPathInfo(c *gin.Context) {
  17. cd := exec.Command("pwd")
  18. str, err := cd.Output()
  19. if err != nil {
  20. response.Fail("获取目录信息失败:"+err.Error(), c)
  21. }
  22. fmt.Println(string(str))
  23. path := strings.Replace(string(str), "\n", "", -1)
  24. fileDirList, err := getPathList(path)
  25. if err != nil {
  26. response.Fail("获取信息失败:"+err.Error(), c)
  27. }
  28. response.OkData("获取成功", PahtInfo{
  29. CurrPath: path,
  30. FileDirList: fileDirList,
  31. }, c)
  32. }
  33. type FileDirList struct {
  34. Name string `json:"name" description:"文件/目录名称"`
  35. FileType string `json:"file_type" description:"文件类型,枚举值:file、dir"`
  36. Size int64 `json:"size" description:"文件大小"`
  37. ModifyTime time.Time `json:"modify_time" description:"最后一次修改时间"`
  38. }
  39. func getPathList(dirname string) (fileDirList []FileDirList, err error) {
  40. fileInfos, err := ioutil.ReadDir(dirname)
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. for _, fi := range fileInfos {
  45. fileDirInfo := FileDirList{
  46. Name: fi.Name(),
  47. FileType: "file",
  48. Size: fi.Size(),
  49. ModifyTime: fi.ModTime(),
  50. }
  51. if fi.IsDir() {
  52. fileDirInfo.FileType = "dir"
  53. }
  54. fileDirList = append(fileDirList, fileDirInfo)
  55. }
  56. return
  57. }