Skip to content

Commit

Permalink
Fix Downloader empty Content-Length
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Jan 5, 2021
1 parent ae1833c commit eb09e18
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 75 deletions.
2 changes: 1 addition & 1 deletion HandyWinGet/HandyWinGet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Downloader" Version="2.0.1" />
<PackageReference Include="Downloader" Version="2.0.4" />
<PackageReference Include="HandyControls" Version="3.0.0" />
<PackageReference Include="ModernWpfUis" Version="1.0.0" />
<PackageReference Include="Prism.DryIoc" Version="8.0.0.1909" />
Expand Down
124 changes: 50 additions & 74 deletions HandyWinGet/ViewModels/PackagesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,17 @@ private void GetPackages(bool isRefresh = false)
if (!Directory.Exists(_path + @"\manifests") || isRefresh)
{
var manifestUrl = "https://github.com/microsoft/winget-pkgs/archive/master.zip";
//Todo: A Fix Need Here
//DownloaderService = new DownloadService();
//DownloaderService.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e)
//{
// OnDownloadProgressChanged(sender, e, DownloadMode.Repository);
//};
//DownloaderService.DownloadFileCompleted += delegate (object sender, AsyncCompletedEventArgs e)
//{
// OnDownloadFileCompleted(sender, e, DownloadMode.Repository);
//};
//await DownloaderService.DownloadFileAsync(manifestUrl, new DirectoryInfo(_path));

WebClient client = new WebClient();
client.DownloadFileCompleted += Client_DownloadFileCompleted;
client.DownloadProgressChanged += Client_DownloadProgressChanged;
await client.DownloadFileTaskAsync(new Uri(manifestUrl), Path.Combine(_path, "master.zip"));

DownloaderService = new DownloadService();
DownloaderService.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e)
{
OnDownloadProgressChanged(sender, e, DownloadMode.Repository);
};
DownloaderService.DownloadFileCompleted += delegate (object sender, AsyncCompletedEventArgs e)
{
OnDownloadFileCompleted(sender, e, DownloadMode.Repository);
};
await DownloaderService.DownloadFileAsync(manifestUrl, new DirectoryInfo(_path));
}

LoadingStatus = "Extracting packages...";
Expand Down Expand Up @@ -335,50 +330,6 @@ private void GetPackages(bool isRefresh = false)
}
}

private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
if (e.TotalBytesToReceive == -1)
{
if (!IsIndeterminate)
{
IsIndeterminate = true;
}

LoadingStatus = "Downloading...";
}
else
{
if (IsIndeterminate)
{
IsIndeterminate = false;
}

Progress = e.ProgressPercentage;
LoadingStatus =
$"Downloading {Tools.ConvertBytesToMegabytes(e.BytesReceived)} MB of {Tools.ConvertBytesToMegabytes(e.TotalBytesToReceive)} MB - {e.ProgressPercentage}%";
}
}

private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
UpdatedDate = DateTime.Now.ToString();
GlobalDataHelper<AppConfig>.Config.UpdatedDate = DateTime.Now;
GlobalDataHelper<AppConfig>.Save();
IsIndeterminate = true;
LoadingStatus = "Extracting Manifests...";
ZipFile.ExtractToDirectory(Path.Combine(_path, "master.zip"), Path.Combine(_path), true);
LoadingStatus = "Cleaning Directory...";
CleanDirectory();
Progress = 0;
IsIndeterminate = false;
}
catch (InvalidDataException)
{
}
}

private async void OnButtonAction(string param)
{
string text = $"winget install {_selectedPackage.Id} -v {_selectedPackage.Version}";
Expand Down Expand Up @@ -687,9 +638,7 @@ public async void InstallInternalMode()
private void CleanDirectory()
{
var rootDir = new DirectoryInfo(_path + @"\winget-pkgs-master");
//Todo: A Fix Need Here
var zipFile = new FileInfo(_path + @"\master.zip");
//var zipFile = new FileInfo(DownloaderService.Package.FileName);
var zipFile = new FileInfo(DownloaderService.Package.FileName);
var pkgDir = new DirectoryInfo(_path + @"\manifests");
var moveDir = new DirectoryInfo(_path + @"\winget-pkgs-master\manifests");
if (moveDir.Exists)
Expand Down Expand Up @@ -728,16 +677,25 @@ private bool FilterCombo(VersionModel item)

private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e, DownloadMode mode)
{
//Todo: A Fix Need Here
switch (mode)
{
case DownloadMode.Repository:
// UpdatedDate = DateTime.Now.ToString();
// GlobalDataHelper<AppConfig>.Config.UpdatedDate = DateTime.Now;
// GlobalDataHelper<AppConfig>.Save();

//await Task.Delay(1000);
//ZipFile.ExtractToDirectory(DownloaderService.Package.FileName, _path, true);
try
{
UpdatedDate = DateTime.Now.ToString();
GlobalDataHelper<AppConfig>.Config.UpdatedDate = DateTime.Now;
GlobalDataHelper<AppConfig>.Save();
IsIndeterminate = true;
LoadingStatus = "Extracting Manifests...";
ZipFile.ExtractToDirectory(DownloaderService.Package.FileName, _path, true);
LoadingStatus = "Cleaning Directory...";
CleanDirectory();
Progress = 0;
IsIndeterminate = false;
}
catch (InvalidDataException)
{
}
CleanDirectory();
break;
case DownloadMode.Package:
Expand All @@ -752,17 +710,35 @@ private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e, D

private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e, DownloadMode mode)
{
//Todo: A Fix Need Here
Progress = (int)e.ProgressPercentage;
switch (mode)
{
case DownloadMode.Repository:
LoadingStatus =
$"Downloading {Tools.ConvertBytesToMegabytes(e.BytesReceived)} MB from {Tools.ConvertBytesToMegabytes(e.TotalBytesToReceive)} MB - {(int)e.ProgressPercentage}%";

if (e.TotalBytesToReceive == -1)
{
if (!IsIndeterminate)
{
IsIndeterminate = true;
}

LoadingStatus = "Downloading...";
}
else
{
if (IsIndeterminate)
{
IsIndeterminate = false;
}

Progress = Progress;
LoadingStatus =
$"Downloading {Tools.ConvertBytesToMegabytes(e.BytesReceived)} MB of {Tools.ConvertBytesToMegabytes(e.TotalBytesToReceive)} MB - {Progress}%";
}
break;
case DownloadMode.Package:
LoadingStatus =
$"Downloading {_selectedPackage.Id}-{_selectedPackage.Version} - {Tools.ConvertBytesToMegabytes(e.BytesReceived)} MB from {Tools.ConvertBytesToMegabytes(e.TotalBytesToReceive)} MB - {(int)e.ProgressPercentage}%";
$"Downloading {_selectedPackage.Id}-{_selectedPackage.Version} - {Tools.ConvertBytesToMegabytes(e.BytesReceived)} MB of {Tools.ConvertBytesToMegabytes(e.TotalBytesToReceive)} MB - {Progress}%";
break;
}
}
Expand Down

0 comments on commit eb09e18

Please sign in to comment.