文件同步
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

128 linhas
3.3 KiB

  1. package main
  2. import (
  3. "fmt"
  4. _ "fmt"
  5. "fts/config"
  6. "fts/websocket"
  7. "github.com/fsnotify/fsnotify"
  8. _ "github.com/ipfs/go-ipfs-api"
  9. "io"
  10. "log"
  11. "net/http"
  12. "os"
  13. _ "os"
  14. "path/filepath"
  15. _ "strings"
  16. )
  17. func main() {
  18. config.InitConfig()
  19. //日志设置
  20. logpath :=config.LocalWorkSpaceDir+"\\"+"fts.log"
  21. logFile, err := os.OpenFile(logpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
  22. if err != nil {
  23. log.Printf("open log file failed, err:", err)
  24. return
  25. }
  26. multiWriter := io.MultiWriter(os.Stdout,logFile)
  27. log.SetOutput(multiWriter)
  28. log.SetPrefix("[fts] ")
  29. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  30. //handle.InitLocalWorkSpace(nil,"324523676458291200","11.4")
  31. //http://localhost:7777/ws
  32. http.HandleFunc("/upload", websocket.UploadHandler)
  33. http.HandleFunc("/subscriptionFileChange", websocket.SubscriptionFileChangeHandler)
  34. http.HandleFunc("/download", websocket.DownloadHandler)
  35. http.HandleFunc("/init", websocket.InitLocalWorkSpaceHandler)
  36. //服务端启动
  37. log.Println("服务启动成功,监听端口7777,等待连接。")
  38. http.ListenAndServe("0.0.0.0:7777", nil)
  39. }
  40. //func main() {
  41. // watch, _ := fsnotify.NewWatcher()
  42. // w := Watch{
  43. // watch: watch,
  44. // }
  45. // w.watchDir("C:\\Users\\yuan_rh\\easycloud\\324523676458291200");
  46. // select {};
  47. //}
  48. type Watch struct {
  49. watch *fsnotify.Watcher;
  50. }
  51. //监控目录
  52. func (w *Watch) watchDir(dir string) {
  53. //通过Walk来遍历目录下的所有子目录
  54. filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  55. //这里判断是否为目录,只需监控目录即可
  56. //目录下的文件也在监控范围内,不需要我们一个一个加
  57. if info.IsDir() {
  58. path, err := filepath.Abs(path);
  59. if err != nil {
  60. return err;
  61. }
  62. err = w.watch.Add(path);
  63. if err != nil {
  64. return err;
  65. }
  66. fmt.Println("监控 : ", path);
  67. }
  68. return nil;
  69. });
  70. go func() {
  71. for {
  72. select {
  73. case ev := <-w.watch.Events:
  74. {
  75. if ev.Op&fsnotify.Create == fsnotify.Create {
  76. fmt.Println("创建文件 : ", ev.Name);
  77. //这里获取新创建文件的信息,如果是目录,则加入监控中
  78. fi, err := os.Stat(ev.Name);
  79. if err == nil && fi.IsDir() {
  80. w.watch.Add(ev.Name);
  81. fmt.Println("添加监控 : ", ev.Name);
  82. }
  83. }
  84. if ev.Op&fsnotify.Write == fsnotify.Write {
  85. fmt.Println("写入文件 : ", ev.Name);
  86. }
  87. if ev.Op&fsnotify.Remove == fsnotify.Remove {
  88. fmt.Println("删除文件 : ", ev.Name);
  89. //如果删除文件是目录,则移除监控
  90. fi, err := os.Stat(ev.Name);
  91. if err == nil && fi.IsDir() {
  92. w.watch.Remove(ev.Name);
  93. fmt.Println("删除监控 : ", ev.Name);
  94. }
  95. }
  96. if ev.Op&fsnotify.Rename == fsnotify.Rename {
  97. fmt.Println("重命名文件 : ", ev.Name);
  98. //如果重命名文件是目录,则移除监控
  99. //注意这里无法使用os.Stat来判断是否是目录了
  100. //因为重命名后,go已经无法找到原文件来获取信息了
  101. //所以这里就简单粗爆的直接remove好了
  102. w.watch.Remove(ev.Name);
  103. }
  104. if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
  105. fmt.Println("修改权限 : ", ev.Name);
  106. }
  107. }
  108. case err := <-w.watch.Errors:
  109. {
  110. fmt.Println("error : ", err);
  111. return;
  112. }
  113. }
  114. }
  115. }();
  116. }