main.go 7.7 KB

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