2023-11-28 15:52:53 +01:00
package internal
import (
"errors"
"io/fs"
"math/rand"
"path/filepath"
"strconv"
"strings"
"time"
"git.velvettear.de/velvettear/loggo"
2023-11-28 16:34:10 +01:00
"git.velvettear.de/velvettear/slideshow-api/internal/config"
2023-11-28 15:52:53 +01:00
)
2023-11-29 16:00:35 +01:00
// map of images
var images map [ string ] string
2023-11-28 15:52:53 +01:00
2023-11-29 16:00:35 +01:00
// temporary map of images
var tmpImages map [ string ] string
2023-11-28 15:52:53 +01:00
// Scan the specified directory
func Scan ( ) {
timestamp := time . Now ( )
directory := config . Directory
loggo . Info ( "scanning directory for images and subdirectories..." , "interval: " + strconv . FormatFloat ( config . ScanInterval . Seconds ( ) , 'f' , 0 , 64 ) + " seconds" , "directory: " + directory )
2023-11-29 16:00:35 +01:00
tmpImages = make ( map [ string ] string )
2023-11-28 15:52:53 +01:00
filepath . WalkDir ( directory , checkDirectory )
images = tmpImages
tmpImages = nil
loggo . InfoTimed ( "found " + strconv . Itoa ( len ( images ) ) + " image(s)" , timestamp . UnixMilli ( ) )
go scheduleRescan ( )
}
// get a random image
2023-11-29 16:00:35 +01:00
func GetRandomImage ( ) ( string , error ) {
2023-11-28 15:52:53 +01:00
if len ( images ) == 0 {
2023-11-29 16:00:35 +01:00
return "" , errors . New ( "no images have been found in directory '" + config . Directory + "'" )
}
random := rand . Intn ( len ( images ) )
index := 0
for key := range images {
if index == random {
return key , nil
}
index ++
}
return "" , errors . New ( "could not find a random image with index '" + strconv . Itoa ( random ) + "'" )
}
// get the path of an image by name
func GetImagePath ( name string ) ( string , error ) {
var path string
if len ( name ) == 0 {
return path , errors . New ( "no image name provided" )
2023-11-28 15:52:53 +01:00
}
2023-11-29 16:00:35 +01:00
image := images [ name ]
if len ( image ) == 0 {
return path , errors . New ( "could not find image '" + name + "' in internal map" )
2023-11-28 15:52:53 +01:00
}
2023-11-29 16:00:35 +01:00
return image , nil
2023-11-28 15:52:53 +01:00
}
// sleep the specified interval and then trigger a rescan of the specified directory
func scheduleRescan ( ) {
loggo . Debug ( "sleeping for " + strconv . FormatInt ( config . ScanInterval . Milliseconds ( ) , 10 ) + "ms before next scan..." )
time . Sleep ( config . ScanInterval )
Scan ( )
}
2023-11-29 16:00:35 +01:00
// add image files to the map of images
2023-11-28 15:52:53 +01:00
func checkDirectory ( path string , dir fs . DirEntry , err error ) error {
if err != nil {
return err
}
if dir . IsDir ( ) || ! isImage ( path ) {
return nil
}
2023-11-29 16:00:35 +01:00
tmpImages [ filepath . Base ( path ) ] = path
loggo . Debug ( "added image to temporary map of images" , path )
2023-11-28 15:52:53 +01:00
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" )
}