115 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			115 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"io"
 | 
						|
	"log"
 | 
						|
	"os"
 | 
						|
	"path/filepath"
 | 
						|
	"strings"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"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) {
 | 
						|
	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))
 | 
						|
		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(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
 | 
						|
}
 |