watch.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package watch
  2. import (
  3. "fmt"
  4. localCache "hongze/hongtao3_watch/cache"
  5. "hongze/hongtao3_watch/global"
  6. "hongze/hongtao3_watch/utils"
  7. "io/fs"
  8. "os"
  9. "path/filepath"
  10. "syscall"
  11. "time"
  12. )
  13. /*
  14. CREATE动作即临时文件的创建
  15. WRITE写文件动作
  16. CHMOD修改文件属性
  17. REMOVE删除临时文件。
  18. */
  19. //func ListenFolderNew() {
  20. // // 如果没有配置就不监听了
  21. // if global.CONFIG.Serve.ListenExcelPath == `` {
  22. // return
  23. // }
  24. //
  25. // fmt.Println("-----文件夹监听-------")
  26. // watcher, err := fsnotify.NewWatcher()
  27. // if err != nil {
  28. // fmt.Println("fsnotify.NewWatcher err:" + err.Error())
  29. // //log.Fatal(err)
  30. // }
  31. // defer watcher.Close()
  32. //
  33. // done2 := make(chan bool)
  34. // go func() {
  35. // for {
  36. // select {
  37. // case event, ok := <-watcher.Events:
  38. // //fmt.Println("event.Name", event.Name)
  39. // //fmt.Println(event.Op)
  40. // //if ok && event.Op == fsnotify.Create &&
  41. // // !strings.Contains(event.Name, "tmp") &&
  42. // // !strings.Contains(event.Name, ".TMP") &&
  43. // // !strings.Contains(event.Name, "~") &&
  44. // // (strings.Contains(event.Name, "xlsx") || strings.Contains(event.Name, "xls")) {
  45. // // WatchIndexFileRelease(event.Name)
  46. // // fmt.Println("op:event.Name", event.Name)
  47. // //}
  48. //
  49. // // 如果是临时文件,那么就忽略
  50. // if strings.Contains(event.Name, "tmp") ||
  51. // strings.Contains(event.Name, ".TMP") ||
  52. // strings.Contains(event.Name, "~") {
  53. // continue
  54. // }
  55. //
  56. // if ok && event.Op&fsnotify.Create == fsnotify.Create {
  57. // //fmt.Println("新增文件 : ", event.Name)
  58. //
  59. // // 判断是否属于文件夹,如果是文件夹,那么就加上该文件夹的监听权限
  60. // fi, err := os.Stat(event.Name)
  61. // if err == nil {
  62. // if fi.IsDir() {
  63. // watcherDir(watcher, event.Name)
  64. // fmt.Println("添加文件夹,添加监控 : ", event.Name)
  65. // } else {
  66. // // 读取文件
  67. // cache.AddIndexHandleExcel(event.Name)
  68. // }
  69. // }
  70. //
  71. // }
  72. // if ok && event.Op&fsnotify.Write == fsnotify.Write {
  73. // //fmt.Println("写入文件 : ", event.Name)
  74. // // 读取文件
  75. //
  76. // cache.AddIndexHandleExcel(event.Name)
  77. // }
  78. // if ok && event.Op&fsnotify.Remove == fsnotify.Remove {
  79. // //fmt.Println("删除文件 : ", event.Name)
  80. // //如果删除文件是目录,则移除监控
  81. // fi, err := os.Stat(event.Name)
  82. // if err == nil && fi.IsDir() {
  83. // _ = watcher.Remove(event.Name)
  84. // fmt.Println("删除文件夹,删除监控 : ", event.Name)
  85. // }
  86. // }
  87. // if ok && event.Op&fsnotify.Rename == fsnotify.Rename {
  88. // //fmt.Println("重命名文件 : ", event.Name)
  89. // //如果重命名文件是目录,则移除监控
  90. // //注意这里无法使用os.Stat来判断是否是目录了
  91. // //因为重命名后,go已经无法找到原文件来获取信息了
  92. // //所以这里就简单粗爆的直接remove好了
  93. // _ = watcher.Remove(event.Name)
  94. // //fmt.Println("更改文件夹名称,删除监控 : ", event.Name)
  95. // }
  96. // if ok && event.Op&fsnotify.Chmod == fsnotify.Chmod {
  97. // fmt.Println("修改权限 : ", event.Name)
  98. // }
  99. // case err := <-watcher.Errors:
  100. // if err != nil {
  101. // fmt.Println("watcher.Errors:", err)
  102. // //log.Println("error:", err)
  103. // }
  104. // case <-time.After(60 * time.Second):
  105. // continue
  106. // }
  107. // }
  108. // }()
  109. //
  110. // // 开始监听
  111. // watcherDir(watcher, global.CONFIG.Serve.ListenExcelPath)
  112. //
  113. // <-done2
  114. //}
  115. //var watcherDirMap map[string]string
  116. //
  117. //// 添加监听目录
  118. //func watcherDir(watcher *fsnotify.Watcher, dirPath string) {
  119. // err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
  120. // //这里判断是否为目录,只需监控目录即可
  121. // //目录下的文件也在监控范围内,不需要我们一个一个加
  122. // if info.IsDir() {
  123. // subpath, e := filepath.Abs(path)
  124. // if e != nil {
  125. // return e
  126. // }
  127. // err = watcher.Add(subpath)
  128. // if err != nil {
  129. // return err
  130. // }
  131. // // 入库
  132. // if watcherDirMap == nil {
  133. // watcherDirMap = make(map[string]string)
  134. // }
  135. // watcherDirMap[subpath] = subpath
  136. // fmt.Println("监控 : ", subpath)
  137. // }
  138. // return nil
  139. // })
  140. // if err != nil {
  141. // fmt.Println("watcher.Add:" + err.Error())
  142. // //log.Fatal(err)
  143. // }
  144. //}
  145. //
  146. //// HandleFileUpdate 处理文件变化
  147. //func HandleFileUpdate(updateFilePath string) {
  148. // fileExt := filepath.Ext(updateFilePath)
  149. //
  150. // if fileExt != ".xlsx" && fileExt != ".xls" {
  151. // fmt.Println("不是excel文件")
  152. // return
  153. // }
  154. //
  155. // // 读取excel内容并操作入库
  156. // services.ReadExcel(updateFilePath)
  157. //}
  158. func ReadWatchIndexFile() {
  159. var err error
  160. defer func() {
  161. if err != nil {
  162. fmt.Println("ReadWatchIndexFile Err:" + err.Error())
  163. }
  164. }()
  165. if global.CacheClient == nil {
  166. fmt.Println("CacheClient 未导入")
  167. return
  168. }
  169. err = filepath.Walk(global.CONFIG.Serve.ListenExcelPath, func(path string, info fs.FileInfo, err error) error {
  170. if err != nil {
  171. return err
  172. }
  173. if !info.IsDir() {
  174. fileInfo, err := os.Stat(path)
  175. if err != nil {
  176. fmt.Println("os.Stat:", err.Error())
  177. }
  178. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  179. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  180. existModifyTime, ok := global.CacheClient.Get(path)
  181. if ok {
  182. existModifyTimeStr := existModifyTime.(string)
  183. if existModifyTimeStr != modifyTimeStr {
  184. localCache.AddIndexHandleExcel(path)
  185. }
  186. } else {
  187. localCache.AddIndexHandleExcel(path)
  188. }
  189. global.CacheClient.Delete(path)
  190. global.CacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  191. }
  192. return nil
  193. })
  194. }