introduced excluded directories

This commit is contained in:
Tordarus 2025-01-15 21:06:45 +01:00
parent 0681c6891b
commit 85436a47e7
3 changed files with 27 additions and 0 deletions

View File

@ -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{

View File

@ -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
}

View File

@ -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)