Skip to content

Commit

Permalink
Bugfix download not cancelled after requested
Browse files Browse the repository at this point in the history
  • Loading branch information
fahminlb33 committed Jan 29, 2022
1 parent 6241617 commit 5719b17
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/KFlearning.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private static void RegisterServices()

// register clients
services.AddTransient<WebClient>();
services.AddTransient<ManualResetEventSlim>();

// setup logging
services.AddLogging(configure =>
Expand Down
58 changes: 35 additions & 23 deletions src/KFlearning.App/Services/FlutterInstallService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using KFlearning.Core.API;
Expand All @@ -27,7 +27,6 @@ public interface IFlutterInstallService

public class FlutterInstallService : IFlutterInstallService
{
private readonly WebClient _webClient;
private readonly IFlutterGitClient _flutterGitClient;
private readonly ILogger<FlutterInstallService> _logger;

Expand All @@ -42,14 +41,12 @@ public class FlutterInstallService : IFlutterInstallService
public string FlutterVersion { get; private set; } = string.Empty;
public string InstallPath { get; set; }

public FlutterInstallService(IFlutterGitClient flutterGitClient, WebClient webClient, IPathManager pathManager, ILogger<FlutterInstallService> logger)
public FlutterInstallService(IFlutterGitClient flutterGitClient, IPathManager pathManager, ILogger<FlutterInstallService> logger)
{
InstallPath = pathManager.GetPath(PathKind.FlutterInstallRoot);

_flutterGitClient = flutterGitClient;
_webClient = webClient;
_logger = logger;
_webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
}

#region Public Methods
Expand All @@ -64,7 +61,6 @@ public void Install()

public void Cancel()
{
_webClient.CancelAsync();
_cancellationSource?.Cancel();
}

Expand Down Expand Up @@ -113,12 +109,41 @@ private void ExecuteSteps()

_downloadPath = Path.GetTempFileName();
var uri = _flutterGitClient.GetFlutterDownloadUri(FlutterVersion);
_logger.LogDebug("Flutter download URI {0}", uri);
_logger.LogDebug("Flutter download path {0}", _downloadPath);
_logger.LogDebug("Flutter URI path {0}", uri);

_logger.LogInformation("Start Flutter download");
_cancellationToken.Register(() => _webClient.CancelAsync());
await _webClient.DownloadFileTaskAsync(new Uri(uri), _downloadPath);

using var httpClient = new HttpClient();
using var response = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, _cancellationToken);
using var downloadStream = await response.Content.ReadAsStreamAsync(_cancellationToken);
using var fileWriterStream = new FileStream(_downloadPath, FileMode.Create);

var totalSize = response.Content.Headers.ContentLength ?? 1;
var totalDownloaded = 0D;
var buffer = new byte[8192];
var readed = 0;

do
{
readed = await downloadStream.ReadAsync(buffer, _cancellationToken);
if (readed == 0)
{
break;
}

await fileWriterStream.WriteAsync(buffer.AsMemory(0, readed), _cancellationToken);
totalDownloaded += readed;

var progressPercentage = Convert.ToInt32(totalDownloaded / totalSize * 100.0);

_logger.LogDebug("Download progress {0}/{1} ({2}%)", totalDownloaded, totalSize, progressPercentage);
OnProgressChanged(this, new FlutterInstallProgressEventArgs
{
ProgressPercentage = progressPercentage,
Status = "Mengunduh paket instalasi..."
});
} while (readed > 0);

// step 2 --- extract to installfolder
OnProgressChanged(this, new FlutterInstallProgressEventArgs
Expand All @@ -129,7 +154,7 @@ private void ExecuteSteps()

_logger.LogInformation("Start Flutter extraction");
_cancellationSource?.Token.ThrowIfCancellationRequested();

ZipFile.ExtractToDirectory(_downloadPath, InstallPath);

// step 3 --- set environment variable
Expand Down Expand Up @@ -181,19 +206,6 @@ private void ExecuteSteps()

#endregion

#region Event Handlers

private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
OnProgressChanged(this, new FlutterInstallProgressEventArgs
{
ProgressPercentage = e.ProgressPercentage,
Status = "Mengunduh..."
});
}

#endregion

#region Event Invocators

private void OnInstallReady(object sender, FlutterInstallReadyEventArgs e)
Expand Down

0 comments on commit 5719b17

Please sign in to comment.