fixed up downloading

This commit is contained in:
Bitl 2021-09-13 14:56:45 -07:00
parent 4f0c904ba6
commit be5986a09c
3 changed files with 59 additions and 14 deletions

View File

@ -1908,5 +1908,13 @@ public class GlobalFuncs
log.Error("EXCEPTION|ADDITIONAL INFO: " + (ex != null ? ex.ToString() : "N/A"));
}
#endif
//http://stevenhollidge.blogspot.com/2012/06/async-taskdelay.html
public static Task Delay(int milliseconds)
{
var tcs = new TaskCompletionSource<object>();
new System.Threading.Timer(_ => tcs.SetResult(null)).Change(milliseconds, -1);
return tcs.Task;
}
}
#endregion

View File

@ -2,6 +2,7 @@
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
#endregion
@ -65,7 +66,6 @@ class Downloader
{
outputfilename = fileName + fileext;
}
string fullpath = path + "\\" + outputfilename;
@ -90,23 +90,37 @@ class Downloader
public void InitDownloadNoDialog(string name, string additionalText = "")
{
int read = 0;
try
{
int read = DownloadFile(fileURL, name);
if (string.IsNullOrWhiteSpace(downloadOutcomeException))
{
downloadOutcome = "File " + Path.GetFileName(name) + " downloaded! " + GlobalFuncs.SizeSuffix(Convert.ToInt64(read), 2) + " written (" + read + " bytes)! " + additionalText;
}
else
{
downloadOutcome = "Download of file " + Path.GetFileName(name) + " failed. " + downloadOutcomeException;
}
read = DownloadFile(fileURL, name);
}
catch (Exception ex)
{
GlobalFuncs.LogExceptions(ex);
downloadOutcome = "Error when downloading file: " + ex.Message;
}
finally
{
//wait a few seconds for the download to finish
Thread.Sleep(2000);
if (File.Exists(name))
{
downloadOutcome = "File " + Path.GetFileName(name) + " downloaded! " + GlobalFuncs.SizeSuffix(Convert.ToInt64(read), 2) + " written (" + read + " bytes)! " + additionalText;
}
else
{
if (!string.IsNullOrWhiteSpace(downloadOutcomeException))
{
downloadOutcome = "Error: Download of file " + Path.GetFileName(name) + " failed. " + downloadOutcomeException;
}
else
{
downloadOutcome = "Error: Download of file " + Path.GetFileName(name) + " failed. The file wasn't downloaded to the assigned directory.";
}
}
}
}
public string GetFullDLPath()

View File

@ -121,7 +121,14 @@ public partial class AssetSDK : Form
if (!string.IsNullOrWhiteSpace(download.getDownloadOutcome()))
{
MessageBox.Show(download.getDownloadOutcome(), "Novetus Asset SDK - Download Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBoxIcon boxicon = MessageBoxIcon.Information;
if (download.getDownloadOutcome().Contains("Error"))
{
boxicon = MessageBoxIcon.Error;
}
MessageBox.Show(download.getDownloadOutcome(), "Novetus Asset SDK - Download Completed", MessageBoxButtons.OK, boxicon);
}
}
else
@ -136,8 +143,10 @@ public partial class AssetSDK : Form
}
}
public static void StartItemBatchDownload(string name, string url, string id, int ver, bool iswebsite, string path)
public static bool StartItemBatchDownload(string name, string url, string id, int ver, bool iswebsite, string path)
{
bool noErrors = true;
try
{
string version = ((ver != 0) && (!iswebsite)) ? "&version=" + ver : "";
@ -154,6 +163,7 @@ public partial class AssetSDK : Form
catch (Exception ex)
{
GlobalFuncs.LogExceptions(ex);
noErrors = false;
}
}
else
@ -164,8 +174,12 @@ public partial class AssetSDK : Form
catch (Exception ex)
{
GlobalFuncs.LogExceptions(ex);
noErrors = false;
}
return noErrors;
}
private void AssetDownloader_URLSelection_SelectedIndexChanged(object sender, EventArgs e)
{
switch (AssetDownloader_URLSelection.SelectedIndex)
@ -222,20 +236,29 @@ public partial class AssetSDK : Form
string[] lines = AssetDownloaderBatch_BatchIDBox.Lines;
int lineCount = lines.Count();
foreach (var line in lines)
{
string[] linesplit = line.Split('|');
StartItemBatchDownload(
bool noErrors = StartItemBatchDownload(
linesplit[0] + extension,
url,
linesplit[1],
Convert.ToInt32(linesplit[2]),
isWebSite, basepath);
if (!noErrors)
{
--lineCount;
}
}
AssetDownloaderBatch_Status.Visible = false;
MessageBox.Show("Batch download complete! " + lines.Count() + " items downloaded!", "Novetus Asset SDK - Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
string extraText = (lines.Count() != lineCount) ? "\n" + (lines.Count() - lineCount) + " errors were detected during the download. Make sure your IDs and links are valid." : "";
MessageBox.Show("Batch download complete! " + lineCount + " items downloaded!" + extraText, "Novetus Asset SDK - Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}