mirror-yaf/config.go

175 lines
3.3 KiB
Go
Raw Permalink Normal View History

2020-10-27 17:17:41 +01:00
package main
import (
"bufio"
"log"
"os"
"strconv"
"strings"
2022-08-23 12:04:56 +02:00
"github.com/go-errors/errors"
2020-10-27 17:17:41 +01:00
)
const (
commentPrefix = "#"
)
type Config struct {
Port int
LinkPrefix string
FileDir string
LinkLength int
ScrubExif bool
ExifAllowedIds []uint16
ExifAllowedPaths []string
ExifAbortOnError bool
FileExpiration bool
MaxFileSizeMB int
2020-10-27 17:17:41 +01:00
}
func ConfigFromFile(filePath string) (*Config, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
oldPrefix := log.Prefix()
defer log.SetPrefix(oldPrefix)
log.SetPrefix("config.FromFile > ")
retval := &Config{
Port: 4711,
2023-09-04 18:16:09 +02:00
LinkPrefix: "https://yaf.example.com/",
2023-09-04 20:49:33 +02:00
FileDir: "/var/www/yaf/",
LinkLength: 5,
ScrubExif: true,
ExifAllowedIds: []uint16{},
ExifAllowedPaths: []string{},
ExifAbortOnError: true,
2023-09-04 18:16:09 +02:00
FileExpiration: false,
MaxFileSizeMB: 50,
}
2020-10-27 17:17:41 +01:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, commentPrefix) {
// Skip comments
continue
}
key, val, found := strings.Cut(line, ":")
if !found {
2020-10-27 17:17:41 +01:00
log.Printf("unexpected line: \"%s\", ignoring\n", line)
continue
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
2020-10-27 17:17:41 +01:00
switch key {
case "Port":
parsed, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
retval.Port = parsed
case "LinkPrefix":
retval.LinkPrefix = val
case "FileDir":
retval.FileDir = val
case "LinkLength":
parsed, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
retval.LinkLength = parsed
case "ScrubExif":
parsed, err := strconv.ParseBool(val)
if err != nil {
return nil, err
}
retval.ScrubExif = parsed
case "ExifAllowedIds":
if val == "" {
// No IDs specified at all
break
}
stringIds := strings.Split(val, " ")
parsedIds := make([]uint16, 0, len(stringIds))
for _, stringId := range stringIds {
var parsed uint64
var err error
if strings.HasPrefix(stringId, "0x") {
// Parse as a hexadecimal number
hexStringId := strings.Replace(stringId, "0x", "", 1)
parsed, err = strconv.ParseUint(hexStringId, 16, 16)
} else {
// Parse as a decimal number
parsed, err = strconv.ParseUint(stringId, 10, 16)
}
if err != nil {
log.Printf(
"Could not parse ID from: \"%s\", ignoring. Error: %s\n",
stringId,
err,
)
continue
}
parsedIds = append(parsedIds, uint16(parsed))
}
retval.ExifAllowedIds = parsedIds
case "ExifAllowedPaths":
if val == "" {
// No paths specified at all
break
}
paths := strings.Split(val, " ")
retval.ExifAllowedPaths = paths
case "ExifAbortOnError":
parsed, err := strconv.ParseBool(val)
if err != nil {
return nil, err
}
retval.ExifAbortOnError = parsed
case "FileExpiration":
parsed, err := strconv.ParseBool(val)
if err != nil {
return nil, err
}
retval.FileExpiration = parsed
case "MaxFileSizeMB":
parsed, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
retval.MaxFileSizeMB = parsed
2020-10-27 17:17:41 +01:00
default:
2022-08-23 12:04:56 +02:00
return nil, errors.Errorf("unexpected config key: \"%s\"", key)
2020-10-27 17:17:41 +01:00
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return retval, nil
}