tj.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }
  16. // 获取数据目录
  17. func getRemoteFilePath(webdav, user, secert, remote, company string) {
  18. success := 0
  19. //failed := 0
  20. client := gowebdav.NewAuthClient(webdav, gowebdav.NewAutoAuth(user, secert))
  21. client.Connect()
  22. // 获取根目录列表
  23. entries, err := client.ReadDir(remote)
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. // 遍历目录结构
  28. for _, entry := range entries {
  29. // log.Println(remote + "/" + entry.Name())
  30. if entry.IsDir() {
  31. // 如果是目录,则递归遍历子目录
  32. success = readDirRecursive(client, remote+"/"+entry.Name(), success)
  33. } else {
  34. pth := rename(client, remote+"/"+entry.Name())
  35. log.Println(pth)
  36. info, _ := client.Read(pth)
  37. str := string(info)
  38. // log.Println(str)
  39. success += strings.Count(str, "文件上传成功!")
  40. }
  41. }
  42. log.Println(company, "上传成功数量为:", success)
  43. }
  44. // 递归遍历目录结构
  45. func readDirRecursive(client *gowebdav.Client, path string, success int) int {
  46. entries, err := client.ReadDir(path)
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. for _, entry := range entries {
  51. if entry.IsDir() {
  52. // 如果是目录,则递归遍历子目录
  53. success = readDirRecursive(client, path+"/"+entry.Name(), success)
  54. } else {
  55. file_path := path + "/" + entry.Name()
  56. if !strings.Contains(file_path, *date) {
  57. continue
  58. }
  59. //计算成功
  60. pth := rename(client, file_path)
  61. info, _ := client.Read(pth)
  62. str := string(info)
  63. // log.Println(str)
  64. success += strings.Count(str, "文件上传成功!")
  65. }
  66. }
  67. return success
  68. }
  69. func rename(client *gowebdav.Client, path string) string {
  70. newpath := path
  71. if strings.Contains(path, ".log") {
  72. newpath = strings.ReplaceAll(path, ".log", ".txt")
  73. client.Rename(path, newpath, true)
  74. }
  75. return newpath
  76. }