package transfer import ( "context" "fmt" "log" "m3u8-downloader/pkg/health" "m3u8-downloader/pkg/logging" "math/rand" "os" "path/filepath" "strings" "sync" "time" "github.com/fsnotify/fsnotify" ) type FileWatcher struct { outputDir string // eventName namespaces the NAS destination. It is passed in rather than // derived from outputDir so that an unnamed event resolves to the NAS root // — matching where the downloader put the files — instead of picking up // the local output directory's own name. eventName string queue *TransferQueue watcher *fsnotify.Watcher settingDelay time.Duration pendingFiles map[string]*time.Timer mu sync.Mutex } func NewFileWatcher(outputDir, eventName string, queue *TransferQueue, settlingDelay time.Duration) (*FileWatcher, error) { watcher, err := fsnotify.NewWatcher() if err != nil { return nil, err } return &FileWatcher{ outputDir: outputDir, eventName: eventName, queue: queue, watcher: watcher, settingDelay: settlingDelay, pendingFiles: make(map[string]*time.Timer), }, nil } func (fw *FileWatcher) Start(ctx context.Context) error { defer fw.watcher.Close() if err := fw.addWatchRecursive(fw.outputDir); err != nil { return fmt.Errorf("Failed to add watch paths: %w", err) } log.Printf("Starting file watcher on %s", fw.outputDir) logging.MustGetFactory().Transfer.Info("file watcher started", "dir", fw.outputDir) for { select { case <-ctx.Done(): log.Println("File watcher shutting down...") logging.MustGetFactory().Transfer.Info("file watcher shutting down") return ctx.Err() case event, ok := <-fw.watcher.Events: if !ok { return fmt.Errorf("Watcher events channel closed") } fw.handleFileEvent(event) case err, ok := <-fw.watcher.Errors: if !ok { return fmt.Errorf("Watcher errors channel closed") } log.Printf("Watcher error: %v", err) logging.MustGetFactory().Transfer.Error("watcher error", logging.KeyError, err.Error()) } } } func (fw *FileWatcher) addWatchRecursive(root string) error { return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { log.Printf("Error walking path %s: %v", path, err) return nil } if info.IsDir() { if err := fw.watcher.Add(path); err != nil { log.Printf("Failed to watch directory %s: %v", path, err) } else { log.Printf("Watching directory %s", path) } } return nil }) } func (fw *FileWatcher) handleFileEvent(event fsnotify.Event) { // Directories must be handled before the .ts filter below. fsnotify is not // recursive, so a resolution directory created after the watcher starts — // which is the normal case, since the transfer service starts before the // downloader creates 1080p/ and friends — has to be picked up here or // nothing inside it is ever seen. if event.Op&fsnotify.Create == fsnotify.Create { if info, err := os.Stat(event.Name); err == nil && info.IsDir() { fw.watchNewDirectory(event.Name) return } } if !strings.HasSuffix(strings.ToLower(event.Name), ".ts") { return } switch { case event.Op&fsnotify.Create == fsnotify.Create: fw.scheduleTransfer(event.Name) case event.Op&fsnotify.Write == fsnotify.Write: fw.scheduleTransfer(event.Name) case event.Op&fsnotify.Remove == fsnotify.Remove: fw.cancelPendingTransfer(event.Name) } } // watchNewDirectory starts watching a directory that appeared after startup, // and sweeps whatever is already inside it. // // The sweep is not redundant: the downloader creates a resolution directory and // writes segments into it immediately, so files can land between the mkdir and // the watch being registered. Those writes generate no event we would see, and // without the sweep the first segments of every recording would be lost. func (fw *FileWatcher) watchNewDirectory(dir string) { if err := fw.addWatchRecursive(dir); err != nil { log.Printf("Failed to watch new directory %s: %v", dir, err) logging.MustGetFactory().Transfer.Warn("failed to watch new directory", "dir", dir, logging.KeyError, err.Error()) return } err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return nil // keep sweeping the rest } if !info.IsDir() && strings.HasSuffix(strings.ToLower(info.Name()), ".ts") { fw.scheduleTransfer(path) } return nil }) if err != nil { log.Printf("Failed to sweep new directory %s: %v", dir, err) } } func (fw *FileWatcher) scheduleTransfer(filePath string) { fw.mu.Lock() defer fw.mu.Unlock() if timer, exists := fw.pendingFiles[filePath]; exists { timer.Stop() } fw.pendingFiles[filePath] = time.AfterFunc(fw.settingDelay, func() { fw.processFile(filePath) fw.mu.Lock() delete(fw.pendingFiles, filePath) fw.mu.Unlock() }) log.Printf("Scheduled file for transfer: %s", filePath) } func (fw *FileWatcher) cancelPendingTransfer(filePath string) { fw.mu.Lock() defer fw.mu.Unlock() if timer, exists := fw.pendingFiles[filePath]; exists { timer.Stop() delete(fw.pendingFiles, filePath) log.Printf("Canceled pending transfer for file: %s", filePath) } } func (fw *FileWatcher) processFile(filePath string) { info, err := os.Stat(filePath) if err != nil { log.Printf("Failed to stat file %s: %v", filePath, err) logging.MustGetFactory().Transfer.Warn("failed to stat file", logging.KeyFile, filePath, logging.KeyError, err.Error()) health.MustGetRegistry().RecordTransferError(filePath, err) return } resolution := fw.extractResolution(filePath) relPath, err := filepath.Rel(fw.outputDir, filePath) if err != nil { log.Printf("Failed to get relative path for file %s: %v", filePath, err) logging.MustGetFactory().Transfer.Warn("failed to get relative path", logging.KeyFile, filePath, logging.KeyError, err.Error()) health.MustGetRegistry().RecordTransferError(filePath, err) return } // The destination is namespaced by event, mirroring the local layout and // matching what QueueExistingFiles produces for the same file — otherwise // live transfers and catch-up transfers would write to different places. item := TransferItem{ ID: generateID(), SourcePath: filePath, DestinationPath: filepath.Join(fw.eventName, relPath), Resolution: resolution, Timestamp: time.Now(), Status: StatusPending, FileSize: info.Size(), } if err := fw.queue.Add(item); err != nil { log.Printf("Failed to add file to queue: %v", err) logging.MustGetFactory().Transfer.Error("failed to add file to queue", logging.KeyFile, filePath, logging.KeyError, err.Error()) health.MustGetRegistry().RecordTransferError(filePath, err) } else { log.Printf("Added file to queue: %s", filePath) logging.MustGetFactory().Transfer.Debug("file queued", logging.KeyFile, filePath) } } func (fw *FileWatcher) extractResolution(filePath string) string { dir := filepath.Dir(filePath) parts := strings.Split(dir, string(filepath.Separator)) for _, part := range parts { if strings.HasSuffix(part, "p") { return part } } return "" } func generateID() string { return fmt.Sprintf("transfer_%d_%d", time.Now().UnixNano(), rand.Intn(1000)) }