文件同步
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

194 строки
5.6 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. _ "strings"
  18. "time"
  19. )
  20. func main() {
  21. config.InitConfig()
  22. //日志设置
  23. _,err := os.Stat(config.LocalWorkSpaceDir)
  24. if err != nil {
  25. //创建文件目录
  26. os.MkdirAll(config.LocalWorkSpaceDir, os.ModePerm)
  27. }
  28. time.Now().Month().String()
  29. logpath :=config.LocalWorkSpaceDir+"\\"+"fts_"+strconv.Itoa(time.Now().Year())+time.Now().Month().String()+".log"
  30. logFile, err := os.OpenFile(logpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
  31. if err != nil {
  32. log.Printf("open log file failed, err:", err)
  33. return
  34. }
  35. multiWriter := io.MultiWriter(os.Stdout,logFile)
  36. log.SetOutput(multiWriter)
  37. log.SetPrefix("[fts] ")
  38. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  39. //handle.InitLocalWorkSpace(nil,"324523676458291200","11.4")
  40. //文件监控
  41. config.GobalWatch, err = fsnotify.NewWatcher()
  42. if err != nil {
  43. log.Println(err)
  44. }
  45. go func() {
  46. for {
  47. select {
  48. case ev := <-config.GobalWatch.Events:
  49. {
  50. log.Println(ev.Op.String()+":"+ev.Name)
  51. if filepath.Ext(ev.Name)==".commit"{
  52. continue
  53. }
  54. if strings.Index(ev.Name,"历史版本查看")>0{
  55. continue
  56. }
  57. if ev.Op&fsnotify.Create == fsnotify.Create {
  58. fmt.Println("创建文件 : ", ev.Name);
  59. //这里获取新创建文件的信息,如果是目录,则加入监控中
  60. fi, err := os.Stat(ev.Name);
  61. if err == nil && fi.IsDir() {
  62. config.GobalWatch.Add(ev.Name);
  63. fmt.Println("添加监控 : ", ev.Name);
  64. }else{
  65. filePath :=ev.Name
  66. abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1)
  67. param :=strings.Split(abs,"\\")
  68. key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1]
  69. log.Println("chan-->"+key)
  70. ch :=config.GobalWatchChannelMap[key]
  71. if ch==nil{
  72. continue
  73. }
  74. log.Println("chan-->"+("create"+";"+ev.Name))
  75. ch <- ("create"+";"+ev.Name)
  76. }
  77. }
  78. if ev.Op&fsnotify.Write == fsnotify.Write {
  79. fmt.Println("写入文件 : ", ev.Name);
  80. //判断文件,发送事件
  81. fi, err := os.Stat(ev.Name);
  82. if err == nil && !fi.IsDir() {
  83. filePath :=ev.Name
  84. abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1)
  85. param :=strings.Split(abs,"\\")
  86. key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1]
  87. log.Println("chan-->"+key)
  88. ch :=config.GobalWatchChannelMap[key]
  89. if ch==nil{
  90. continue
  91. }
  92. log.Println("chan-->"+("write"+";"+ev.Name))
  93. log.Println(ch)
  94. ch <- ("write"+";"+ev.Name)
  95. }
  96. }
  97. if ev.Op&fsnotify.Remove == fsnotify.Remove {
  98. fmt.Println("删除文件 : ", ev.Name);
  99. //如果删除文件是目录,则移除监控
  100. fi, err := os.Stat(ev.Name);
  101. if err == nil && fi.IsDir() {
  102. config.GobalWatch.Remove(ev.Name);
  103. fmt.Println("删除监控 : ", ev.Name);
  104. }else{
  105. filePath :=ev.Name
  106. abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1)
  107. param :=strings.Split(abs,"\\")
  108. key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1]
  109. ch :=config.GobalWatchChannelMap[key]
  110. if ch==nil{
  111. continue
  112. }
  113. log.Println("chan-->"+("remove"+";"+ev.Name))
  114. ch <- ("remove"+";"+ev.Name)
  115. }
  116. }
  117. if ev.Op&fsnotify.Rename == fsnotify.Rename {
  118. fmt.Println("重命名文件 : ", ev.Name);
  119. //如果重命名文件是目录,则移除监控
  120. //注意这里无法使用os.Stat来判断是否是目录了
  121. //因为重命名后,go已经无法找到原文件来获取信息了
  122. //所以这里就简单粗爆的直接remove好了
  123. config.GobalWatch.Remove(ev.Name);
  124. }
  125. if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
  126. fmt.Println("修改权限 : ", ev.Name);
  127. }
  128. }
  129. case err := <-config.GobalWatch.Errors:
  130. {
  131. fmt.Println("error : ", err);
  132. return;
  133. }
  134. }
  135. }
  136. }();
  137. defer config.GobalWatch.Close()
  138. go func() {
  139. //客户端心跳检测
  140. log.Println("keeplive")
  141. time.Sleep(20*time.Second)
  142. var keepliveTimeOut int64 =5 //20s
  143. var lastAccept int64 = time.Now().Unix() //秒时间戳
  144. for true{
  145. //log.Print(!websocket.IsKeeplive)
  146. //log.Println((time.Now().Unix()-lastAccept)>keepliveTimeOut)
  147. if !websocket.IsKeeplive {
  148. if (time.Now().Unix()-lastAccept)>keepliveTimeOut{
  149. log.Println("长时间未检测到心跳 Exit")
  150. os.Exit(1)
  151. }
  152. continue
  153. }
  154. lastAccept = time.Now().Unix()
  155. websocket.IsKeeplive = false
  156. //time.Sleep(100*time.Millisecond)
  157. }
  158. }()
  159. //http://localhost:7777/ws
  160. http.HandleFunc("/upload", websocket.UploadHandler)
  161. http.HandleFunc("/subscriptionFileChange", websocket.SubscriptionFileChangeHandler)
  162. http.HandleFunc("/download", websocket.DownloadHandler)
  163. http.HandleFunc("/init", websocket.InitLocalWorkSpaceHandler)
  164. http.HandleFunc("/getFolderFileInfo", websocket.GetFolderFileInfoHandler)
  165. http.HandleFunc("/openFileWith", websocket.OpenFileWithHandler)
  166. http.HandleFunc("/checkForUpdates", websocket.CheckForUpdatesHandler)
  167. http.HandleFunc("/initClientConfig", websocket.InitClientConfigHandler)
  168. //http.HandleFunc("/watchFile", websocket.WatchFileHandler)
  169. http.HandleFunc("/keeplive", websocket.KeepliveHandler)
  170. http.HandleFunc("/queryCommitHistory", websocket.QueryCommitHistoryHandler)
  171. http.HandleFunc("/editCommitHistoryMilestone", websocket.EditCommitHistoryMilestoneHandler)
  172. //服务端启动
  173. log.Println("服务启动成功,监听端口7777,等待连接。")
  174. http.ListenAndServe("0.0.0.0:7777", nil)
  175. }