From acf172933d48496965cda6c6ac1822ffe5767abc Mon Sep 17 00:00:00 2001 From: Joren Date: Sun, 6 Oct 2024 00:09:43 +0200 Subject: [PATCH 1/4] Watcher --- config.toml | 1 + src/config.go | 5 +++ src/go.mod | 3 ++ src/go.sum | 4 +++ src/main.go | 1 + src/watcher.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+) create mode 100644 src/watcher.go diff --git a/config.toml b/config.toml index 58e198c..0effe73 100644 --- a/config.toml +++ b/config.toml @@ -2,6 +2,7 @@ BaseDir = "/mnt/media" Format = "mkv" TempBaseDir = "/tmp/nre" EnableConsole = true +WatchedFolder = "/mnt/watched" [N_m3u8DLRE] Path = "nre" \ No newline at end of file diff --git a/src/config.go b/src/config.go index fc36b81..3af4b15 100644 --- a/src/config.go +++ b/src/config.go @@ -16,6 +16,7 @@ type Config struct { Path string } EnableConsole bool + WatchedFolder string } var config Config @@ -40,4 +41,8 @@ func loadConfig() { return } + if config.WatchedFolder == "" { + fmt.Println("Error: Watched folder is not specified in the config file") + return + } } diff --git a/src/go.mod b/src/go.mod index 38e8b9d..f8ce615 100644 --- a/src/go.mod +++ b/src/go.mod @@ -8,9 +8,12 @@ require ( github.com/beevik/etree v1.4.1 ) +require golang.org/x/sys v0.4.0 // indirect + require ( github.com/asticode/go-astikit v0.20.0 // indirect github.com/asticode/go-astits v1.8.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 github.com/gorilla/websocket v1.5.3 golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect golang.org/x/text v0.3.2 // indirect diff --git a/src/go.sum b/src/go.sum index 68a64b8..cb34f19 100644 --- a/src/go.sum +++ b/src/go.sum @@ -10,6 +10,8 @@ github.com/beevik/etree v1.4.1 h1:PmQJDDYahBGNKDcpdX8uPy1xRCwoCGVUiW669MEirVI= github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/pkg/exec v0.0.0-20150614095509-0bd164ad2a5a h1:EN123kAtAAE2pg/+TvBsUBZfHCWNNFyL2ZBPPfNWAc0= @@ -28,6 +30,8 @@ golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/src/main.go b/src/main.go index d9434f4..6089f4d 100644 --- a/src/main.go +++ b/src/main.go @@ -60,6 +60,7 @@ func main() { flag.Parse() if *inputFile == "" { + go watchFolder() startWebServer() } else { items, err := parseInputFile(*inputFile) diff --git a/src/watcher.go b/src/watcher.go new file mode 100644 index 0000000..ffd1da8 --- /dev/null +++ b/src/watcher.go @@ -0,0 +1,85 @@ +package main + +import ( + "fmt" + "io" + "log" + "os" + "path/filepath" + "strings" + + "github.com/fsnotify/fsnotify" +) + +func watchFolder() { + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + + done := make(chan bool) + + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + if event.Op&fsnotify.Create == fsnotify.Create { + if strings.HasSuffix(event.Name, ".drmd") { + fmt.Println("New .drmd detected:", event.Name) + processWatchedFile(event.Name) + } + } + case err, ok := <-watcher.Errors: + if !ok { + return + } + log.Println("Error:", err) + } + } + }() + + err = watcher.Add(config.WatchedFolder) + if err != nil { + log.Fatal(err) + } + <-done +} + +func processWatchedFile(filePath string) { + file, err := os.Open(filePath) + if err != nil { + logger.LogError("Watcher", fmt.Sprintf("Error opening file: %v", err)) + return + } + defer file.Close() + + originalFilename := filepath.Base(filePath) + tempFile, err := os.CreateTemp(uploadDir, originalFilename) + if err != nil { + logger.LogError("Watcher", fmt.Sprintf("Error creating temporary file: %v", err)) + return + } + defer tempFile.Close() + + _, err = io.Copy(tempFile, file) + if err != nil { + logger.LogError("Watcher", fmt.Sprintf("Error copying file: %v", err)) + return + } + + if err := os.Remove(filePath); err != nil { + logger.LogError("Watcher", fmt.Sprintf("Error deleting original file: %v", err)) + } + + items, err := parseInputFile(tempFile.Name()) + if err != nil { + logger.LogError("Watcher", fmt.Sprintf("Error parsing input file: %v", err)) + return + } + + go processItems(tempFile.Name(), items) +} From f1015ab62ef53b813bf6fd13329d168548eb57c3 Mon Sep 17 00:00:00 2001 From: Joren Date: Sun, 6 Oct 2024 00:14:46 +0200 Subject: [PATCH 2/4] Change watched file name --- src/watcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/watcher.go b/src/watcher.go index ffd1da8..af839f4 100644 --- a/src/watcher.go +++ b/src/watcher.go @@ -81,5 +81,5 @@ func processWatchedFile(filePath string) { return } - go processItems(tempFile.Name(), items) + go processItems(filepath.Base(tempFile.Name()), items) } From e03226a7ee9c7086cebed179de89c643be6f7d9b Mon Sep 17 00:00:00 2001 From: Joren Date: Sun, 6 Oct 2024 00:37:47 +0200 Subject: [PATCH 3/4] Make sure the file is fully written --- src/watcher.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/watcher.go b/src/watcher.go index af839f4..fd1d982 100644 --- a/src/watcher.go +++ b/src/watcher.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/fsnotify/fsnotify" ) @@ -50,6 +51,26 @@ func watchFolder() { } func processWatchedFile(filePath string) { + for { + initialSize, err := getFileSize(filePath) + if err != nil { + logger.LogError("Watcher", fmt.Sprintf("Error getting file size: %v", err)) + return + } + + time.Sleep(1 * time.Second) + + currentSize, err := getFileSize(filePath) + if err != nil { + logger.LogError("Watcher", fmt.Sprintf("Error getting file size: %v", err)) + return + } + + if initialSize == currentSize { + break + } + } + file, err := os.Open(filePath) if err != nil { logger.LogError("Watcher", fmt.Sprintf("Error opening file: %v", err)) @@ -83,3 +104,11 @@ func processWatchedFile(filePath string) { go processItems(filepath.Base(tempFile.Name()), items) } + +func getFileSize(filePath string) (int64, error) { + fileInfo, err := os.Stat(filePath) + if err != nil { + return 0, err + } + return fileInfo.Size(), nil +} From 1f42b2a877ba362a4a4e7c850a2df6b32fc35ba8 Mon Sep 17 00:00:00 2001 From: Joren Date: Sun, 6 Oct 2024 22:45:33 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1628f8c..a4a4808 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ BaseDir = "/path/to/save/downloads" Format = "mkv" TempBaseDir = "/tmp/nre" EnableConsole = true +WatchedFolder = "/path/to/watched/folder" [N_m3u8DL-RE] Path = "/path/to/N_m3u8DL-RE"