upgrade download bugfix

This commit is contained in:
Robert McRackan 2021-07-18 16:29:31 -04:00
parent 491aa67a81
commit e61418c677
6 changed files with 41 additions and 32 deletions

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> --> <!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>5.1.9.13</Version> <Version>5.1.10.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -277,8 +277,8 @@ namespace LibationLauncher
{ // appsettings.json { // appsettings.json
var appSettingsKey = UNSAFE_MigrationHelper.LIBATION_FILES_KEY; var appSettingsKey = UNSAFE_MigrationHelper.LIBATION_FILES_KEY;
if (UNSAFE_MigrationHelper.AppSettings_TryGet(appSettingsKey, out var value)) if (UNSAFE_MigrationHelper.APPSETTINGS_TryGet(appSettingsKey, out var value))
UNSAFE_MigrationHelper.AppSettings_Update(appSettingsKey, translatePath(value)); UNSAFE_MigrationHelper.APPSETTINGS_Update(appSettingsKey, translatePath(value));
} }
} }
@ -421,8 +421,7 @@ namespace LibationLauncher
try try
{ {
LibationWinForms.BookLiberation.ProcessorAutomationController.DownloadFileAsync(zipUrl, selectedPath).GetAwaiter().GetResult(); LibationWinForms.BookLiberation.ProcessorAutomationController.DownloadFile(zipUrl, selectedPath, true);
MessageBox.Show("File downloaded");
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -12,44 +12,44 @@ namespace LibationLauncher
internal static class UNSAFE_MigrationHelper internal static class UNSAFE_MigrationHelper
{ {
#region appsettings.json #region appsettings.json
public const string APPSETTINGS_JSON = "appsettings.json"; private const string APPSETTINGS_JSON = "appsettings.json";
public static bool AppSettingsJson_Exists => File.Exists(APPSETTINGS_JSON); public static bool APPSETTINGS_Json_Exists => File.Exists(APPSETTINGS_JSON);
public static bool AppSettings_TryGet(string key, out string value) public static bool APPSETTINGS_TryGet(string key, out string value)
{ {
bool success = false; bool success = false;
JToken val = null; JToken val = null;
process_AppSettingsJson(jObj => success = jObj.TryGetValue(key, out val), false); process_APPSETTINGS_Json(jObj => success = jObj.TryGetValue(key, out val), false);
value = success ? val.Value<string>() : null; value = success ? val.Value<string>() : null;
return success; return success;
} }
/// <summary>only insert if not exists</summary> /// <summary>only insert if not exists</summary>
public static void AppSettings_Insert(string key, string value) public static void APPSETTINGS_Insert(string key, string value)
=> process_AppSettingsJson(jObj => jObj.TryAdd(key, value)); => process_APPSETTINGS_Json(jObj => jObj.TryAdd(key, value));
/// <summary>only update if exists</summary> /// <summary>only update if exists</summary>
public static void AppSettings_Update(string key, string value) public static void APPSETTINGS_Update(string key, string value)
=> process_AppSettingsJson(jObj => { => process_APPSETTINGS_Json(jObj => {
if (jObj.ContainsKey(key)) if (jObj.ContainsKey(key))
jObj[key] = value; jObj[key] = value;
}); });
/// <summary>only delete if exists</summary> /// <summary>only delete if exists</summary>
public static void AppSettings_Delete(string key) public static void APPSETTINGS_Delete(string key)
=> process_AppSettingsJson(jObj => { => process_APPSETTINGS_Json(jObj => {
if (jObj.ContainsKey(key)) if (jObj.ContainsKey(key))
jObj.Remove(key); jObj.Remove(key);
}); });
/// <param name="save">True: save if contents changed. False: no not attempt save</param> /// <param name="save">True: save if contents changed. False: no not attempt save</param>
private static void process_AppSettingsJson(Action<JObject> action, bool save = true) private static void process_APPSETTINGS_Json(Action<JObject> action, bool save = true)
{ {
// only insert if not exists // only insert if not exists
if (!AppSettingsJson_Exists) if (!APPSETTINGS_Json_Exists)
return; return;
var startingContents = File.ReadAllText(APPSETTINGS_JSON); var startingContents = File.ReadAllText(APPSETTINGS_JSON);
@ -73,13 +73,13 @@ namespace LibationLauncher
#region Settings.json #region Settings.json
public const string LIBATION_FILES_KEY = "LibationFiles"; public const string LIBATION_FILES_KEY = "LibationFiles";
public const string SETTINGS_JSON = "Settings.json"; private const string SETTINGS_JSON = "Settings.json";
public static string SettingsJsonPath public static string SettingsJsonPath
{ {
get get
{ {
var success = AppSettings_TryGet(LIBATION_FILES_KEY, out var value); var success = APPSETTINGS_TryGet(LIBATION_FILES_KEY, out var value);
return !success || value is null ? null : Path.Combine(value, SETTINGS_JSON); return !success || value is null ? null : Path.Combine(value, SETTINGS_JSON);
} }
} }

View File

@ -46,7 +46,7 @@ namespace LibationWinForms.BookLiberation
lastUpdateLbl.UIThread(() => lastUpdateLbl.Visible = lastDownloadProgress.AddSeconds(30) < DateTime.Now); lastUpdateLbl.UIThread(() => lastUpdateLbl.Visible = lastDownloadProgress.AddSeconds(30) < DateTime.Now);
if (lastUpdateLbl.Visible) if (lastUpdateLbl.Visible)
{ {
var diff = lastDownloadProgress - DateTime.Now; var diff = DateTime.Now - lastDownloadProgress;
var min = (int)diff.TotalMinutes; var min = (int)diff.TotalMinutes;
var minText = min > 0 ? $"{min}min " : ""; var minText = min > 0 ? $"{min}min " : "";

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using DataLayer; using DataLayer;
using Dinah.Core.ErrorHandling; using Dinah.Core.ErrorHandling;
using Dinah.Core.Windows.Forms;
using FileLiberator; using FileLiberator;
namespace LibationWinForms.BookLiberation namespace LibationWinForms.BookLiberation
@ -168,21 +169,27 @@ namespace LibationWinForms.BookLiberation
return downloadPdf; return downloadPdf;
} }
public static async Task DownloadFileAsync(string url, string destination) public static void DownloadFile(string url, string destination, bool showDownloadCompletedDialog = false)
{ {
var downloadDialog = new DownloadForm();
downloadDialog.UpdateFilename(destination);
downloadDialog.Show();
new System.Threading.Thread(() => {
var downloadFile = new DownloadFile(); var downloadFile = new DownloadFile();
// frustratingly copy pasta from wireUpEvents(IDownloadable downloadable) due to Completed being EventHandler<LibraryBook> downloadFile.DownloadProgressChanged += (_, progress) => downloadDialog.UIThread(() =>
var downloadDialog = new DownloadForm(); downloadDialog.DownloadProgressChanged(progress.BytesReceived, progress.TotalBytesToReceive)
downloadFile.DownloadBegin += (_, str) => );
downloadFile.DownloadCompleted += (_, __) => downloadDialog.UIThread(() =>
{ {
downloadDialog.UpdateFilename(str); downloadDialog.Close();
downloadDialog.Show(); if (showDownloadCompletedDialog)
}; MessageBox.Show("File downloaded");
downloadFile.DownloadProgressChanged += (_, progress) => downloadDialog.DownloadProgressChanged(progress.BytesReceived, progress.TotalBytesToReceive); });
downloadFile.DownloadCompleted += (_, __) => downloadDialog.Close();
await downloadFile.PerformDownloadFileAsync(url, destination); downloadFile.PerformDownloadFileAsync(url, destination).GetAwaiter().GetResult();
}).Start();
} }
// subscribed to Begin event because a new form should be created+processed+closed on each iteration // subscribed to Begin event because a new form should be created+processed+closed on each iteration

View File

@ -37,6 +37,9 @@ namespace LibationWinForms
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
{ {
if (this.DesignMode)
return;
// load default/missing cover images. this will also initiate the background image downloader // load default/missing cover images. this will also initiate the background image downloader
var format = System.Drawing.Imaging.ImageFormat.Jpeg; var format = System.Drawing.Imaging.ImageFormat.Jpeg;
PictureStorage.SetDefaultImage(PictureSize._80x80, Properties.Resources.default_cover_80x80.ToBytes(format)); PictureStorage.SetDefaultImage(PictureSize._80x80, Properties.Resources.default_cover_80x80.ToBytes(format));