Libation 4.0 prep: migrate old settings => new files. remove old values. remove old file
This commit is contained in:
parent
1ad2135a3f
commit
04a32533cb
@ -16,17 +16,15 @@ namespace InternalUtilities
|
|||||||
|
|
||||||
public static void EnsureAccountsSettingsFileExists()
|
public static void EnsureAccountsSettingsFileExists()
|
||||||
{
|
{
|
||||||
if (File.Exists(AccountsSettingsFile))
|
|
||||||
return;
|
|
||||||
|
|
||||||
// saves. BEWARE: this will overwrite an existing file
|
// saves. BEWARE: this will overwrite an existing file
|
||||||
|
if (!File.Exists(AccountsSettingsFile))
|
||||||
_ = new AccountsPersister(new Accounts(), AccountsSettingsFile);
|
_ = new AccountsPersister(new Accounts(), AccountsSettingsFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEMP
|
// TEMP
|
||||||
public static string GetJsonPath() => null;
|
public static string GetIdentityTokensJsonPath() => null;
|
||||||
|
|
||||||
public static string GetJsonPath(string username, string locale)
|
public static string GetIdentityTokensJsonPath(string username, string locale)
|
||||||
{
|
{
|
||||||
var usernameSanitized = JsonConvert.ToString(username);
|
var usernameSanitized = JsonConvert.ToString(username);
|
||||||
var localeSanitized = JsonConvert.ToString(locale);
|
var localeSanitized = JsonConvert.ToString(locale);
|
||||||
|
|||||||
@ -87,12 +87,24 @@ namespace LibationLauncher
|
|||||||
if (!File.Exists(AudibleApiStorage.AccountsSettingsFileLegacy30))
|
if (!File.Exists(AudibleApiStorage.AccountsSettingsFileLegacy30))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// in here: don't rely on applicable POCOs. some is legacy and must be: json file => JObject
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//
|
updateLegacyFileWithLocale();
|
||||||
// in here: read directly from json file => JObject. A lot of this is legacy; don't rely on applicable POCOs
|
|
||||||
//
|
|
||||||
|
|
||||||
|
var account = addAccountToNewAccountFile();
|
||||||
|
|
||||||
|
importDecryptKey(account);
|
||||||
|
}
|
||||||
|
// migration is a convenience. if something goes wrong: just move on
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
// delete legacy token file
|
||||||
|
File.Delete(AudibleApiStorage.AccountsSettingsFileLegacy30);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void updateLegacyFileWithLocale()
|
||||||
|
{
|
||||||
var legacyContents = File.ReadAllText(AudibleApiStorage.AccountsSettingsFileLegacy30);
|
var legacyContents = File.ReadAllText(AudibleApiStorage.AccountsSettingsFileLegacy30);
|
||||||
var legacyJObj = JObject.Parse(legacyContents);
|
var legacyJObj = JObject.Parse(legacyContents);
|
||||||
|
|
||||||
@ -109,33 +121,64 @@ namespace LibationLauncher
|
|||||||
// save
|
// save
|
||||||
var newContents = legacyJObj.ToString(Formatting.Indented);
|
var newContents = legacyJObj.ToString(Formatting.Indented);
|
||||||
File.WriteAllText(AudibleApiStorage.AccountsSettingsFileLegacy30, newContents);
|
File.WriteAllText(AudibleApiStorage.AccountsSettingsFileLegacy30, newContents);
|
||||||
|
}
|
||||||
// re get contents
|
|
||||||
legacyContents = File.ReadAllText(AudibleApiStorage.AccountsSettingsFileLegacy30);
|
|
||||||
legacyJObj = JObject.Parse(legacyContents);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new account stub in new file
|
private static Account addAccountToNewAccountFile()
|
||||||
|
{
|
||||||
var api = AudibleApiActions.GetApiAsyncLegacy30Async().GetAwaiter().GetResult();
|
var api = AudibleApiActions.GetApiAsyncLegacy30Async().GetAwaiter().GetResult();
|
||||||
var email = api.GetEmailAsync().GetAwaiter().GetResult();
|
var email = api.GetEmailAsync().GetAwaiter().GetResult();
|
||||||
var locale = api.GetLocaleAsync(AudibleApi.CustomerOptions.All).GetAwaiter().GetResult();
|
var locale = api.GetLocaleAsync(AudibleApi.CustomerOptions.All).GetAwaiter().GetResult();
|
||||||
|
|
||||||
// more to do?
|
// identity has likely been updated above. re-get contents
|
||||||
|
var legacyContents = File.ReadAllText(AudibleApiStorage.AccountsSettingsFileLegacy30);
|
||||||
|
|
||||||
}
|
var identity = AudibleApi.Authorization.Identity.FromJson(legacyContents);
|
||||||
catch
|
|
||||||
|
if (!identity.IsValid)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var accountsPersist = new AccountsPersister(AudibleApiStorage.AccountsSettingsFile);
|
||||||
|
var account = new Account(email)
|
||||||
{
|
{
|
||||||
// migration is a convenience. if something goes wrong: just move on
|
AccountName = $"{email} - {locale.Name}",
|
||||||
|
IdentityTokens = identity
|
||||||
|
};
|
||||||
|
|
||||||
|
// saves to new file
|
||||||
|
accountsPersist.Accounts.Add(account);
|
||||||
|
|
||||||
|
return account;
|
||||||
}
|
}
|
||||||
|
|
||||||
// more to do. prob deleting legacy token file
|
private static void importDecryptKey(Account account)
|
||||||
|
{
|
||||||
|
if (account is null || !string.IsNullOrWhiteSpace(account.DecryptKey) || !File.Exists(Configuration.Instance.SettingsFilePath))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var settingsContents = File.ReadAllText(Configuration.Instance.SettingsFilePath);
|
||||||
|
if (JObject.Parse(settingsContents).TryGetValue("DecryptKey", out var jToken))
|
||||||
|
{
|
||||||
|
var decryptKey = jToken.Value<string>() ?? "";
|
||||||
|
account.DecryptKey = decryptKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void updateSettingsFile()
|
private static void updateSettingsFile()
|
||||||
{
|
{
|
||||||
//throw new NotImplementedException();
|
if (!File.Exists(Configuration.Instance.SettingsFilePath))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// use JObject to remove decrypt key and locale from Settings.json
|
||||||
|
var settingsContents = File.ReadAllText(Configuration.Instance.SettingsFilePath);
|
||||||
|
var jObj = JObject.Parse(settingsContents);
|
||||||
|
|
||||||
|
jObj.Property("DecryptKey")?.Remove();
|
||||||
|
jObj.Property("LocaleCountryCode")?.Remove();
|
||||||
|
|
||||||
|
// remember to remove these from Configuration.cs
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string defaultLoggingLevel { get; } = "Information";
|
private static string defaultLoggingLevel { get; } = "Information";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user