67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package watcher
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"videnc-vibe/pkg/types"
|
|
)
|
|
|
|
type Watcher struct {
|
|
inputDir string
|
|
interval time.Duration
|
|
}
|
|
|
|
func New(inputDir string, intervalSeconds int) *Watcher {
|
|
interval := time.Duration(intervalSeconds) * time.Second
|
|
if interval < 10*time.Second {
|
|
interval = 10 * time.Second
|
|
}
|
|
if interval > 30*time.Second {
|
|
interval = 30 * time.Second
|
|
}
|
|
return &Watcher{
|
|
inputDir: inputDir,
|
|
interval: interval,
|
|
}
|
|
}
|
|
|
|
func (w *Watcher) Start(processFn func(string) error, done chan struct{}) {
|
|
ticker := time.NewTicker(w.interval)
|
|
defer ticker.Stop()
|
|
|
|
w.scanAndProcess(processFn)
|
|
|
|
for {
|
|
select {
|
|
case <-done:
|
|
return
|
|
case <-ticker.C:
|
|
w.scanAndProcess(processFn)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *Watcher) scanAndProcess(processFn func(string) error) {
|
|
files, err := filepath.Glob(filepath.Join(w.inputDir, "*.mkv"))
|
|
if err != nil {
|
|
fmt.Printf("Error scanning input directory: %v\n", err)
|
|
return
|
|
}
|
|
|
|
for _, file := range files {
|
|
if err := processFn(file); err != nil {
|
|
fmt.Printf("Error processing %s: %v\n", file, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func DetectMediaType(width, height int) types.MediaType {
|
|
pixels := width * height
|
|
if pixels < 600000 {
|
|
return types.MediaTypeDVD
|
|
}
|
|
return types.MediaTypeBluRay
|
|
}
|