|
@@ -2,13 +2,13 @@ package watch
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
- "github.com/fsnotify/fsnotify"
|
|
|
- "hongze/hongtao3_watch/cache"
|
|
|
+ localCache "hongze/hongtao3_watch/cache"
|
|
|
"hongze/hongtao3_watch/global"
|
|
|
- "hongze/hongtao3_watch/services"
|
|
|
+ "hongze/hongtao3_watch/utils"
|
|
|
+ "io/fs"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
- "strings"
|
|
|
+ "syscall"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -19,143 +19,182 @@ CHMOD修改文件属性
|
|
|
REMOVE删除临时文件。
|
|
|
*/
|
|
|
|
|
|
-func ListenFolderNew() {
|
|
|
- // 如果没有配置就不监听了
|
|
|
- if global.CONFIG.Serve.ListenExcelPath == `` {
|
|
|
+//func ListenFolderNew() {
|
|
|
+// // 如果没有配置就不监听了
|
|
|
+// if global.CONFIG.Serve.ListenExcelPath == `` {
|
|
|
+// return
|
|
|
+// }
|
|
|
+//
|
|
|
+// fmt.Println("-----文件夹监听-------")
|
|
|
+// watcher, err := fsnotify.NewWatcher()
|
|
|
+// if err != nil {
|
|
|
+// fmt.Println("fsnotify.NewWatcher err:" + err.Error())
|
|
|
+// //log.Fatal(err)
|
|
|
+// }
|
|
|
+// defer watcher.Close()
|
|
|
+//
|
|
|
+// done2 := make(chan bool)
|
|
|
+// go func() {
|
|
|
+// for {
|
|
|
+// select {
|
|
|
+// case event, ok := <-watcher.Events:
|
|
|
+// //fmt.Println("event.Name", event.Name)
|
|
|
+// //fmt.Println(event.Op)
|
|
|
+// //if ok && event.Op == fsnotify.Create &&
|
|
|
+// // !strings.Contains(event.Name, "tmp") &&
|
|
|
+// // !strings.Contains(event.Name, ".TMP") &&
|
|
|
+// // !strings.Contains(event.Name, "~") &&
|
|
|
+// // (strings.Contains(event.Name, "xlsx") || strings.Contains(event.Name, "xls")) {
|
|
|
+// // WatchIndexFileRelease(event.Name)
|
|
|
+// // fmt.Println("op:event.Name", event.Name)
|
|
|
+// //}
|
|
|
+//
|
|
|
+// // 如果是临时文件,那么就忽略
|
|
|
+// if strings.Contains(event.Name, "tmp") ||
|
|
|
+// strings.Contains(event.Name, ".TMP") ||
|
|
|
+// strings.Contains(event.Name, "~") {
|
|
|
+// continue
|
|
|
+// }
|
|
|
+//
|
|
|
+// if ok && event.Op&fsnotify.Create == fsnotify.Create {
|
|
|
+// //fmt.Println("新增文件 : ", event.Name)
|
|
|
+//
|
|
|
+// // 判断是否属于文件夹,如果是文件夹,那么就加上该文件夹的监听权限
|
|
|
+// fi, err := os.Stat(event.Name)
|
|
|
+// if err == nil {
|
|
|
+// if fi.IsDir() {
|
|
|
+// watcherDir(watcher, event.Name)
|
|
|
+// fmt.Println("添加文件夹,添加监控 : ", event.Name)
|
|
|
+// } else {
|
|
|
+// // 读取文件
|
|
|
+// cache.AddIndexHandleExcel(event.Name)
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+// if ok && event.Op&fsnotify.Write == fsnotify.Write {
|
|
|
+// //fmt.Println("写入文件 : ", event.Name)
|
|
|
+// // 读取文件
|
|
|
+//
|
|
|
+// cache.AddIndexHandleExcel(event.Name)
|
|
|
+// }
|
|
|
+// if ok && event.Op&fsnotify.Remove == fsnotify.Remove {
|
|
|
+// //fmt.Println("删除文件 : ", event.Name)
|
|
|
+// //如果删除文件是目录,则移除监控
|
|
|
+// fi, err := os.Stat(event.Name)
|
|
|
+// if err == nil && fi.IsDir() {
|
|
|
+// _ = watcher.Remove(event.Name)
|
|
|
+// fmt.Println("删除文件夹,删除监控 : ", event.Name)
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if ok && event.Op&fsnotify.Rename == fsnotify.Rename {
|
|
|
+// //fmt.Println("重命名文件 : ", event.Name)
|
|
|
+// //如果重命名文件是目录,则移除监控
|
|
|
+// //注意这里无法使用os.Stat来判断是否是目录了
|
|
|
+// //因为重命名后,go已经无法找到原文件来获取信息了
|
|
|
+// //所以这里就简单粗爆的直接remove好了
|
|
|
+// _ = watcher.Remove(event.Name)
|
|
|
+// //fmt.Println("更改文件夹名称,删除监控 : ", event.Name)
|
|
|
+// }
|
|
|
+// if ok && event.Op&fsnotify.Chmod == fsnotify.Chmod {
|
|
|
+// fmt.Println("修改权限 : ", event.Name)
|
|
|
+// }
|
|
|
+// case err := <-watcher.Errors:
|
|
|
+// if err != nil {
|
|
|
+// fmt.Println("watcher.Errors:", err)
|
|
|
+// //log.Println("error:", err)
|
|
|
+// }
|
|
|
+// case <-time.After(60 * time.Second):
|
|
|
+// continue
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }()
|
|
|
+//
|
|
|
+// // 开始监听
|
|
|
+// watcherDir(watcher, global.CONFIG.Serve.ListenExcelPath)
|
|
|
+//
|
|
|
+// <-done2
|
|
|
+//}
|
|
|
+
|
|
|
+//var watcherDirMap map[string]string
|
|
|
+//
|
|
|
+//// 添加监听目录
|
|
|
+//func watcherDir(watcher *fsnotify.Watcher, dirPath string) {
|
|
|
+// err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
|
|
|
+// //这里判断是否为目录,只需监控目录即可
|
|
|
+// //目录下的文件也在监控范围内,不需要我们一个一个加
|
|
|
+// if info.IsDir() {
|
|
|
+// subpath, e := filepath.Abs(path)
|
|
|
+// if e != nil {
|
|
|
+// return e
|
|
|
+// }
|
|
|
+// err = watcher.Add(subpath)
|
|
|
+// if err != nil {
|
|
|
+// return err
|
|
|
+// }
|
|
|
+// // 入库
|
|
|
+// if watcherDirMap == nil {
|
|
|
+// watcherDirMap = make(map[string]string)
|
|
|
+// }
|
|
|
+// watcherDirMap[subpath] = subpath
|
|
|
+// fmt.Println("监控 : ", subpath)
|
|
|
+// }
|
|
|
+// return nil
|
|
|
+// })
|
|
|
+// if err != nil {
|
|
|
+// fmt.Println("watcher.Add:" + err.Error())
|
|
|
+// //log.Fatal(err)
|
|
|
+// }
|
|
|
+//}
|
|
|
+//
|
|
|
+//// HandleFileUpdate 处理文件变化
|
|
|
+//func HandleFileUpdate(updateFilePath string) {
|
|
|
+// fileExt := filepath.Ext(updateFilePath)
|
|
|
+//
|
|
|
+// if fileExt != ".xlsx" && fileExt != ".xls" {
|
|
|
+// fmt.Println("不是excel文件")
|
|
|
+// return
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 读取excel内容并操作入库
|
|
|
+// services.ReadExcel(updateFilePath)
|
|
|
+//}
|
|
|
+
|
|
|
+func ReadWatchIndexFile() {
|
|
|
+ var err error
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("ReadWatchIndexFile Err:" + err.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ if global.CacheClient == nil {
|
|
|
+ fmt.Println("CacheClient 未导入")
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
- fmt.Println("-----文件夹监听-------")
|
|
|
- watcher, err := fsnotify.NewWatcher()
|
|
|
- if err != nil {
|
|
|
- fmt.Println("fsnotify.NewWatcher err:" + err.Error())
|
|
|
- //log.Fatal(err)
|
|
|
- }
|
|
|
- defer watcher.Close()
|
|
|
-
|
|
|
- done2 := make(chan bool)
|
|
|
- go func() {
|
|
|
- for {
|
|
|
- select {
|
|
|
- case event, ok := <-watcher.Events:
|
|
|
- //fmt.Println("event.Name", event.Name)
|
|
|
- //fmt.Println(event.Op)
|
|
|
- //if ok && event.Op == fsnotify.Create &&
|
|
|
- // !strings.Contains(event.Name, "tmp") &&
|
|
|
- // !strings.Contains(event.Name, ".TMP") &&
|
|
|
- // !strings.Contains(event.Name, "~") &&
|
|
|
- // (strings.Contains(event.Name, "xlsx") || strings.Contains(event.Name, "xls")) {
|
|
|
- // WatchIndexFileRelease(event.Name)
|
|
|
- // fmt.Println("op:event.Name", event.Name)
|
|
|
- //}
|
|
|
-
|
|
|
- // 如果是临时文件,那么就忽略
|
|
|
- if strings.Contains(event.Name, "tmp") ||
|
|
|
- strings.Contains(event.Name, ".TMP") ||
|
|
|
- strings.Contains(event.Name, "~") {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- if ok && event.Op&fsnotify.Create == fsnotify.Create {
|
|
|
- //fmt.Println("新增文件 : ", event.Name)
|
|
|
-
|
|
|
- // 判断是否属于文件夹,如果是文件夹,那么就加上该文件夹的监听权限
|
|
|
- fi, err := os.Stat(event.Name)
|
|
|
- if err == nil {
|
|
|
- if fi.IsDir() {
|
|
|
- watcherDir(watcher, event.Name)
|
|
|
- fmt.Println("添加文件夹,添加监控 : ", event.Name)
|
|
|
- } else {
|
|
|
- // 读取文件
|
|
|
- cache.AddIndexHandleExcel(event.Name)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- if ok && event.Op&fsnotify.Write == fsnotify.Write {
|
|
|
- //fmt.Println("写入文件 : ", event.Name)
|
|
|
- // 读取文件
|
|
|
-
|
|
|
- cache.AddIndexHandleExcel(event.Name)
|
|
|
- }
|
|
|
- if ok && event.Op&fsnotify.Remove == fsnotify.Remove {
|
|
|
- //fmt.Println("删除文件 : ", event.Name)
|
|
|
- //如果删除文件是目录,则移除监控
|
|
|
- fi, err := os.Stat(event.Name)
|
|
|
- if err == nil && fi.IsDir() {
|
|
|
- _ = watcher.Remove(event.Name)
|
|
|
- fmt.Println("删除文件夹,删除监控 : ", event.Name)
|
|
|
- }
|
|
|
- }
|
|
|
- if ok && event.Op&fsnotify.Rename == fsnotify.Rename {
|
|
|
- //fmt.Println("重命名文件 : ", event.Name)
|
|
|
- //如果重命名文件是目录,则移除监控
|
|
|
- //注意这里无法使用os.Stat来判断是否是目录了
|
|
|
- //因为重命名后,go已经无法找到原文件来获取信息了
|
|
|
- //所以这里就简单粗爆的直接remove好了
|
|
|
- _ = watcher.Remove(event.Name)
|
|
|
- //fmt.Println("更改文件夹名称,删除监控 : ", event.Name)
|
|
|
- }
|
|
|
- if ok && event.Op&fsnotify.Chmod == fsnotify.Chmod {
|
|
|
- fmt.Println("修改权限 : ", event.Name)
|
|
|
- }
|
|
|
- case err := <-watcher.Errors:
|
|
|
- if err != nil {
|
|
|
- fmt.Println("watcher.Errors:", err)
|
|
|
- //log.Println("error:", err)
|
|
|
- }
|
|
|
- case <-time.After(60 * time.Second):
|
|
|
- continue
|
|
|
- }
|
|
|
+ err = filepath.Walk(global.CONFIG.Serve.ListenExcelPath, func(path string, info fs.FileInfo, err error) error {
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
- }()
|
|
|
-
|
|
|
- // 开始监听
|
|
|
- watcherDir(watcher, global.CONFIG.Serve.ListenExcelPath)
|
|
|
-
|
|
|
- <-done2
|
|
|
-}
|
|
|
-
|
|
|
-var watcherDirMap map[string]string
|
|
|
-
|
|
|
-// 添加监听目录
|
|
|
-func watcherDir(watcher *fsnotify.Watcher, dirPath string) {
|
|
|
- err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
|
|
|
- //这里判断是否为目录,只需监控目录即可
|
|
|
- //目录下的文件也在监控范围内,不需要我们一个一个加
|
|
|
- if info.IsDir() {
|
|
|
- subpath, e := filepath.Abs(path)
|
|
|
- if e != nil {
|
|
|
- return e
|
|
|
- }
|
|
|
- err = watcher.Add(subpath)
|
|
|
+ if !info.IsDir() {
|
|
|
+ fileInfo, err := os.Stat(path)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ fmt.Println("os.Stat:", err.Error())
|
|
|
}
|
|
|
- // 入库
|
|
|
- if watcherDirMap == nil {
|
|
|
- watcherDirMap = make(map[string]string)
|
|
|
+ winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
|
|
|
+ modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
|
|
|
+
|
|
|
+ existModifyTime, ok := global.CacheClient.Get(path)
|
|
|
+ if ok {
|
|
|
+ existModifyTimeStr := existModifyTime.(string)
|
|
|
+ if existModifyTimeStr != modifyTimeStr {
|
|
|
+ localCache.AddIndexHandleExcel(path)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ localCache.AddIndexHandleExcel(path)
|
|
|
}
|
|
|
- watcherDirMap[subpath] = subpath
|
|
|
- fmt.Println("监控 : ", subpath)
|
|
|
+ global.CacheClient.Delete(path)
|
|
|
+ global.CacheClient.Set(path, modifyTimeStr, 24*time.Hour)
|
|
|
}
|
|
|
return nil
|
|
|
})
|
|
|
- if err != nil {
|
|
|
- fmt.Println("watcher.Add:" + err.Error())
|
|
|
- //log.Fatal(err)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// HandleFileUpdate 处理文件变化
|
|
|
-func HandleFileUpdate(updateFilePath string) {
|
|
|
- fileExt := filepath.Ext(updateFilePath)
|
|
|
-
|
|
|
- if fileExt != ".xlsx" && fileExt != ".xls" {
|
|
|
- fmt.Println("不是excel文件")
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- // 读取excel内容并操作入库
|
|
|
- services.ReadExcel(updateFilePath)
|
|
|
}
|