Exemplo n.º 1
0
        /// <summary>
        /// Returns download information for the installation archive asset corresponding to
        /// the current platform contained within <paramref name="release"/>.
        /// </summary>
        /// <param name="release">The Github release to return download information for.</param>
        public static DownloadInfo GetDownloadInfoForAsset(GithubRelease release)
        {
            string assetName = "";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                assetName = "win-x64.zip";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                assetName = "linux-x64.zip";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                assetName = "osx-x64.zip";
            }
            else
            {
                return(null);
            }

            var asset = release.assets.Where(x => x.name.Contains(assetName))?.First();

            if (asset == null)
            {
                throw new Exception($"Github asset \"{assetName}\" not available!");
            }

            return(new DownloadInfo
            {
                DownloadSize = Math.Round((asset.size / 1024M) / 1024M, 2),
                DownloadUrl = asset.browser_download_url
            });
        }
Exemplo n.º 2
0
#pragma warning disable 1998
        /// <summary>
        /// Downloads the zip archive of the latest release to a local file and
        /// returns the full path to that file.
        /// </summary>
        /// <param name="release">The release to download the installation archive for.</param>
        /// <param name="progress">An optional progress object to receive download progress notifications.</param>
        /// <returns>Returns the full file path of the downloaded file.</returns>
        public async static Task <string> DownloadRelease(GithubRelease release, IProgress <KeyValuePair <int, string> > progress = null)
        {
            return(await Task.Run(async() =>
            {
                AutoResetEvent downloadComplete = new AutoResetEvent(false);
                bool success = false;

                var wc = new WebClient
                {
                    CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore)
                };

                AddHeaders(wc);

                if (progress != null)
                {
                    wc.DownloadProgressChanged += ((s, e) =>
                    {
                        decimal doneMB = Math.Round(e.BytesReceived / 1024M / 1024M, 2);
                        decimal totalMB = Math.Round(e.TotalBytesToReceive / 1024M / 1024M, 2);

                        string msg = _loc.GetLocalizationValue("InstallStatusDownloading") +
                                     $" {doneMB}/{totalMB} MB";

                        progress.Report(new KeyValuePair <int, string>(e.ProgressPercentage, msg));
                    });
                }

                wc.DownloadFileCompleted += ((s, e) =>
                {
                    success = (!e.Cancelled && e.Error == null);
                    downloadComplete.Set();
                });

                var downloadLink = CommonUtils.GetDownloadLinkByPlatform(release);
                var fileName = Path.GetFileName(downloadLink);
                var tempFilePath = Path.GetTempFileName();

                try
                {
                    wc.DownloadFileAsync(new Uri(downloadLink), tempFilePath);
                    downloadComplete.WaitOne();
                    return success ? tempFilePath : null;
                }
                catch (Exception ex)
                {
                    Log.Error($"Error downloading \"{downloadLink}\":\r\n{ex}");
                    return null;
                }
            }));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the download link for the installation archive of the given <paramref name="release"/>.
        /// </summary>
        /// <param name="release">The release for which the download link should be returned.</param>
        public static string GetDownloadLinkByPlatform(GithubRelease release)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return(release.assets.Where(x => x.name.Contains("win-x64.zip")).First().browser_download_url);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                return(release.assets.Where(x => x.name.Contains("osx-x64.zip")).First().browser_download_url);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return(release.assets.Where(x => x.name.Contains("linux-x64.zip")).First().browser_download_url);
            }

            return("");
        }