src/WingetCreateCore/Common/PackageParser.cs
/// <summary>
/// Download file at specified URL to temp directory, unless it's already present.
/// </summary>
/// <param name="url">The URL of the file to be downloaded.</param>
/// <param name="maxDownloadSize">The maximum file size in bytes to download.</param>
/// <returns>Path of downloaded, or previously downloaded, file.</returns>
Download file at specified URL to temp directory, unless it's already present.
Download file at specified URL to temp directory unless it ' s already present .
public static async Task<string> DownloadFileAsync(string url, long? maxDownloadSize = null)
        {
            var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
            int redirectCount = 0;
            while (response.StatusCode == System.Net.HttpStatusCode.Redirect && redirectCount < 2)
            {
                var redirectUri = response.Headers.Location;
                response = await httpClient.GetAsync(redirectUri, HttpCompletionOption.ResponseHeadersRead);
                redirectCount++;
            }
            if (!response.IsSuccessStatusCode)
            {
                string message = await response.Content.ReadAsStringAsync();
                throw new HttpRequestException(message, null, response.StatusCode);
            }
            string urlFile = Path.GetFileName(url.Split('?').Last());
            string contentDispositionFile = response.Content.Headers.ContentDisposition?.FileName?.Trim('"');
            if (!Directory.Exists(InstallerDownloadPath))
            {
                Directory.CreateDirectory(InstallerDownloadPath);
            }
            string targetFile = Path.Combine(InstallerDownloadPath, contentDispositionFile ?? urlFile);
            long? downloadSize = response.Content.Headers.ContentLength;
            if (downloadSize > maxDownloadSize)
            {
                throw new DownloadSizeExceededException(maxDownloadSize.Value);
            }
            if (!File.Exists(targetFile) || new FileInfo(targetFile).Length != downloadSize)
            {
                File.Delete(targetFile);
                using var targetFileStream = File.OpenWrite(targetFile);
                var contentStream = await response.Content.ReadAsStreamAsync();
                await contentStream.CopyToAsync(targetFileStream);
            }
            return targetFile;
        }
public static async Task < string > DownloadFileAsync ( string url , long ? maxDownloadSize = null ) { var response = await httpClient . GetAsync ( url , HttpCompletionOption . ResponseHeadersRead ) ; int redirectCount = 0 ; while ( response . StatusCode == System . Net . HttpStatusCode . Redirect && redirectCount < 2 ) { var redirectUri = response . Headers . Location ; response = await httpClient . GetAsync ( redirectUri , HttpCompletionOption . ResponseHeadersRead ) ; redirectCount ++ ; } if ( ! response . IsSuccessStatusCode ) { string message = await response . Content . ReadAsStringAsync ( ) ; throw new HttpRequestException ( message , null , response . StatusCode ) ; } string urlFile = Path . GetFileName ( url . Split ( ' ' ) . Last ( ) ) ; string contentDispositionFile = response . Content . Headers . ContentDisposition ? . FileName ? . Trim ( ' ' ) ; if ( ! Directory . Exists ( InstallerDownloadPath ) ) { Directory . CreateDirectory ( InstallerDownloadPath ) ; } string targetFile = Path . Combine ( InstallerDownloadPath , contentDispositionFile ?? urlFile ) ; long ? downloadSize = response . Content . Headers . ContentLength ; if ( downloadSize > maxDownloadSize ) { throw new DownloadSizeExceededException ( maxDownloadSize . Value ) ; } if ( ! File . Exists ( targetFile ) || new FileInfo ( targetFile ) . Length != downloadSize ) { File . Delete ( targetFile ) ; using var targetFileStream = File . OpenWrite ( targetFile ) ; var contentStream = await response . Content . ReadAsStreamAsync ( ) ; await contentStream . CopyToAsync ( targetFileStream ) ; } return targetFile ; }
Download file at specified URL to temp directory, unless it's already present.
Download file at specified URL to temp directory unless it ' s already present .
Path of downloaded, or previously downloaded, file.
Path of downloaded or previously downloaded file .
url
string
The URL of the file to be downloaded.
The URL of the file to be downloaded .
maxDownloadSize
long?
The maximum file size in bytes to download.
The maximum file size in bytes to download .