Skip to content

Commit

Permalink
[preprocessor/folder] change fsnotify to rjeczalik/notify
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed May 31, 2024
1 parent c2cdaa7 commit 38bdf7d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 47 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ require (
)

require (
github.com/fsnotify/fsnotify v1.7.0
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/rjeczalik/notify v0.9.3
github.com/x448/float16 v0.8.4 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ github.com/quic-go/webtransport-go v0.8.0 h1:HxSrwun11U+LlmwpgM1kEqIqH90IT4N8auv
github.com/quic-go/webtransport-go v0.8.0/go.mod h1:N99tjprW432Ut5ONql/aUhSLT0YVSlwHohQsuac9WaM=
github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtBsk=
github.com/raulk/go-watchdog v1.3.0/go.mod h1:fIvOnLbF0b0ZwkB9YU4mOW9Did//4vPZtDqv66NfsMU=
github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY=
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
Expand Down Expand Up @@ -943,6 +945,7 @@ golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181029174526-d69651ed3497/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
73 changes: 27 additions & 46 deletions preprocessor/folder/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path/filepath"

"github.com/fsnotify/fsnotify"
"github.com/rjeczalik/notify"
"github.com/starlinglab/integrity-v2/config"
)

Expand Down Expand Up @@ -47,57 +47,38 @@ func Run(args []string) error {
}

// Init directory watcher
watcher, err := fsnotify.NewWatcher()
c := make(chan notify.EventInfo, 1)
scanRoot := config.GetConfig().FolderPreprocessor.SyncFolderRoot
err = notify.Watch(scanRoot+"/...", c, notify.Create, notify.Rename)
if err != nil {
return err
}
defer watcher.Close()
defer notify.Stop(c)

go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) {
filePath := event.Name
file, err := os.Open(filePath)
if err != nil {
// File may be moved away for fsnotify.Rename
continue
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
log.Println("error getting file info:", err)
continue
}
if shouldIncludeFile(fileInfo.Name()) {
cid, err := handleNewFile(filePath)
if err != nil {
log.Println(err)
} else {
log.Printf("File %s uploaded to webhook with CID %s\n", filePath, cid)
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
for {
ei := <-c
event := ei.Event()
if event == notify.Rename || event == notify.Create {
filePath := ei.Path()
file, err := os.Open(filePath)
if err != nil {
// File may be moved away for notify.Rename
continue
}
fileInfo, err := file.Stat()
if err != nil {
log.Println("error getting file info:", err)
continue
}
if shouldIncludeFile(fileInfo.Name()) {
cid, err := handleNewFile(filePath)
if err != nil {
log.Println(err)
} else {
log.Printf("File %s uploaded to webhook with CID %s\n", filePath, cid)
}
log.Println("error:", err)
}
file.Close()
}
}()

scanRoot := config.GetConfig().FolderPreprocessor.SyncFolderRoot
err = watcher.Add(scanRoot)
if err != nil {
return err
}

// Block main goroutine forever.
<-make(chan struct{})
return nil
}

0 comments on commit 38bdf7d

Please sign in to comment.