main.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "app.yhyue.com/moapp/jybase/common"
  12. "github.com/studio-b12/gowebdav"
  13. )
  14. var (
  15. webdav = "https://cloudreve.jydev.jianyu360.com/dav"
  16. user = "jichunling@topnet.net.cn"
  17. // secert = "imq6aZfwvWdu9XRsd368uywaalD5Jgwf" //作废
  18. secert = "WFDoZThbvx5TsKRQAOhTcgyjpQdNs0C1"
  19. filename = "./upload.txt"
  20. // waibao = "华软"
  21. // waibao = "中电科"
  22. waibao = "机电学院"
  23. )
  24. func SpitHref(path string) string {
  25. result := filepath.Dir(path)
  26. result = strings.Replace(result, `\`, `/`, -1)
  27. return result
  28. }
  29. var (
  30. localPath = flag.String("localPath", "", "本地路径")
  31. cloudPath = flag.String("cloudPath", "", "云盘路径")
  32. date = flag.String("date", "", "年月日,例如,2024_04")
  33. )
  34. //日志上传
  35. func logUpload(client *gowebdav.Client) {
  36. c := "/云盘管理/日志/" + waibao
  37. filepath.WalkDir("./jylog", func(path string, info os.DirEntry, err error) error {
  38. if err != nil {
  39. return nil
  40. }
  41. if !info.IsDir() {
  42. l := path
  43. c := c + "/" + strings.ReplaceAll(path, `\`, `/`)
  44. UploadYunPan(client, l, c, nil)
  45. }
  46. return nil
  47. })
  48. return
  49. }
  50. func main() {
  51. flag.Parse()
  52. defer common.Catch()
  53. Init()
  54. time.Sleep(500 * time.Millisecond)
  55. txt := `请悉知:
  56. 该文件调用需要两个参数:localPath 、cloudPath
  57. 参数说明:
  58. localPath:本地需要上传的路径 ,必须精确到年份 例: D:/jianyu/data/2024年
  59. cloudPath:云盘需要上传的路径 ,必须精确到年份 例: 2024年
  60. 本地上传路径必须满足相关目录树结构:
  61. 年份/省份/城市/区县/类型/文件
  62. 年份/省份/城市/类型/文件
  63. 类型为:学校、政府、医院等
  64. `
  65. fmt.Println(txt)
  66. if *localPath == "" && *cloudPath == "" {
  67. log.Println("参数错误")
  68. return
  69. } else if *localPath == "" {
  70. log.Println("本地路径设置错误")
  71. return
  72. } else if *cloudPath == "" {
  73. log.Println("云盘路径设置错误")
  74. return
  75. }
  76. log.Println("本地目录:", *localPath)
  77. log.Println("远程目录:", *cloudPath)
  78. // init
  79. client := gowebdav.NewAuthClient(webdav, gowebdav.NewAutoAuth(user, secert))
  80. client.Connect()
  81. m := LoadingPath(client, webdav, user, secert, "")
  82. // localPath := `D:/jianyu/data/202/2021_test1`
  83. // localPath := `D:/jianyu/data/202/2021_test1/2024年`
  84. // webdavPath := `2021_test`
  85. lp := *localPath
  86. cp := *cloudPath
  87. lp = strings.ReplaceAll(lp, `\`, `/`)
  88. cp = strings.ReplaceAll(cp, `\`, `/`)
  89. //文件上传
  90. uploadDirectory_new(client, lp, cp, m)
  91. //日志上传
  92. logUpload(client)
  93. fmt.Println("结束")
  94. }
  95. //加载目录树
  96. func LoadingPath(client *gowebdav.Client, webdav, user, secert, remote string) map[string]bool {
  97. m := map[string]bool{}
  98. // 获取文件内容
  99. filePath := "/云盘管理/预算目录(千万不要删).txt"
  100. content, err := client.Read(filePath)
  101. if err != nil {
  102. log.Println("未找到文件路径", err)
  103. return m
  104. }
  105. // 将文件内容转换为字符串
  106. contentStr := string(content)
  107. // fmt.Println("File content:")
  108. arr := strings.Split(contentStr, "\n")
  109. if len(arr) > 1 {
  110. year := strings.Split(Replace_hh(arr[0]), "、")
  111. hrefType := strings.Split(Replace_hh(arr[1]), "、")
  112. // log.Println(year)
  113. // log.Println(hrefType)
  114. for k, v := range arr {
  115. for _, vv := range year {
  116. for _, vvv := range hrefType {
  117. if v == "" || k < 2 {
  118. continue
  119. }
  120. href := Replace_hh(fmt.Sprintf(v, vv)) + Replace_hh(vvv)
  121. m[href] = true
  122. }
  123. }
  124. }
  125. }
  126. log.Println("路径载入完毕")
  127. return m
  128. }
  129. //上传
  130. func uploadDirectory_new(client *gowebdav.Client, localPath string, webdavPath string, pathMap map[string]bool) error {
  131. //已上传的
  132. alreadyUpload, _ := readKeysFromFile(filename)
  133. fileUploadTxt, _ := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
  134. defer fileUploadTxt.Close()
  135. // failPathMap := map[string]bool{}
  136. var err error
  137. // m := map[string]bool{}
  138. // city := ""
  139. // 遍历本地目录下的文件和子目录
  140. err = filepath.Walk(localPath, func(path string, info os.FileInfo, err error) error {
  141. if err != nil {
  142. return err
  143. }
  144. //目录
  145. if !info.IsDir() {
  146. //文件
  147. // 如果是文件,则构建远程文件路径并上传文件
  148. relativePath, _ := filepath.Rel(localPath, path)
  149. // log.Println("~~~~~~~~", webdavPath, "==", relativePath)
  150. remotePath := filepath.Join(webdavPath, relativePath)
  151. // log.Println("~~~~~~~~", remotePath)
  152. remotePath = Replace_path(remotePath)
  153. path = Replace_path(path)
  154. //判断是否是规定路径
  155. spitpath := SpitHref(remotePath)
  156. if !pathMap[spitpath] {
  157. log.Println("上传的文件不符合规定路径,请检查:", spitpath)
  158. fmt.Println("上传的文件不符合规定路径,请检查:", spitpath)
  159. } else {
  160. pth := SpitFilePath(remotePath)
  161. if !alreadyUpload[pth] {
  162. //
  163. time.Sleep(time.Millisecond * 200)
  164. UploadYunPan(client, path, remotePath, fileUploadTxt)
  165. alreadyUpload[pth] = true
  166. } else {
  167. log.Println("已经上传过,过滤", pth)
  168. fmt.Println("已经上传过,过滤", pth)
  169. }
  170. }
  171. }
  172. return nil
  173. })
  174. return err
  175. }
  176. //切割path
  177. func SpitFilePath(path string) string {
  178. index := strings.Index(path, "/")
  179. if index != -1 && index < len(path)-1 {
  180. return path[index+1:]
  181. }
  182. return path
  183. }
  184. //读取已上传的文件
  185. func readKeysFromFile(filename string) (map[string]bool, error) {
  186. result := map[string]bool{}
  187. content, err := ioutil.ReadFile(filename)
  188. if err != nil {
  189. return result, err
  190. }
  191. lines := strings.Split(string(content), "\n")
  192. for _, line := range lines {
  193. line = strings.TrimSpace(line)
  194. if line != "" {
  195. result[line] = true
  196. }
  197. }
  198. return result, nil
  199. }
  200. func UploadYunPan(client *gowebdav.Client, localFilePath, cloudFilePath string, fileUploadTxt *os.File) {
  201. // log.Println("localFilePath:", localFilePath)
  202. // log.Println("cloudFilePath:", cloudFilePath)
  203. localFilePath = strings.ReplaceAll(localFilePath, `\`, `/`)
  204. cloudFilePath = strings.ReplaceAll(cloudFilePath, `\`, `/`)
  205. // log.Println("---cloudFilePath", cloudFilePath)
  206. // return
  207. // 打开本地文件并读取文件内容
  208. file, err := os.Open(localFilePath)
  209. if err != nil {
  210. log.Println("打开本地文件失败:", err)
  211. return
  212. }
  213. defer file.Close()
  214. data, err := ioutil.ReadAll(file)
  215. if err != nil {
  216. log.Println("读取本地文件内容失败:", err)
  217. return
  218. }
  219. // 将文件上传到云盘
  220. err = client.Write(cloudFilePath, data, 0644)
  221. if err != nil {
  222. time.Sleep(time.Second * 2)
  223. for i := 1; i <= 5; i++ {
  224. err = client.Write(cloudFilePath, data, 0644)
  225. if err == nil {
  226. break
  227. }
  228. }
  229. if err != nil {
  230. log.Println("上传文件到云盘失败:", err)
  231. }
  232. return
  233. }
  234. //监控日志过滤,不打印
  235. if !strings.Contains(cloudFilePath, "云盘管理") {
  236. log.Println("文件上传成功!", cloudFilePath)
  237. fmt.Println("文件上传成功!", cloudFilePath)
  238. }
  239. if fileUploadTxt != nil {
  240. fileUploadTxt.WriteString(SpitFilePath(cloudFilePath) + "\n")
  241. }
  242. }