123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package watch
- import (
- "fmt"
- localCache "hongze/hongtao3_watch/cache"
- "hongze/hongtao3_watch/global"
- "hongze/hongtao3_watch/utils"
- "io/fs"
- "os"
- "path/filepath"
- "syscall"
- "time"
- )
- /*
- CREATE动作即临时文件的创建
- WRITE写文件动作
- CHMOD修改文件属性
- REMOVE删除临时文件。
- */
- //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
- }
- err = filepath.Walk(global.CONFIG.Serve.ListenExcelPath, func(path string, info fs.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !info.IsDir() {
- fileInfo, err := os.Stat(path)
- if err != nil {
- fmt.Println("os.Stat:", err.Error())
- }
- 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)
- }
- global.CacheClient.Delete(path)
- global.CacheClient.Set(path, modifyTimeStr, 24*time.Hour)
- }
- return nil
- })
- }
|