tj.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "log"
  4. "strings"
  5. "github.com/studio-b12/gowebdav"
  6. )
  7. func Statistics() {
  8. log.Println("获取:", *date, " 期间上传的数据量")
  9. path := "云盘管理/日志/机电学院"
  10. getRemoteFilePath(webdav, user, secert, path, path)
  11. // path = "云盘管理/日志/中电科"
  12. // getRemoteFilePath(webdav, user, secert, path, path)
  13. // path = "云盘管理/日志/华软"
  14. // getRemoteFilePath(webdav, user, secert, path, path)
  15. // path := "云盘管理/日志/张金石"
  16. // getRemoteFilePath(webdav, user, secert, path, path)
  17. }
  18. // 获取数据目录
  19. func getRemoteFilePath(webdav, user, secert, remote, company string) {
  20. success := 0
  21. failed := 0
  22. client := gowebdav.NewAuthClient(webdav, gowebdav.NewAutoAuth(user, secert))
  23. client.Connect()
  24. // 获取根目录列表
  25. entries, err := client.ReadDir(remote)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. m := map[string]bool{}
  30. // 遍历目录结构
  31. for _, entry := range entries {
  32. // log.Println(remote + "/" + entry.Name())
  33. if entry.IsDir() {
  34. // 如果是目录,则递归遍历子目录
  35. success, failed = readDirRecursive(client, remote+"/"+entry.Name(), success, m, failed)
  36. } else {
  37. pth := rename(client, remote+"/"+entry.Name())
  38. if m[pth] {
  39. continue
  40. }
  41. m[pth] = true
  42. log.Println(pth)
  43. info, _ := client.Read(pth)
  44. str := string(info)
  45. // log.Println(str)
  46. success += strings.Count(str, "文件上传成功!")
  47. failed += strings.Count(str, "已经上传过,过滤")
  48. }
  49. }
  50. log.Println(company, "上传成功数量为:", success, "失败数量为:", failed)
  51. }
  52. // 递归遍历目录结构
  53. func readDirRecursive(client *gowebdav.Client, path string, success int, m map[string]bool, failed int) (int, int) {
  54. entries, err := client.ReadDir(path)
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. for _, entry := range entries {
  59. if entry.IsDir() {
  60. // 如果是目录,则递归遍历子目录
  61. success, failed = readDirRecursive(client, path+"/"+entry.Name(), success, m, failed)
  62. } else {
  63. file_path := path + "/" + entry.Name()
  64. if !strings.Contains(file_path, *date) {
  65. continue
  66. } else {
  67. log.Println(file_path)
  68. }
  69. //计算成功
  70. pth := rename(client, file_path)
  71. if m[pth] {
  72. continue
  73. }
  74. m[pth] = true
  75. info, _ := client.Read(pth)
  76. str := string(info)
  77. // log.Println("ssss:", str)
  78. success += strings.Count(str, "文件上传成功!")
  79. // log.Println(file_path, strings.Count(str, "文件上传成功!"))
  80. failed += strings.Count(str, "已经上传过,过滤")
  81. }
  82. }
  83. return success, failed
  84. }
  85. func rename(client *gowebdav.Client, path string) string {
  86. newpath := path
  87. if strings.Contains(path, ".log") {
  88. newpath = strings.ReplaceAll(path, ".log", ".txt")
  89. client.Rename(path, newpath, true)
  90. }
  91. return newpath
  92. }