diff --git a/main.go b/main.go index bd2fefb..271c12c 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ var ( // flags FlagSourceDir = envvars.String("SOURCE_PATH", "/video") FlagTargetDir = envvars.String("TARGET_PATH", "/audio") FlagCacheFile = envvars.String("CACHE_FILE", "/hashes.bin") + FlagExcludeFiles = envvars.StringSlice("EXCLUDE_FILES", ":", []string{".songs"}) FlagUpdateCacheFileInterval = envvars.Duration("UPDATE_CACHE_FILE_INTERVAL", 10*time.Second) ) @@ -33,6 +34,10 @@ func main() { FlagTargetDir = AbsPath(FlagTargetDir) FlagCacheFile = AbsPath(FlagCacheFile) + for i := 0; i < len(FlagExcludeFiles); i++ { + FlagExcludeFiles[i] = filepath.Join(FlagSourceDir, FlagExcludeFiles[i]) + } + InitCache() ops := []fsnotify.Op{ diff --git a/utils.go b/utils.go index 9507949..500a8a6 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,7 @@ import ( "crypto/sha512" "io" "os" + "strings" ) func HashFile(path string) ([sha512.Size]byte, error) { @@ -23,3 +24,15 @@ func HashFile(path string) ([sha512.Size]byte, error) { copy(res[:], hash) return res, nil } + +func PathIsExcluded(path string) bool { + path = AbsPath(path) + + for _, excludedPath := range FlagExcludeFiles { + if strings.HasPrefix(path, AbsPath(excludedPath)) { + return true + } + } + + return false +} diff --git a/watch_dir.go b/watch_dir.go index 833a8c2..5b57e49 100644 --- a/watch_dir.go +++ b/watch_dir.go @@ -44,6 +44,11 @@ func WatchDirectory(ctx context.Context, path string, op ...fsnotify.Op) (<-chan for _, file := range files { path := filepath.Join(path, file.Name()) + + if PathIsExcluded(path) { + continue + } + if file.IsDir() { //fmt.Println("watching directory", path) watcher.Add(path) @@ -66,6 +71,10 @@ func WatchDirectory(ctx context.Context, path string, op ...fsnotify.Op) (<-chan return } + if PathIsExcluded(event.Name) { + continue + } + if fi, err := os.Stat(event.Name); err == nil && fi.IsDir() { if event.Op&fsnotify.Create == fsnotify.Create { //fmt.Println("watching directory", event.Name)