Skip to content

Commit

Permalink
added validateData method to program.cs of sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Dec 23, 2023
1 parent 18f67ed commit 2084c85
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Samples/Downloader.Sample/DownloadList.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
},
{
"FolderPath": "D:\\TestDownload",
"Url": "http://localhost:3333/dummyfile/file/LocalFile100MB_WithContentDisposition.dat/size/104857600"
"Url": "http://localhost:3333/dummyfile/file/LocalFile100MB_WithContentDisposition.dat/size/104857600",
"ValidateData": true
},
{
"FolderPath": "D:\\TestDownload",
"Url": "http://localhost:3333/dummyfile/noheader/file/LocalFile100MB_WithoutHeader.dat?size=104857600"
"Url": "http://localhost:3333/dummyfile/noheader/file/LocalFile100MB_WithoutHeader.dat?size=104857600",
"ValidateData": true
},
{
"FolderPath": "D:\\TestDownload",
"Url": "http://localhost:3333/dummyfile/file/LocalFile100MB.dat?size=104857600"
"Url": "http://localhost:3333/dummyfile/file/LocalFile100MB.dat?size=104857600",
"ValidateData": true
}
]
24 changes: 24 additions & 0 deletions src/Samples/Downloader.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,33 @@ private static async Task<IDownloadService> DownloadFile(DownloadItem downloadIt
await CurrentDownloadService.DownloadFileTaskAsync(downloadItem.Url, downloadItem.FileName).ConfigureAwait(false);
}

if (downloadItem.ValidateData)
{
var isValid = await ValidateDataAsync(CurrentDownloadService.Package.FileName, CurrentDownloadService.Package.TotalFileSize).ConfigureAwait(false);
if (!isValid)
{
throw new InvalidDataException("Downloaded data is invalid: " + CurrentDownloadService.Package.FileName);
}
}

return CurrentDownloadService;
}

private static async Task<bool> ValidateDataAsync(string filename, long size)
{
await using var stream = File.OpenRead(filename);
for (var i = 0L; i < size; i++)
{
var next = stream.ReadByte();
if (next != i % 256)
{
return false;
}
}

return true;
}

private static void WriteKeyboardGuidLines()
{
Console.Clear();
Expand Down

0 comments on commit 2084c85

Please sign in to comment.