Make sure the file is fully written

This commit is contained in:
Joren 2024-10-06 00:37:47 +02:00
parent f1015ab62e
commit e03226a7ee
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
) )
@ -50,6 +51,26 @@ func watchFolder() {
} }
func processWatchedFile(filePath string) { 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) file, err := os.Open(filePath)
if err != nil { if err != nil {
logger.LogError("Watcher", fmt.Sprintf("Error opening file: %v", err)) 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) 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
}