文件同步
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 rivejä
5.4 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. _,err := os.Stat(config.LocalWorkSpaceDir)
  21. if err != nil {
  22. //创建文件目录
  23. os.MkdirAll(config.LocalWorkSpaceDir, os.ModePerm)
  24. }
  25. logpath :=config.LocalWorkSpaceDir+"\\"+"fts.log"
  26. logFile, err := os.OpenFile(logpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
  27. if err != nil {
  28. log.Printf("open log file failed, err:", err)
  29. return
  30. }
  31. multiWriter := io.MultiWriter(os.Stdout,logFile)
  32. log.SetOutput(multiWriter)
  33. log.SetPrefix("[fts] ")
  34. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  35. //handle.InitLocalWorkSpace(nil,"324523676458291200","11.4")
  36. //文件监控
  37. /*config.GobalWatch, err = fsnotify.NewWatcher()
  38. if err != nil {
  39. log.Println(err)
  40. }
  41. go func() {
  42. for {
  43. select {
  44. case ev := <-config.GobalWatch.Events:
  45. {
  46. log.Println(ev.Op.String()+":"+ev.Name)
  47. if ev.Op&fsnotify.Create == fsnotify.Create {
  48. fmt.Println("创建文件 : ", ev.Name);
  49. //这里获取新创建文件的信息,如果是目录,则加入监控中
  50. fi, err := os.Stat(ev.Name);
  51. if err == nil && fi.IsDir() {
  52. config.GobalWatch.Add(ev.Name);
  53. fmt.Println("添加监控 : ", ev.Name);
  54. }
  55. }
  56. if ev.Op&fsnotify.Write == fsnotify.Write {
  57. fmt.Println("写入文件 : ", ev.Name);
  58. }
  59. if ev.Op&fsnotify.Remove == fsnotify.Remove {
  60. fmt.Println("删除文件 : ", ev.Name);
  61. //如果删除文件是目录,则移除监控
  62. fi, err := os.Stat(ev.Name);
  63. if err == nil && fi.IsDir() {
  64. config.GobalWatch.Remove(ev.Name);
  65. fmt.Println("删除监控 : ", ev.Name);
  66. }
  67. }
  68. if ev.Op&fsnotify.Rename == fsnotify.Rename {
  69. fmt.Println("重命名文件 : ", ev.Name);
  70. //如果重命名文件是目录,则移除监控
  71. //注意这里无法使用os.Stat来判断是否是目录了
  72. //因为重命名后,go已经无法找到原文件来获取信息了
  73. //所以这里就简单粗爆的直接remove好了
  74. config.GobalWatch.Remove(ev.Name);
  75. }
  76. if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
  77. fmt.Println("修改权限 : ", ev.Name);
  78. }
  79. }
  80. case err := <-config.GobalWatch.Errors:
  81. {
  82. fmt.Println("error : ", err);
  83. return;
  84. }
  85. }
  86. }
  87. }();*/
  88. //defer config.GobalWatch.Close()
  89. //http://localhost:7777/ws
  90. http.HandleFunc("/upload", websocket.UploadHandler)
  91. http.HandleFunc("/subscriptionFileChange", websocket.SubscriptionFileChangeHandler)
  92. http.HandleFunc("/download", websocket.DownloadHandler)
  93. http.HandleFunc("/init", websocket.InitLocalWorkSpaceHandler)
  94. http.HandleFunc("/getFolderFileInfo", websocket.GetFolderFileInfoHandler)
  95. http.HandleFunc("/openFileWith", websocket.OpenFileWithHandler)
  96. http.HandleFunc("/checkForUpdates", websocket.CheckForUpdatesHandler)
  97. http.HandleFunc("/initClientConfig", websocket.InitClientConfigHandler)
  98. http.HandleFunc("/watchFile", websocket.WatchFileHandler)
  99. //服务端启动
  100. log.Println("服务启动成功,监听端口7777,等待连接。")
  101. http.ListenAndServe("0.0.0.0:7777", nil)
  102. }
  103. //func main() {
  104. // watch, _ := fsnotify.NewWatcher()
  105. // w := Watch{
  106. // watch: watch,
  107. // }
  108. // w.watchDir("C:\\Users\\yuan_rh\\easycloud\\324523676458291200");
  109. // select {};
  110. //}
  111. type Watch struct {
  112. watch *fsnotify.Watcher;
  113. }
  114. //监控目录
  115. func (w *Watch) watchDir(dir string) {
  116. //通过Walk来遍历目录下的所有子目录
  117. filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  118. //这里判断是否为目录,只需监控目录即可
  119. //目录下的文件也在监控范围内,不需要我们一个一个加
  120. if info.IsDir() {
  121. path, err := filepath.Abs(path);
  122. if err != nil {
  123. return err;
  124. }
  125. err = w.watch.Add(path);
  126. if err != nil {
  127. return err;
  128. }
  129. fmt.Println("监控目录 : ", path);
  130. }
  131. return nil;
  132. });
  133. go func() {
  134. for {
  135. select {
  136. case ev := <-w.watch.Events:
  137. {
  138. if ev.Op&fsnotify.Create == fsnotify.Create {
  139. fmt.Println("创建文件 : ", ev.Name);
  140. //这里获取新创建文件的信息,如果是目录,则加入监控中
  141. fi, err := os.Stat(ev.Name);
  142. if err == nil && fi.IsDir() {
  143. w.watch.Add(ev.Name);
  144. fmt.Println("添加监控 : ", ev.Name);
  145. }
  146. }
  147. if ev.Op&fsnotify.Write == fsnotify.Write {
  148. fmt.Println("写入文件 : ", ev.Name);
  149. }
  150. if ev.Op&fsnotify.Remove == fsnotify.Remove {
  151. fmt.Println("删除文件 : ", ev.Name);
  152. //如果删除文件是目录,则移除监控
  153. fi, err := os.Stat(ev.Name);
  154. if err == nil && fi.IsDir() {
  155. w.watch.Remove(ev.Name);
  156. fmt.Println("删除监控 : ", ev.Name);
  157. }
  158. }
  159. if ev.Op&fsnotify.Rename == fsnotify.Rename {
  160. fmt.Println("重命名文件 : ", ev.Name);
  161. //如果重命名文件是目录,则移除监控
  162. //注意这里无法使用os.Stat来判断是否是目录了
  163. //因为重命名后,go已经无法找到原文件来获取信息了
  164. //所以这里就简单粗爆的直接remove好了
  165. w.watch.Remove(ev.Name);
  166. }
  167. if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
  168. fmt.Println("修改权限 : ", ev.Name);
  169. }
  170. }
  171. case err := <-w.watch.Errors:
  172. {
  173. fmt.Println("error : ", err);
  174. return;
  175. }
  176. }
  177. }
  178. }();
  179. }