1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 01:20:11 +00:00

hyphenate/dehyphenate users.json if necessary

doubt this would have caused problems anyway but why not.
This commit is contained in:
Harvey Tindall 2021-04-06 15:46:28 +01:00
parent 8a6cfe0b4d
commit 78049d4a33
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
2 changed files with 72 additions and 16 deletions

36
main.go
View File

@ -489,31 +489,47 @@ func start(asDaemon, firstCall bool) {
} }
if noHyphens == app.jf.Hyphens { if noHyphens == app.jf.Hyphens {
var newEmails map[string]interface{} var newEmails map[string]interface{}
var status int var newUsers map[string]time.Time
var err error var status, status2 int
var err, err2 error
if app.jf.Hyphens { if app.jf.Hyphens {
app.info.Println(info("Your build of Jellyfin appears to hypenate user IDs. Your emails.json file will be modified to match.")) app.info.Println(info("Your build of Jellyfin appears to hypenate user IDs. Your emails.json/users.json file will be modified to match."))
time.Sleep(time.Second * time.Duration(3)) time.Sleep(time.Second * time.Duration(3))
newEmails, status, err = app.hyphenateEmailStorage(app.storage.emails) newEmails, status, err = app.hyphenateEmailStorage(app.storage.emails)
newUsers, status2, err2 = app.hyphenateUserStorage(app.storage.users)
} else { } else {
app.info.Println(info("Your emails.json file uses hyphens, but the Jellyfin server no longer does. It will be modified.")) app.info.Println(info("Your emails.json/users.json file uses hyphens, but the Jellyfin server no longer does. It will be modified."))
time.Sleep(time.Second * time.Duration(3)) time.Sleep(time.Second * time.Duration(3))
newEmails, status, err = app.deHyphenateEmailStorage(app.storage.emails) newEmails, status, err = app.deHyphenateEmailStorage(app.storage.emails)
newUsers, status2, err2 = app.deHyphenateUserStorage(app.storage.users)
} }
if status != 200 || err != nil { if status != 200 || err != nil {
app.err.Printf("Failed to get users from Jellyfin: Code %d", status) app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err)
app.debug.Printf("Error: %s", err)
app.err.Fatalf("Couldn't upgrade emails.json") app.err.Fatalf("Couldn't upgrade emails.json")
} }
bakFile := app.storage.emails_path + ".bak" if status2 != 200 || err2 != nil {
err = storeJSON(bakFile, app.storage.emails) app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err)
app.err.Fatalf("Couldn't upgrade users.json")
}
emailBakFile := app.storage.emails_path + ".bak"
usersBakFile := app.storage.users_path + ".bak"
err = storeJSON(emailBakFile, app.storage.emails)
err2 = storeJSON(usersBakFile, app.storage.users)
if err != nil { if err != nil {
app.err.Fatalf("couldn't store emails.json backup: %s", err) app.err.Fatalf("couldn't store emails.json backup: %v", err)
}
if err2 != nil {
app.err.Fatalf("couldn't store users.json backup: %v", err)
} }
app.storage.emails = newEmails app.storage.emails = newEmails
app.storage.users = newUsers
err = app.storage.storeEmails() err = app.storage.storeEmails()
err2 = app.storage.storeUsers()
if err != nil { if err != nil {
app.err.Fatalf("couldn't store emails.json: %s", err) app.err.Fatalf("couldn't store emails.json: %v", err)
}
if err2 != nil {
app.err.Fatalf("couldn't store users.json: %v", err)
} }
} }
} }

View File

@ -638,7 +638,7 @@ func hyphenate(userID string) string {
return userID[:8] + "-" + userID[8:12] + "-" + userID[12:16] + "-" + userID[16:20] + "-" + userID[20:] 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) { func (app *appContext) deHyphenateStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
jfUsers, status, err := app.jf.GetUsers(false) jfUsers, status, err := app.jf.GetUsers(false)
if status != 200 || err != nil { if status != 200 || err != nil {
return nil, status, err return nil, status, err
@ -647,15 +647,15 @@ func (app *appContext) deHyphenateEmailStorage(old map[string]interface{}) (map[
for _, user := range jfUsers { for _, user := range jfUsers {
unHyphenated := user.ID unHyphenated := user.ID
hyphenated := hyphenate(unHyphenated) hyphenated := hyphenate(unHyphenated)
email, ok := old[hyphenated] val, ok := old[hyphenated]
if ok { if ok {
newEmails[unHyphenated] = email newEmails[unHyphenated] = val
} }
} }
return newEmails, status, err return newEmails, status, err
} }
func (app *appContext) hyphenateEmailStorage(old map[string]interface{}) (map[string]interface{}, int, error) { func (app *appContext) hyphenateStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
jfUsers, status, err := app.jf.GetUsers(false) jfUsers, status, err := app.jf.GetUsers(false)
if status != 200 || err != nil { if status != 200 || err != nil {
return nil, status, err return nil, status, err
@ -664,10 +664,50 @@ func (app *appContext) hyphenateEmailStorage(old map[string]interface{}) (map[st
for _, user := range jfUsers { for _, user := range jfUsers {
unstripped := user.ID unstripped := user.ID
stripped := strings.ReplaceAll(unstripped, "-", "") stripped := strings.ReplaceAll(unstripped, "-", "")
email, ok := old[stripped] val, ok := old[stripped]
if ok { if ok {
newEmails[unstripped] = email newEmails[unstripped] = val
} }
} }
return newEmails, status, err return newEmails, status, err
} }
func (app *appContext) hyphenateEmailStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
return app.hyphenateStorage(old)
}
func (app *appContext) deHyphenateEmailStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
return app.deHyphenateStorage(old)
}
func (app *appContext) hyphenateUserStorage(old map[string]time.Time) (map[string]time.Time, int, error) {
asInterface := map[string]interface{}{}
for k, v := range old {
asInterface[k] = v
}
fixed, status, err := app.hyphenateStorage(asInterface)
if err != nil {
return nil, status, err
}
out := map[string]time.Time{}
for k, v := range fixed {
out[k] = v.(time.Time)
}
return out, status, err
}
func (app *appContext) deHyphenateUserStorage(old map[string]time.Time) (map[string]time.Time, int, error) {
asInterface := map[string]interface{}{}
for k, v := range old {
asInterface[k] = v
}
fixed, status, err := app.deHyphenateStorage(asInterface)
if err != nil {
return nil, status, err
}
out := map[string]time.Time{}
for k, v := range fixed {
out[k] = v.(time.Time)
}
return out, status, err
}