preferences.nim 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # SPDX-License-Identifier: AGPL-3.0-only
  2. import strutils, uri, os, re, algorithm
  3. import jester
  4. import router_utils
  5. import ../types
  6. import ../views/[general, preferences]
  7. export preferences
  8. let titleizeRegex = re"(?<![A-z])[a-z]"
  9. proc titleize(str: string): string =
  10. result = str
  11. var idx = 0
  12. while idx != -1:
  13. idx = str.find(titleizeRegex, start = idx)
  14. if idx != -1:
  15. result[idx] = str[idx].toUpperAscii
  16. inc idx
  17. proc findThemes*(dir: string): seq[string] =
  18. for kind, path in walkDir(dir / "css" / "themes"):
  19. let theme = path.splitFile.name
  20. result.add theme.replace("_", " ").titleize
  21. sort(result)
  22. proc createPrefRouter*(cfg: Config) =
  23. router preferences:
  24. get "/settings":
  25. let
  26. prefs = cookiePrefs()
  27. html = renderPreferences(prefs, refPath(), findThemes(cfg.staticDir))
  28. resp renderMain(html, request, cfg, prefs, "Preferences")
  29. get "/settings/@i?":
  30. redirect("/settings")
  31. post "/saveprefs":
  32. genUpdatePrefs()
  33. redirect(refPath())
  34. post "/resetprefs":
  35. genResetPrefs()
  36. redirect("/settings?referer=" & encodeUrl(refPath()))
  37. post "/enablehls":
  38. savePref("hlsPlayback", "on", request)
  39. redirect(refPath())