文件同步
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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