2020-08-01 23:05:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-25 21:26:54 +00:00
|
|
|
"encoding/json"
|
2021-02-01 17:39:19 +00:00
|
|
|
"io/fs"
|
2021-01-25 21:26:54 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2020-08-01 23:05:35 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2020-11-02 00:53:08 +00:00
|
|
|
"github.com/hrfee/jfa-go/common"
|
2021-01-09 20:38:13 +00:00
|
|
|
"github.com/hrfee/jfa-go/mediabrowser"
|
2020-08-01 23:05:35 +00:00
|
|
|
)
|
|
|
|
|
2021-01-25 21:26:54 +00:00
|
|
|
func (app *appContext) ServeSetup(gc *gin.Context) {
|
|
|
|
lang := gc.Query("lang")
|
|
|
|
if lang == "" {
|
|
|
|
lang = "en-us"
|
|
|
|
} else if _, ok := app.storage.lang.Admin[lang]; !ok {
|
|
|
|
lang = "en-us"
|
|
|
|
}
|
|
|
|
emailLang := lang
|
|
|
|
if _, ok := app.storage.lang.Email[lang]; !ok {
|
|
|
|
emailLang = "en-us"
|
|
|
|
}
|
2021-01-26 22:57:29 +00:00
|
|
|
|
|
|
|
messages := map[string]map[string]string{
|
|
|
|
"ui": {
|
|
|
|
"contact_message": app.config.Section("ui").Key("contact_message").String(),
|
|
|
|
"help_message": app.config.Section("ui").Key("help_message").String(),
|
|
|
|
"success_message": app.config.Section("ui").Key("success_message").String(),
|
|
|
|
},
|
|
|
|
"email": {
|
|
|
|
"message": app.config.Section("email").Key("message").String(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
msg, err := json.Marshal(messages)
|
|
|
|
if err != nil {
|
|
|
|
respond(500, "Failed to fetch default values", gc)
|
|
|
|
return
|
|
|
|
}
|
2021-01-27 21:37:43 +00:00
|
|
|
gc.HTML(200, "setup.html", gin.H{
|
2021-01-25 21:26:54 +00:00
|
|
|
"lang": app.storage.lang.Setup[lang],
|
|
|
|
"emailLang": app.storage.lang.Email[emailLang],
|
2021-01-26 22:57:29 +00:00
|
|
|
"language": app.storage.lang.Setup[lang].JSON,
|
|
|
|
"messages": string(msg),
|
2021-01-25 21:26:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:05:35 +00:00
|
|
|
type testReq struct {
|
2021-01-27 12:55:39 +00:00
|
|
|
ServerType string `json:"type"`
|
|
|
|
Server string `json:"server"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2020-08-01 23:05:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) TestJF(gc *gin.Context) {
|
2020-08-01 23:05:35 +00:00
|
|
|
var req testReq
|
|
|
|
gc.BindJSON(&req)
|
2021-01-27 12:55:39 +00:00
|
|
|
serverType := mediabrowser.JellyfinServer
|
|
|
|
if req.ServerType == "emby" {
|
|
|
|
serverType = mediabrowser.EmbyServer
|
|
|
|
}
|
|
|
|
tempjf, _ := mediabrowser.NewServer(serverType, req.Server, "jfa-go-setup", app.version, "auth", "auth", common.NewTimeoutHandler("authJF", req.Server, true), 30)
|
2020-11-02 00:53:08 +00:00
|
|
|
_, status, err := tempjf.Authenticate(req.Username, req.Password)
|
2020-08-01 23:05:35 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.info.Printf("Auth failed with code %d (%s)", status, err)
|
2020-08-01 23:05:35 +00:00
|
|
|
gc.JSON(401, map[string]bool{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gc.JSON(200, map[string]bool{"success": true})
|
|
|
|
}
|
2021-01-25 21:26:54 +00:00
|
|
|
|
2021-02-01 17:39:19 +00:00
|
|
|
// The first filesystem passed should be the localFS, to ensure the local lang files are loaded first.
|
|
|
|
func (st *Storage) loadLangSetup(filesystems ...fs.FS) error {
|
2021-01-25 21:26:54 +00:00
|
|
|
st.lang.Setup = map[string]setupLang{}
|
|
|
|
var english setupLang
|
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 := setupLang{}
|
2021-02-08 12:03:22 +00:00
|
|
|
f, err := fs.ReadFile(filesystem, FSJoin(st.lang.SetupPath, fname))
|
2021-01-25 21:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(f, &lang)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
st.lang.Common.patchCommon(index, &lang.Strings)
|
|
|
|
if fname != "en-us.json" {
|
|
|
|
patchLang(&english.Strings, &lang.Strings)
|
|
|
|
patchLang(&english.StartPage, &lang.StartPage)
|
|
|
|
patchLang(&english.EndPage, &lang.EndPage)
|
|
|
|
patchLang(&english.Language, &lang.Language)
|
|
|
|
patchLang(&english.Login, &lang.Login)
|
|
|
|
patchLang(&english.JellyfinEmby, &lang.JellyfinEmby)
|
|
|
|
patchLang(&english.Email, &lang.Email)
|
|
|
|
patchLang(&english.Notifications, &lang.Notifications)
|
|
|
|
patchLang(&english.PasswordResets, &lang.PasswordResets)
|
|
|
|
patchLang(&english.InviteEmails, &lang.InviteEmails)
|
|
|
|
patchLang(&english.PasswordValidation, &lang.PasswordValidation)
|
|
|
|
patchLang(&english.HelpMessages, &lang.HelpMessages)
|
|
|
|
}
|
2021-01-26 22:57:29 +00:00
|
|
|
stringSettings, err := json.Marshal(lang)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lang.JSON = string(stringSettings)
|
2021-01-25 21:26:54 +00:00
|
|
|
st.lang.Setup[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.Setup["en-us"]
|
2021-02-01 18:41:45 +00:00
|
|
|
setupLoaded := false
|
2021-02-01 17:39:19 +00:00
|
|
|
for _, filesystem := range filesystems {
|
|
|
|
files, err := fs.ReadDir(filesystem, st.lang.SetupPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() != "en-us.json" {
|
|
|
|
err = load(filesystem, f.Name())
|
2021-02-01 18:41:45 +00:00
|
|
|
if err == nil {
|
|
|
|
setupLoaded = true
|
2021-02-01 17:39:19 +00:00
|
|
|
}
|
2021-01-25 21:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 18:41:45 +00:00
|
|
|
if !setupLoaded {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-25 21:26:54 +00:00
|
|
|
return nil
|
|
|
|
}
|