文件同步
Não pode escolher mais do que 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.

main.go 5.5 KiB

há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. ch <- ("create"+";"+ev.Name)
  75. }
  76. }
  77. if ev.Op&fsnotify.Write == fsnotify.Write {
  78. fmt.Println("写入文件 : ", ev.Name);
  79. //判断文件,发送事件
  80. fi, err := os.Stat(ev.Name);
  81. if err == nil && !fi.IsDir() {
  82. filePath :=ev.Name
  83. abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1)
  84. param :=strings.Split(abs,"\\")
  85. key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1]
  86. log.Println("chan-->"+key)
  87. ch :=config.GobalWatchChannelMap[key]
  88. if ch==nil{
  89. continue
  90. }
  91. ch <- ("write"+";"+ev.Name)
  92. }
  93. }
  94. if ev.Op&fsnotify.Remove == fsnotify.Remove {
  95. fmt.Println("删除文件 : ", ev.Name);
  96. //如果删除文件是目录,则移除监控
  97. fi, err := os.Stat(ev.Name);
  98. if err == nil && fi.IsDir() {
  99. config.GobalWatch.Remove(ev.Name);
  100. fmt.Println("删除监控 : ", ev.Name);
  101. }else{
  102. filePath :=ev.Name
  103. abs := strings.Replace(filePath,config.LocalWorkSpaceDir,"",1)
  104. param :=strings.Split(abs,"\\")
  105. key :=config.LocalWorkSpaceDir+param[0]+"\\"+param[1]
  106. log.Println("chan-->"+key)
  107. ch :=config.GobalWatchChannelMap[key]
  108. if ch==nil{
  109. continue
  110. }
  111. ch <- ("remove"+";"+ev.Name)
  112. }
  113. }
  114. if ev.Op&fsnotify.Rename == fsnotify.Rename {
  115. fmt.Println("重命名文件 : ", ev.Name);
  116. //如果重命名文件是目录,则移除监控
  117. //注意这里无法使用os.Stat来判断是否是目录了
  118. //因为重命名后,go已经无法找到原文件来获取信息了
  119. //所以这里就简单粗爆的直接remove好了
  120. config.GobalWatch.Remove(ev.Name);
  121. }
  122. if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
  123. fmt.Println("修改权限 : ", ev.Name);
  124. }
  125. }
  126. case err := <-config.GobalWatch.Errors:
  127. {
  128. fmt.Println("error : ", err);
  129. return;
  130. }
  131. }
  132. }
  133. }();
  134. defer config.GobalWatch.Close()
  135. go func() {
  136. //客户端心跳检测
  137. log.Println("keeplive")
  138. time.Sleep(20*time.Second)
  139. var keepliveTimeOut int64 =5 //20s
  140. var lastAccept int64 = time.Now().Unix() //秒时间戳
  141. for true{
  142. //log.Print(!websocket.IsKeeplive)
  143. //log.Println((time.Now().Unix()-lastAccept)>keepliveTimeOut)
  144. if !websocket.IsKeeplive {
  145. if (time.Now().Unix()-lastAccept)>keepliveTimeOut{
  146. log.Println("长时间未检测到心跳 Exit")
  147. os.Exit(1)
  148. }
  149. continue
  150. }
  151. lastAccept = time.Now().Unix()
  152. websocket.IsKeeplive = false
  153. //time.Sleep(100*time.Millisecond)
  154. }
  155. }()
  156. //http://localhost:7777/ws
  157. http.HandleFunc("/upload", websocket.UploadHandler)
  158. http.HandleFunc("/subscriptionFileChange", websocket.SubscriptionFileChangeHandler)
  159. http.HandleFunc("/download", websocket.DownloadHandler)
  160. http.HandleFunc("/init", websocket.InitLocalWorkSpaceHandler)
  161. http.HandleFunc("/getFolderFileInfo", websocket.GetFolderFileInfoHandler)
  162. http.HandleFunc("/openFileWith", websocket.OpenFileWithHandler)
  163. http.HandleFunc("/checkForUpdates", websocket.CheckForUpdatesHandler)
  164. http.HandleFunc("/initClientConfig", websocket.InitClientConfigHandler)
  165. //http.HandleFunc("/watchFile", websocket.WatchFileHandler)
  166. http.HandleFunc("/keeplive", websocket.KeepliveHandler)
  167. http.HandleFunc("/queryCommitHistory", websocket.QueryCommitHistoryHandler)
  168. http.HandleFunc("/editCommitHistoryMilestone", websocket.EditCommitHistoryMilestoneHandler)
  169. //服务端启动
  170. log.Println("服务启动成功,监听端口7777,等待连接。")
  171. http.ListenAndServe("0.0.0.0:7777", nil)
  172. }