Skip to content

Commit

Permalink
Post download verification for wistia and generic downloads. Generic …
Browse files Browse the repository at this point in the history
…downloads now send a head request to get the content length so a duplicates filesize can be compared and the download verified
  • Loading branch information
loc0 committed Jun 15, 2019
1 parent 399c03c commit a56e44e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
64 changes: 51 additions & 13 deletions codeWithMoshDownloader/codeWithMoshDownloader/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static codeWithMoshDownloader.Helpers;
Expand Down Expand Up @@ -197,17 +198,21 @@ private async Task<bool> DownloadFile(GenericFile downloadInfo, string sectionPa

Console.WriteLine($"[download] Downloading {downloadInfo.FileName}");

HttpResponseMessage fileHeadRequest = await SimpleHead(downloadInfo.Url);

var downloadSize = fileHeadRequest.Content.Headers.ContentLength;

string filePath = Path.Combine(sectionPath, downloadInfo.FileName);

if (File.Exists(filePath) && !_force)
if (FileExists(filePath, downloadSize))
{

Console.WriteLine("[download] File already exists");
return true;
}

bool result = await DownloadClient(downloadInfo.Url, filePath);

result = VerifyDownload(result, filePath, downloadSize);

if (result && _unZip && Path.GetExtension(downloadInfo.FileName) == ".zip")
{
UnZipArchive(sectionPath, filePath);
Expand All @@ -216,6 +221,42 @@ private async Task<bool> DownloadFile(GenericFile downloadInfo, string sectionPa
return result;
}

private static bool VerifyDownload(bool result, string filePath, long? downloadSize)
{
if (result)
{
long fileSize = new FileInfo(filePath).Length;

if (downloadSize != fileSize)
{
Console.WriteLine("[download] Download failed\n");
result = false;
}
else
{
Console.Write("[download] Download complete\n");
}
}

return result;
}

private bool FileExists(string filePath, long? downloadSize)
{
if (File.Exists(filePath) && !_force)
{
long fileSize = new FileInfo(filePath).Length;

if (downloadSize == fileSize)
{
Console.WriteLine("[download] File already exists");
return true;
}
}

return false;
}

private void UnZipArchive(string sectionPath, string filePath)
{
Console.WriteLine("[download] Unzipping archive");
Expand Down Expand Up @@ -282,20 +323,18 @@ private async Task<bool> DownloadFile(WistiaDownloadInfo wistiaDownloadInfo, str
wistiaDownloadInfo.FileName = AddIndex(wistiaDownloadInfo.FileName, _currentItemIndex).GetSafeFilename();
string filePath = Path.Combine(sectionPath, wistiaDownloadInfo.FileName);

if (File.Exists(filePath) && !_force)
if (FileExists(filePath, wistiaDownloadInfo.FileSize))
{
long fileSize = new FileInfo(filePath).Length;

if (fileSize == wistiaDownloadInfo.FileSize)
{
Console.WriteLine("[download] File already exists");
return true;
}
return true;
}

Console.WriteLine($"[download] Downloading {wistiaDownloadInfo.FileName}");

return await DownloadClient(wistiaDownloadInfo.Url, filePath);
bool result = await DownloadClient(wistiaDownloadInfo.Url, filePath);

result = VerifyDownload(result, filePath, wistiaDownloadInfo.FileSize);

return result;
}

private async Task<bool> DownloadClient(string url, string filepath)
Expand All @@ -316,7 +355,6 @@ private async Task<bool> DownloadClient(string url, string filepath)
else if (!args.Cancelled)
{
result = true;
Console.Write("[download] Download complete\n");
}
else
{
Expand Down
11 changes: 11 additions & 0 deletions codeWithMoshDownloader/codeWithMoshDownloader/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public static async Task<string> SimpleGet(string url)
}
}

public static async Task<HttpResponseMessage> SimpleHead(string url)
{
using (var request = new HttpRequestMessage(HttpMethod.Head, url))
{
using (HttpResponseMessage response = await HttpClient.SendAsync(request))
{
return response;
}
}
}

public static string GetSafeFilename(this string filename)
{
return string.Join("_", filename.Split(Path.GetInvalidFileNameChars()));
Expand Down

0 comments on commit a56e44e

Please sign in to comment.