feat: add locally syncing and watchdog

This commit is contained in:
DaanSelen
2026-04-22 15:26:59 +02:00
parent 0c287cc917
commit ec3a996d6a
12 changed files with 283 additions and 179 deletions
+30 -4
View File
@@ -1,8 +1,10 @@
package utility
import (
"log/slog"
"os"
"path/filepath"
"slices"
"strconv"
)
@@ -13,14 +15,24 @@ type Environment struct {
ContentDirectory string
Hostname string
Port int
WatchInterval int
WatchdogInterval int
WatchdogSyncMode string
}
var (
validSyncModes = []string{
"sync",
"strict",
"dry",
}
)
// part of environment checking
func safeStringGrab(key, fallback string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
slog.Debug("using fallback", "key", key, "fallback", fallback)
return fallback
}
@@ -30,6 +42,17 @@ func safeIntGrab(key string, fallback int) int {
return i
}
}
slog.Debug("using fallback", "key", key, "fallback", fallback)
return fallback
}
func safeSyncModeGrab(key, fallback string) string {
if v, ok := os.LookupEnv(key); ok {
if slices.Contains(validSyncModes, v) {
return v
}
}
slog.Debug("using fallback", "key", key, "fallback", fallback)
return fallback
}
@@ -48,8 +71,11 @@ func GrabEnvironment() Environment {
DataDirectory: safeStringGrab("DATA_DIR", fbBase),
ContentDirectory: safeStringGrab("CONTENT_DIR", fbContent),
Hostname: safeStringGrab("HOSTNAME", "0.0.0.0"),
Port: safeIntGrab("PORT", 8080),
WatchInterval: safeIntGrab("WATCHDOG_INTERVAL", 60),
Port: safeIntGrab("PORT", 8080),
WatchdogInterval: safeIntGrab("WATCHDOG_INTERVAL", 60),
// sync: sync local files to the database, for example when you want to allow local inserting files which then get added to the database
// strict: make the database leading, this is when you only allow API uploads to be registered, and remove orphaned filesystem files
// dry: do nothing
WatchdogSyncMode: safeStringGrab("WATCHDOG_SYNC_MODE", "strict"),
}
}
+41
View File
@@ -0,0 +1,41 @@
package utility
import (
"crypto/sha512"
"encoding/hex"
"io"
"mime/multipart"
"os"
)
func HashReader(r io.Reader) (string, error) {
h := sha512.New()
if _, err := io.Copy(h, r); err != nil {
return "", err
}
// return the sha checksum in hex
return hex.EncodeToString(h.Sum(nil)), nil
// alternatively return in base64
//return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}
func HashUpload(fileHeader *multipart.FileHeader) (string, error) {
stream, err := fileHeader.Open()
if err != nil {
return "", err
}
defer stream.Close()
return HashReader(stream)
}
func HashFile(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return HashReader(file)
}