public override async Task <DirectoryInfo> DownloadAsync(DownloadableVersionModel version, string savePath, IProgress <int> progress, CancellationToken cancellationToken)
        {
            _logger.LogDebug("{0}: Downloading {1} to {2}", nameof(ApiDonwloadService), version.DownloadPath, savePath);

            int bytesRead = 0;

            byte[] buffer        = new byte[1024];
            var    contentLength = await GetContentLength(version.DownloadPath);

            if (cancellationToken.IsCancellationRequested)
            {
                CleanUpDownload(savePath);
            }

            var progressValue = 0;

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(version.DownloadPath);
                HttpResponseMessage response = await httpClient.GetAsync(version.DownloadPath);

                if (response.IsSuccessStatusCode)
                {
                    var responseStream = await response.Content.ReadAsStreamAsync();

                    using (FileStream fs = new FileStream(savePath, FileMode.CreateNew))
                    {
                        while (!cancellationToken.IsCancellationRequested)
                        {
                            bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);

                            progressValue = (int)(fs.Position / (contentLength * 1m) * 100);
                            if (bytesRead == 0)
                            {
                                progress.Report(100);
                                break;
                            }

                            await fs.WriteAsync(buffer, 0, bytesRead);

                            progress.Report(progressValue);
                        }
                    }
                }
                else
                {
                    throw new FileNotFoundException(version.DownloadPath);
                }
            }

            if (cancellationToken.IsCancellationRequested)
            {
                CleanUpDownload(savePath);
            }

            return(new DirectoryInfo(savePath));
        }
Example #2
0
        public virtual async Task <DownloadableVersionModel> GetLatestVersionModel()
        {
            _logger.LogDebug("{0}: Getting latest version model", nameof(BaseDownloadService));

            var versions = await GetAllAvailableVersions();

            DownloadableVersionModel latestVersion = null;

            foreach (var version in versions)
            {
                if (latestVersion == null || version.Version.CompareTo(latestVersion.Version) > 0)
                {
                    latestVersion = version;
                }
            }

            return(latestVersion);
        }
Example #3
0
 public abstract Task <DirectoryInfo> DownloadAsync(DownloadableVersionModel version, string savePath, IProgress <int> progress, CancellationToken cancellationToken);
        public override async Task <DirectoryInfo> DownloadAsync(DownloadableVersionModel version, string savePath, IProgress <int> progress, CancellationToken cancellationToken)
        {
            _logger.LogDebug("{0}: Downloading {1} to {2} [{progress}]", nameof(FtpDownloadService), version.DownloadPath, savePath, progress);

            int bytesRead = 0;

            byte[] buffer       = new byte[1024];
            string downloadPath = $"{_ftpConfig.Server}{version.FileName}";

            var contentLength = await GetContentLength(downloadPath);

            if (cancellationToken.IsCancellationRequested)
            {
                return(new DirectoryInfo(savePath));
            }

            var progressValue = 0;

            var request = (FtpWebRequest)WebRequest.Create(downloadPath);

            request.Method     = WebRequestMethods.Ftp.DownloadFile;
            request.UsePassive = true;
            request.UseBinary  = true;
            request.KeepAlive  = true;

            request.Credentials = new NetworkCredential(_ftpConfig.Username, _ftpConfig.Password);

            var response = (FtpWebResponse)request.GetResponse();

            try
            {
                Stream responseStream = response.GetResponseStream();
                using (FileStream fs = new FileStream(savePath, FileMode.CreateNew))
                {
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);

                        progressValue = (int)(fs.Position / (contentLength * 1m) * 100);
                        if (bytesRead == 0)
                        {
                            progress.Report(100);
                            break;
                        }

                        await fs.WriteAsync(buffer, 0, bytesRead);

                        progress.Report(progressValue);
                    }
                }

                responseStream.Close();

                if (cancellationToken.IsCancellationRequested)
                {
                    CleanUpDownload(savePath);
                }
            }
            catch (IOException ex)
            {
                _logger.LogError(ex, $"Downloading {downloadPath} failed.");
            }

            return(new DirectoryInfo(savePath));
        }