package scanner import ( "io/fs" "math/rand" "path/filepath" "strconv" "strings" "time" "git.velvettear.de/velvettear/loggo" "git.velvettear.de/velvettear/slideshow/internal/config" ) // slice of images var images []string // temporary slice of images var tmpImages []string // time of next scan var nextScan time.Time // duration of the last scan var lastScanDuration time.Duration // scan the specified directories for images func Initialize() { directory := config.Directory scan(directory) } // get a random image func GetRandomImage() string { return images[rand.Intn(len(images))] } // check if images has been found func HasFoundImages() bool { return len(images) > 0 } // get the time of the next scan func GetNextScan() time.Time { return nextScan } // get the duration of the last scan func GetLastScanDuration() time.Duration { return lastScanDuration } // scan the specified directory func scan(directory string) { timestamp := time.Now() loggo.Info("scanning directory for images and subdirectories...", "interval: "+strconv.FormatFloat(config.ScanInterval.Seconds(), 'f', 0, 64)+" seconds", "directory: "+directory) filepath.WalkDir(directory, checkDirectory) images = tmpImages tmpImages = nil loggo.InfoTimed("found "+strconv.Itoa(len(images))+" image(s)", timestamp.UnixMilli()) lastScanDuration = time.Since(timestamp) nextScan = time.Now().Add(config.ScanInterval) go scheduleRescan(directory, nextScan) } // sleep the specified interval and then trigger a rescan of the specified directory func scheduleRescan(directory string, timestamp time.Time) { sleepTime := time.Until(timestamp) loggo.Debug("sleeping for " + strconv.FormatInt(sleepTime.Milliseconds(), 10) + "ms before next scan...") time.Sleep(sleepTime) scan(directory) } // add image files to the slice of images and subdirectoies to the watcher func checkDirectory(path string, dir fs.DirEntry, err error) error { if err != nil { return err } if dir.IsDir() || !isImage(path) { return nil } tmpImages = append(tmpImages, path) loggo.Debug("added image to temporary slice of images", path) return nil } // check if a file is an image func isImage(file string) bool { return strings.HasSuffix(file, ".jpeg") || strings.HasSuffix(file, ".jpg") || strings.HasSuffix(file, ".png") }