From e03226a7ee9c7086cebed179de89c643be6f7d9b Mon Sep 17 00:00:00 2001 From: Joren Date: Sun, 6 Oct 2024 00:37:47 +0200 Subject: [PATCH] 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 +}