tj.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = readDirRecursive(client, remote+"/"+entry.Name(), success, m)
  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. }
  48. }
  49. log.Println(company, "上传成功数量为:", success)
  50. }
  51. // 递归遍历目录结构
  52. func readDirRecursive(client *gowebdav.Client, path string, success int, m map[string]bool) int {
  53. entries, err := client.ReadDir(path)
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. for _, entry := range entries {
  58. if entry.IsDir() {
  59. // 如果是目录,则递归遍历子目录
  60. success = readDirRecursive(client, path+"/"+entry.Name(), success, m)
  61. } else {
  62. file_path := path + "/" + entry.Name()
  63. if !strings.Contains(file_path, *date) {
  64. continue
  65. }
  66. //计算成功
  67. pth := rename(client, file_path)
  68. if m[pth] {
  69. continue
  70. }
  71. m[pth] = true
  72. info, _ := client.Read(pth)
  73. str := string(info)
  74. // log.Println(str)
  75. success += strings.Count(str, "文件上传成功!")
  76. }
  77. }
  78. return success
  79. }
  80. func rename(client *gowebdav.Client, path string) string {
  81. newpath := path
  82. if strings.Contains(path, ".log") {
  83. newpath = strings.ReplaceAll(path, ".log", ".txt")
  84. client.Rename(path, newpath, true)
  85. }
  86. return newpath
  87. }