slideshow-api/internal/config/config.go

78 lines
1.8 KiB
Go

package config
import (
"os"
"os/exec"
"strconv"
"strings"
"time"
"git.velvettear.de/velvettear/loggo"
)
var ServerAddress string
var ServerPort int
var Directory string
var ScanInterval time.Duration
var ConvertAvailable bool
// initialize the config
func Initialize() {
loggo.SetLogLevelByName(os.Getenv("SLIDESHOW_LOGLEVEL"))
ServerAddress = os.Getenv("SLIDESHOW_ADDRESS")
tmpInt, _ := strconv.Atoi(os.Getenv("SLIDESHOW_PORT"))
if tmpInt <= 0 {
tmpInt = 3000
}
ServerPort = tmpInt
Directory = os.Getenv("SLIDESHOW_DIRECTORY")
if len(Directory) == 0 {
tmp, error := os.UserHomeDir()
if error != nil {
loggo.Fatal("encountered an error getting the current user's home directory", error.Error())
}
Directory = tmp
}
stats, error := os.Stat(Directory)
if error != nil {
loggo.Fatal("encountered an error checking the directory '"+Directory+"'", error.Error())
}
if !stats.IsDir() {
loggo.Fatal("configured directory '" + Directory + "' is not a valid directory")
}
tmpInt, _ = strconv.Atoi(os.Getenv("SLIDESHOW_SCANINTERVAL"))
if tmpInt <= 0 {
tmpInt = 60
}
ScanInterval = time.Duration(tmpInt) * time.Second
checkConvertCommand()
}
// check if 'convert' is available
func checkConvertCommand() {
cmd := exec.Command("which", "convert")
error := cmd.Run()
ConvertAvailable = error == nil
if error == nil {
return
}
loggo.Warning("could not find imagemagick's 'convert' command, scaling is unavailable", error.Error())
}
// check if the given string is a valid resolution
func IsValidResolution(resolution string) bool {
if len(resolution) <= 0 {
return false
}
width, height, found := strings.Cut(resolution, "x")
if !found {
return false
}
_, error := strconv.Atoi(width)
if error != nil {
return false
}
_, error = strconv.Atoi(height)
return error == nil
}