2020-07-29 21:11:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-02-01 17:39:19 +00:00
|
|
|
"io/fs"
|
2020-11-02 23:20:06 +00:00
|
|
|
"log"
|
2021-01-31 23:12:50 +00:00
|
|
|
"os"
|
2021-01-11 19:17:43 +00:00
|
|
|
"path/filepath"
|
2020-09-22 23:01:07 +00:00
|
|
|
"strconv"
|
2020-11-29 18:01:10 +00:00
|
|
|
"strings"
|
2020-07-29 21:11:28 +00:00
|
|
|
"time"
|
2021-02-19 00:47:01 +00:00
|
|
|
|
|
|
|
"github.com/hrfee/jfa-go/mediabrowser"
|
2020-07-29 21:11:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Storage struct {
|
2021-02-19 21:38:20 +00:00
|
|
|
timePattern string
|
|
|
|
invite_path, emails_path, policy_path, configuration_path, displayprefs_path, ombi_path, profiles_path, customEmails_path string
|
|
|
|
invites Invites
|
|
|
|
profiles map[string]Profile
|
|
|
|
defaultProfile string
|
|
|
|
emails, displayprefs, ombi_template map[string]interface{}
|
|
|
|
customEmails customEmails
|
|
|
|
policy mediabrowser.Policy
|
|
|
|
configuration mediabrowser.Configuration
|
|
|
|
lang Lang
|
|
|
|
}
|
|
|
|
|
|
|
|
type customEmails struct {
|
2021-02-20 00:22:40 +00:00
|
|
|
UserCreated customEmail `json:"userCreated"`
|
|
|
|
InviteExpiry customEmail `json:"inviteExpiry"`
|
|
|
|
PasswordReset customEmail `json:"passwordReset"`
|
|
|
|
UserDeleted customEmail `json:"userDeleted"`
|
|
|
|
InviteEmail customEmail `json:"inviteEmail"`
|
|
|
|
WelcomeEmail customEmail `json:"welcomeEmail"`
|
|
|
|
EmailConfirmation customEmail `json:"emailConfirmation"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type customEmail struct {
|
|
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Variables []string `json:"variables,omitempty"`
|
2020-10-30 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 21:11:28 +00:00
|
|
|
// timePattern: %Y-%m-%dT%H:%M:%S.%f
|
|
|
|
|
2020-09-20 10:21:04 +00:00
|
|
|
type Profile struct {
|
2021-02-19 00:47:01 +00:00
|
|
|
Admin bool `json:"admin,omitempty"`
|
|
|
|
LibraryAccess string `json:"libraries,omitempty"`
|
|
|
|
FromUser string `json:"fromUser,omitempty"`
|
|
|
|
Policy mediabrowser.Policy `json:"policy,omitempty"`
|
|
|
|
Configuration mediabrowser.Configuration `json:"configuration,omitempty"`
|
|
|
|
Displayprefs map[string]interface{} `json:"displayprefs,omitempty"`
|
|
|
|
Default bool `json:"default,omitempty"`
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 21:11:28 +00:00
|
|
|
type Invite struct {
|
2020-07-31 11:48:37 +00:00
|
|
|
Created time.Time `json:"created"`
|
2020-07-29 21:11:28 +00:00
|
|
|
NoLimit bool `json:"no-limit"`
|
|
|
|
RemainingUses int `json:"remaining-uses"`
|
|
|
|
ValidTill time.Time `json:"valid_till"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
UsedBy [][]string `json:"used-by"`
|
|
|
|
Notify map[string]map[string]bool `json:"notify"`
|
2020-09-20 10:21:04 +00:00
|
|
|
Profile string `json:"profile"`
|
2021-01-24 15:55:45 +00:00
|
|
|
Label string `json:"label,omitempty"`
|
2021-01-31 23:12:50 +00:00
|
|
|
Keys []string `json"keys,omitempty"`
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 00:29:29 +00:00
|
|
|
type Lang struct {
|
|
|
|
chosenFormLang string
|
|
|
|
chosenAdminLang string
|
|
|
|
chosenEmailLang string
|
|
|
|
AdminPath string
|
|
|
|
Admin adminLangs
|
|
|
|
AdminJSON map[string]string
|
|
|
|
FormPath string
|
|
|
|
Form formLangs
|
|
|
|
EmailPath string
|
|
|
|
Email emailLangs
|
2021-01-25 21:26:54 +00:00
|
|
|
CommonPath string
|
|
|
|
Common commonLangs
|
|
|
|
SetupPath string
|
|
|
|
Setup setupLangs
|
2021-01-19 00:29:29 +00:00
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
|
2021-02-01 17:39:19 +00:00
|
|
|
func (st *Storage) loadLang(filesystems ...fs.FS) (err error) {
|
|
|
|
err = st.loadLangCommon(filesystems...)
|
2021-01-25 21:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
err = st.loadLangAdmin(filesystems...)
|
2021-01-19 00:29:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
err = st.loadLangForm(filesystems...)
|
2021-01-19 00:29:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
err = st.loadLangEmail(filesystems...)
|
2021-01-19 00:29:29 +00:00
|
|
|
return
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 21:26:54 +00:00
|
|
|
func (common *commonLangs) patchCommon(lang string, other *langSection) {
|
|
|
|
if *other == nil {
|
|
|
|
*other = langSection{}
|
|
|
|
}
|
|
|
|
if _, ok := (*common)[lang]; !ok {
|
|
|
|
lang = "en-us"
|
|
|
|
}
|
|
|
|
for n, ev := range (*common)[lang].Strings {
|
|
|
|
if v, ok := (*other)[n]; !ok || v == "" {
|
|
|
|
(*other)[n] = ev
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 00:29:29 +00:00
|
|
|
// If a given language has missing values, fill it in with the english value.
|
|
|
|
func patchLang(english, other *langSection) {
|
2021-01-24 15:19:58 +00:00
|
|
|
if *other == nil {
|
|
|
|
*other = langSection{}
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
for n, ev := range *english {
|
|
|
|
if v, ok := (*other)[n]; !ok || v == "" {
|
|
|
|
(*other)[n] = ev
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 00:29:29 +00:00
|
|
|
func patchQuantityStrings(english, other *map[string]quantityString) {
|
|
|
|
for n, ev := range *english {
|
|
|
|
qs, ok := (*other)[n]
|
|
|
|
if !ok {
|
|
|
|
(*other)[n] = ev
|
|
|
|
return
|
|
|
|
} else if qs.Singular == "" {
|
|
|
|
qs.Singular = ev.Singular
|
|
|
|
} else if (*other)[n].Plural == "" {
|
|
|
|
qs.Plural = ev.Plural
|
|
|
|
}
|
|
|
|
(*other)[n] = qs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:39:19 +00:00
|
|
|
func (st *Storage) loadLangCommon(filesystems ...fs.FS) error {
|
2021-01-25 21:26:54 +00:00
|
|
|
st.lang.Common = map[string]commonLang{}
|
|
|
|
var english commonLang
|
2021-02-01 17:39:19 +00:00
|
|
|
load := func(filesystem fs.FS, fname string) error {
|
2021-01-25 21:26:54 +00:00
|
|
|
index := strings.TrimSuffix(fname, filepath.Ext(fname))
|
|
|
|
lang := commonLang{}
|
2021-02-08 12:03:22 +00:00
|
|
|
f, err := fs.ReadFile(filesystem, FSJoin(st.lang.CommonPath, fname))
|
2021-01-25 21:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if substituteStrings != "" {
|
|
|
|
f = []byte(strings.ReplaceAll(string(f), "Jellyfin", substituteStrings))
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(f, &lang)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fname != "en-us.json" {
|
|
|
|
patchLang(&english.Strings, &lang.Strings)
|
|
|
|
}
|
|
|
|
st.lang.Common[index] = lang
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
engFound := false
|
|
|
|
var err error
|
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
err = load(filesystem, "en-us.json")
|
|
|
|
if err == nil {
|
|
|
|
engFound = true
|
|
|
|
}
|
2021-01-25 21:26:54 +00:00
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
if !engFound {
|
2021-01-25 21:26:54 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
english = st.lang.Common["en-us"]
|
|
|
|
commonLoaded := false
|
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
files, err := fs.ReadDir(filesystem, st.lang.CommonPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() != "en-us.json" {
|
|
|
|
err = load(filesystem, f.Name())
|
|
|
|
if err == nil {
|
|
|
|
commonLoaded = true
|
|
|
|
}
|
2021-01-25 21:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
if !commonLoaded {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-25 21:26:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:39:19 +00:00
|
|
|
func (st *Storage) loadLangAdmin(filesystems ...fs.FS) error {
|
2021-01-19 00:29:29 +00:00
|
|
|
st.lang.Admin = map[string]adminLang{}
|
|
|
|
var english adminLang
|
2021-02-01 17:39:19 +00:00
|
|
|
load := func(filesystem fs.FS, fname string) error {
|
2021-01-19 00:29:29 +00:00
|
|
|
index := strings.TrimSuffix(fname, filepath.Ext(fname))
|
|
|
|
lang := adminLang{}
|
2021-02-08 12:03:22 +00:00
|
|
|
f, err := fs.ReadFile(filesystem, FSJoin(st.lang.AdminPath, fname))
|
2021-01-12 00:11:40 +00:00
|
|
|
if err != nil {
|
2021-01-19 00:29:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if substituteStrings != "" {
|
|
|
|
f = []byte(strings.ReplaceAll(string(f), "Jellyfin", substituteStrings))
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(f, &lang)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-25 21:26:54 +00:00
|
|
|
st.lang.Common.patchCommon(index, &lang.Strings)
|
2021-01-19 00:29:29 +00:00
|
|
|
if fname != "en-us.json" {
|
|
|
|
patchLang(&english.Strings, &lang.Strings)
|
|
|
|
patchLang(&english.Notifications, &lang.Notifications)
|
|
|
|
patchQuantityStrings(&english.QuantityStrings, &lang.QuantityStrings)
|
|
|
|
}
|
|
|
|
stringAdmin, err := json.Marshal(lang)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lang.JSON = string(stringAdmin)
|
|
|
|
st.lang.Admin[index] = lang
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
engFound := false
|
|
|
|
var err error
|
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
err = load(filesystem, "en-us.json")
|
|
|
|
if err == nil {
|
|
|
|
engFound = true
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
if !engFound {
|
2021-01-19 00:29:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
english = st.lang.Admin["en-us"]
|
|
|
|
adminLoaded := false
|
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
files, err := fs.ReadDir(filesystem, st.lang.AdminPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() != "en-us.json" {
|
|
|
|
err = load(filesystem, f.Name())
|
|
|
|
if err == nil {
|
|
|
|
adminLoaded = true
|
|
|
|
}
|
2021-01-12 00:11:40 +00:00
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
if !adminLoaded {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-12 23:15:12 +00:00
|
|
|
|
2021-02-01 17:39:19 +00:00
|
|
|
func (st *Storage) loadLangForm(filesystems ...fs.FS) error {
|
2021-01-19 00:29:29 +00:00
|
|
|
st.lang.Form = map[string]formLang{}
|
|
|
|
var english formLang
|
2021-02-01 17:39:19 +00:00
|
|
|
load := func(filesystem fs.FS, fname string) error {
|
2021-01-19 00:29:29 +00:00
|
|
|
index := strings.TrimSuffix(fname, filepath.Ext(fname))
|
|
|
|
lang := formLang{}
|
2021-02-08 12:03:22 +00:00
|
|
|
f, err := fs.ReadFile(filesystem, FSJoin(st.lang.FormPath, fname))
|
2021-01-19 00:29:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if substituteStrings != "" {
|
|
|
|
f = []byte(strings.ReplaceAll(string(f), "Jellyfin", substituteStrings))
|
2021-01-12 00:11:40 +00:00
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
err = json.Unmarshal(f, &lang)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-25 21:26:54 +00:00
|
|
|
st.lang.Common.patchCommon(index, &lang.Strings)
|
2021-01-19 00:29:29 +00:00
|
|
|
if fname != "en-us.json" {
|
|
|
|
patchLang(&english.Strings, &lang.Strings)
|
2021-01-25 18:01:18 +00:00
|
|
|
patchLang(&english.Notifications, &lang.Notifications)
|
2021-01-19 00:29:29 +00:00
|
|
|
patchQuantityStrings(&english.ValidationStrings, &lang.ValidationStrings)
|
|
|
|
}
|
2021-01-25 18:01:18 +00:00
|
|
|
notifications, err := json.Marshal(lang.Notifications)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
validationStrings, err := json.Marshal(lang.ValidationStrings)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-25 18:01:18 +00:00
|
|
|
lang.notificationsJSON = string(notifications)
|
2021-01-19 00:29:29 +00:00
|
|
|
lang.validationStringsJSON = string(validationStrings)
|
|
|
|
st.lang.Form[index] = lang
|
|
|
|
return nil
|
2021-01-12 00:11:40 +00:00
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
engFound := false
|
|
|
|
var err error
|
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
err = load(filesystem, "en-us.json")
|
|
|
|
if err == nil {
|
|
|
|
engFound = true
|
|
|
|
}
|
2020-10-30 22:51:47 +00:00
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
if !engFound {
|
2021-01-19 00:29:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
english = st.lang.Form["en-us"]
|
|
|
|
formLoaded := false
|
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
files, err := fs.ReadDir(filesystem, st.lang.FormPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() != "en-us.json" {
|
|
|
|
err = load(filesystem, f.Name())
|
|
|
|
if err == nil {
|
|
|
|
formLoaded = true
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
if !formLoaded {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:39:19 +00:00
|
|
|
func (st *Storage) loadLangEmail(filesystems ...fs.FS) error {
|
2021-01-19 00:29:29 +00:00
|
|
|
st.lang.Email = map[string]emailLang{}
|
|
|
|
var english emailLang
|
2021-02-01 17:39:19 +00:00
|
|
|
load := func(filesystem fs.FS, fname string) error {
|
2021-01-19 00:29:29 +00:00
|
|
|
index := strings.TrimSuffix(fname, filepath.Ext(fname))
|
|
|
|
lang := emailLang{}
|
2021-02-08 12:03:22 +00:00
|
|
|
f, err := fs.ReadFile(filesystem, FSJoin(st.lang.EmailPath, fname))
|
2021-01-11 19:17:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
if substituteStrings != "" {
|
|
|
|
f = []byte(strings.ReplaceAll(string(f), "Jellyfin", substituteStrings))
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(f, &lang)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
st.lang.Common.patchCommon(index, &lang.Strings)
|
2021-01-19 00:29:29 +00:00
|
|
|
if fname != "en-us.json" {
|
|
|
|
patchLang(&english.UserCreated, &lang.UserCreated)
|
|
|
|
patchLang(&english.InviteExpiry, &lang.InviteExpiry)
|
|
|
|
patchLang(&english.PasswordReset, &lang.PasswordReset)
|
|
|
|
patchLang(&english.UserDeleted, &lang.UserDeleted)
|
|
|
|
patchLang(&english.InviteEmail, &lang.InviteEmail)
|
2021-01-24 15:19:58 +00:00
|
|
|
patchLang(&english.WelcomeEmail, &lang.WelcomeEmail)
|
2021-01-19 00:29:29 +00:00
|
|
|
}
|
|
|
|
st.lang.Email[index] = lang
|
|
|
|
return nil
|
2020-10-30 22:51:47 +00:00
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
engFound := false
|
|
|
|
var err error
|
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
err = load(filesystem, "en-us.json")
|
|
|
|
if err == nil {
|
|
|
|
engFound = true
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
if !engFound {
|
2021-01-19 00:29:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
english = st.lang.Email["en-us"]
|
|
|
|
emailLoaded := false
|
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
files, err := fs.ReadDir(filesystem, st.lang.EmailPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() != "en-us.json" {
|
|
|
|
err = load(filesystem, f.Name())
|
|
|
|
if err == nil {
|
|
|
|
emailLoaded = true
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
}
|
2021-01-14 17:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 17:39:19 +00:00
|
|
|
if !emailLoaded {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
return nil
|
2020-10-30 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 00:29:29 +00:00
|
|
|
type Invites map[string]Invite
|
|
|
|
|
|
|
|
func (st *Storage) loadInvites() error {
|
|
|
|
return loadJSON(st.invite_path, &st.invites)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) storeInvites() error {
|
|
|
|
return storeJSON(st.invite_path, st.invites)
|
|
|
|
}
|
|
|
|
|
2020-07-29 21:11:28 +00:00
|
|
|
func (st *Storage) loadEmails() error {
|
|
|
|
return loadJSON(st.emails_path, &st.emails)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) storeEmails() error {
|
|
|
|
return storeJSON(st.emails_path, st.emails)
|
|
|
|
}
|
|
|
|
|
2021-02-19 21:38:20 +00:00
|
|
|
func (st *Storage) loadCustomEmails() error {
|
|
|
|
return loadJSON(st.customEmails_path, &st.customEmails)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) storeCustomEmails() error {
|
|
|
|
return storeJSON(st.customEmails_path, st.customEmails)
|
|
|
|
}
|
|
|
|
|
2020-07-29 21:11:28 +00:00
|
|
|
func (st *Storage) loadPolicy() error {
|
|
|
|
return loadJSON(st.policy_path, &st.policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) storePolicy() error {
|
|
|
|
return storeJSON(st.policy_path, st.policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) loadConfiguration() error {
|
|
|
|
return loadJSON(st.configuration_path, &st.configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) storeConfiguration() error {
|
|
|
|
return storeJSON(st.configuration_path, st.configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) loadDisplayprefs() error {
|
|
|
|
return loadJSON(st.displayprefs_path, &st.displayprefs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) storeDisplayprefs() error {
|
|
|
|
return storeJSON(st.displayprefs_path, st.displayprefs)
|
|
|
|
}
|
|
|
|
|
2020-09-05 16:32:49 +00:00
|
|
|
func (st *Storage) loadOmbiTemplate() error {
|
|
|
|
return loadJSON(st.ombi_path, &st.ombi_template)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) storeOmbiTemplate() error {
|
|
|
|
return storeJSON(st.ombi_path, st.ombi_template)
|
|
|
|
}
|
|
|
|
|
2020-09-20 10:21:04 +00:00
|
|
|
func (st *Storage) loadProfiles() error {
|
2020-09-22 23:01:07 +00:00
|
|
|
err := loadJSON(st.profiles_path, &st.profiles)
|
|
|
|
for name, profile := range st.profiles {
|
2020-09-23 17:48:00 +00:00
|
|
|
if profile.Default {
|
|
|
|
st.defaultProfile = name
|
|
|
|
}
|
2020-09-22 23:01:07 +00:00
|
|
|
change := false
|
2021-02-19 00:47:01 +00:00
|
|
|
if profile.Policy.IsAdministrator != profile.Admin {
|
2020-09-22 23:01:07 +00:00
|
|
|
change = true
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
profile.Admin = profile.Policy.IsAdministrator
|
|
|
|
if profile.Policy.EnabledFolders != nil {
|
|
|
|
length := len(profile.Policy.EnabledFolders)
|
2020-09-22 23:01:07 +00:00
|
|
|
if length == 0 {
|
|
|
|
profile.LibraryAccess = "All"
|
|
|
|
} else {
|
|
|
|
profile.LibraryAccess = strconv.Itoa(length)
|
|
|
|
}
|
|
|
|
change = true
|
|
|
|
}
|
|
|
|
if profile.FromUser == "" {
|
|
|
|
profile.FromUser = "Unknown"
|
|
|
|
change = true
|
|
|
|
}
|
|
|
|
if change {
|
|
|
|
st.profiles[name] = profile
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 17:48:00 +00:00
|
|
|
if st.defaultProfile == "" {
|
|
|
|
for n := range st.profiles {
|
|
|
|
st.defaultProfile = n
|
|
|
|
}
|
2020-09-22 23:01:07 +00:00
|
|
|
}
|
|
|
|
return err
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) storeProfiles() error {
|
|
|
|
return storeJSON(st.profiles_path, st.profiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *Storage) migrateToProfile() error {
|
|
|
|
st.loadPolicy()
|
|
|
|
st.loadConfiguration()
|
|
|
|
st.loadDisplayprefs()
|
|
|
|
st.loadProfiles()
|
|
|
|
st.profiles["Default"] = Profile{
|
|
|
|
Policy: st.policy,
|
|
|
|
Configuration: st.configuration,
|
|
|
|
Displayprefs: st.displayprefs,
|
|
|
|
}
|
|
|
|
return st.storeProfiles()
|
|
|
|
}
|
|
|
|
|
2020-07-29 21:11:28 +00:00
|
|
|
func loadJSON(path string, obj interface{}) error {
|
2020-08-01 20:20:02 +00:00
|
|
|
var file []byte
|
|
|
|
var err error
|
2021-01-31 23:12:50 +00:00
|
|
|
file, err = os.ReadFile(path)
|
2020-07-29 21:11:28 +00:00
|
|
|
if err != nil {
|
2020-08-01 20:20:02 +00:00
|
|
|
file = []byte("{}")
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
err = json.Unmarshal(file, &obj)
|
2020-11-02 23:20:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: Failed to read \"%s\": %s", path, err)
|
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func storeJSON(path string, obj interface{}) error {
|
|
|
|
data, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-31 23:12:50 +00:00
|
|
|
err = os.WriteFile(path, data, 0644)
|
2020-11-02 23:20:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: Failed to write to \"%s\": %s", path, err)
|
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-29 18:01:10 +00:00
|
|
|
|
2020-12-18 15:44:19 +00:00
|
|
|
// One build of JF 10.7.0 hyphenated user IDs while another one later didn't. These functions will hyphenate/de-hyphenate email storage.
|
|
|
|
|
|
|
|
func hyphenate(userID string) string {
|
|
|
|
if userID[8] == '-' {
|
|
|
|
return userID
|
|
|
|
}
|
|
|
|
return userID[:8] + "-" + userID[8:12] + "-" + userID[12:16] + "-" + userID[16:20] + "-" + userID[20:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) deHyphenateEmailStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
|
|
|
|
jfUsers, status, err := app.jf.GetUsers(false)
|
|
|
|
if status != 200 || err != nil {
|
|
|
|
return nil, status, err
|
|
|
|
}
|
|
|
|
newEmails := map[string]interface{}{}
|
|
|
|
for _, user := range jfUsers {
|
2021-02-19 00:47:01 +00:00
|
|
|
unHyphenated := user.ID
|
2020-12-18 15:44:19 +00:00
|
|
|
hyphenated := hyphenate(unHyphenated)
|
|
|
|
email, ok := old[hyphenated]
|
|
|
|
if ok {
|
|
|
|
newEmails[unHyphenated] = email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newEmails, status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) hyphenateEmailStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
|
2020-11-29 18:01:10 +00:00
|
|
|
jfUsers, status, err := app.jf.GetUsers(false)
|
|
|
|
if status != 200 || err != nil {
|
|
|
|
return nil, status, err
|
|
|
|
}
|
|
|
|
newEmails := map[string]interface{}{}
|
|
|
|
for _, user := range jfUsers {
|
2021-02-19 00:47:01 +00:00
|
|
|
unstripped := user.ID
|
2020-11-29 18:01:10 +00:00
|
|
|
stripped := strings.ReplaceAll(unstripped, "-", "")
|
|
|
|
email, ok := old[stripped]
|
|
|
|
if ok {
|
|
|
|
newEmails[unstripped] = email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newEmails, status, err
|
|
|
|
}
|