101 lines
2.5 KiB
Go
101 lines
2.5 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 Resolution string
|
|
var PaletteAlgorithm int
|
|
var PaletteColors int
|
|
var BufferSize int
|
|
|
|
// 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
|
|
Resolution = os.Getenv("SLIDESHOW_RESOLUTION")
|
|
if len(Resolution) > 0 {
|
|
width, height, found := strings.Cut(Resolution, "x")
|
|
if !found {
|
|
loggo.Fatal("encountered an error parsing the configured resolution, make sure to specify the format like '1920x1080'")
|
|
}
|
|
_, error = strconv.Atoi(width)
|
|
if error != nil {
|
|
loggo.Fatal("encountered an error parsing the configured width '" + width + "'")
|
|
}
|
|
_, error = strconv.Atoi(height)
|
|
if error != nil {
|
|
loggo.Fatal("encountered an error parsing the configured height '" + height + "'")
|
|
}
|
|
}
|
|
tmpString := os.Getenv("SLIDESHOW_PALETTE_ALGORITHM")
|
|
if strings.ToLower(tmpString) == "wu" {
|
|
PaletteAlgorithm = 0
|
|
} else {
|
|
PaletteAlgorithm = 1
|
|
}
|
|
tmpInt, _ = strconv.Atoi(os.Getenv("SLIDESHOW_PALETTE_COLORS"))
|
|
if tmpInt <= 0 {
|
|
tmpInt = 16
|
|
}
|
|
PaletteColors = tmpInt
|
|
tmpInt, _ = strconv.Atoi(os.Getenv("SLIDESHOW_BUFFERSIZE"))
|
|
if tmpInt <= 0 {
|
|
tmpInt = 4096
|
|
}
|
|
BufferSize = tmpInt
|
|
if IsResolutionSet() {
|
|
checkForConvert()
|
|
}
|
|
}
|
|
|
|
// check if 'convert' is available
|
|
func checkForConvert() {
|
|
cmd := exec.Command("convert")
|
|
error := cmd.Run()
|
|
if error == nil {
|
|
return
|
|
}
|
|
Resolution = ""
|
|
loggo.Warning("could not find imagemagick's 'convert' command, no scaling will be done", error.Error())
|
|
}
|
|
|
|
// check if a resolution has been specified
|
|
func IsResolutionSet() bool {
|
|
return len(Resolution) > 0
|
|
}
|