From 7476e2397dc36b8d64809ef0091d191df5e05815 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Sat, 25 Jun 2022 14:02:25 +0200 Subject: [PATCH] add a filewatcher to automatically reindex if new file is added --- apiGo/go.mod | 1 + apiGo/go.sum | 5 ++- apiGo/main.go | 1 + apiGo/videoparser/FileWatcher.go | 58 ++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 apiGo/videoparser/FileWatcher.go diff --git a/apiGo/go.mod b/apiGo/go.mod index 89ff76c..fe647bb 100644 --- a/apiGo/go.mod +++ b/apiGo/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/3d0c/gmf v0.0.0-20210925211039-e278e6e53b16 github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/fsnotify/fsnotify v1.5.4 github.com/go-sql-driver/mysql v1.6.0 github.com/pelletier/go-toml/v2 v2.0.0-beta.3 github.com/pressly/goose/v3 v3.1.0 diff --git a/apiGo/go.sum b/apiGo/go.sum index 3556c6c..5cea09c 100644 --- a/apiGo/go.sum +++ b/apiGo/go.sum @@ -9,6 +9,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= +github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= @@ -82,8 +84,9 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/apiGo/main.go b/apiGo/main.go index 58575b3..b4f358d 100644 --- a/apiGo/main.go +++ b/apiGo/main.go @@ -42,6 +42,7 @@ func main() { api.AddHandlers() videoparser.SetupSettingsWebsocket() + videoparser.InitFileWatcher() // add the static files static.ServeStaticFiles() diff --git a/apiGo/videoparser/FileWatcher.go b/apiGo/videoparser/FileWatcher.go new file mode 100644 index 0000000..9ef0173 --- /dev/null +++ b/apiGo/videoparser/FileWatcher.go @@ -0,0 +1,58 @@ +package videoparser + +import ( + "github.com/fsnotify/fsnotify" + "log" + "openmediacenter/apiGo/config" + "openmediacenter/apiGo/database" + "strings" +) + +func InitFileWatcher() { + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal("NewWatcher failed: ", err) + } + + mSettings, _, _ := database.GetSettings() + vidFolder := config.GetConfig().General.ReindexPrefix + mSettings.VideoPath + epsfolder := config.GetConfig().General.ReindexPrefix + mSettings.EpisodePath + + defer watcher.Close() + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + // start new reindex + // (may be optimized by checking here if added file is video + // and start reindex just for one file) + if strings.Contains(event.Name, vidFolder) { + StartReindex() + } else if strings.Contains(event.Name, epsfolder) { + StartTVShowReindex() + } else { + log.Printf("Event in wrong folder: %s %s\n", event.Name, event.Op) + } + case err, ok := <-watcher.Errors: + if !ok { + return + } + log.Println("error:", err) + } + } + + }() + + err = watcher.Add(vidFolder) + if err != nil { + log.Println("Adding of file watcher failed: ", err) + } + + err = watcher.Add(epsfolder) + if err != nil { + log.Println("Adding of file watcher failed: ", err) + } +}