From 496c3d41db9216cefd7b7714312293ccf11798b2 Mon Sep 17 00:00:00 2001 From: Ravi Patel Date: Thu, 18 Jul 2019 12:07:43 +0530 Subject: [PATCH] Now using ContentDisposition class to get file name from downloaded file. --- AutoUpdater.NET/DownloadUpdateDialog.cs | 42 +++++-------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/AutoUpdater.NET/DownloadUpdateDialog.cs b/AutoUpdater.NET/DownloadUpdateDialog.cs index af745465..c1641d66 100644 --- a/AutoUpdater.NET/DownloadUpdateDialog.cs +++ b/AutoUpdater.NET/DownloadUpdateDialog.cs @@ -5,6 +5,7 @@ using System.IO; using System.Net; using System.Net.Cache; +using System.Net.Mime; using System.Security.Cryptography; using System.Text; using System.Windows.Forms; @@ -128,21 +129,16 @@ private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEvent } } - string fileName; - string contentDisposition = _webClient.ResponseHeaders["Content-Disposition"] ?? string.Empty; - if (string.IsNullOrEmpty(contentDisposition)) + ContentDisposition contentDisposition = null; + if (_webClient.ResponseHeaders["Content-Disposition"] != null) { - fileName = Path.GetFileName(_webClient.ResponseUri.LocalPath); - } - else - { - fileName = TryToFindFileName(contentDisposition, "filename="); - if (string.IsNullOrEmpty(fileName)) - { - fileName = TryToFindFileName(contentDisposition, "filename*=UTF-8''"); - } + contentDisposition = new ContentDisposition(_webClient.ResponseHeaders["Content-Disposition"]); } + var fileName = string.IsNullOrEmpty(contentDisposition?.FileName) + ? Path.GetFileName(_webClient.ResponseUri.LocalPath) + : contentDisposition.FileName; + var tempPath = Path.Combine( string.IsNullOrEmpty(AutoUpdater.DownloadPath) ? Path.GetTempPath() : AutoUpdater.DownloadPath, @@ -256,28 +252,6 @@ private static String BytesToString(long byteCount) return $"{(Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture)} {suf[place]}"; } - private static string TryToFindFileName(string contentDisposition, string lookForFileName) - { - string fileName = String.Empty; - if (!string.IsNullOrEmpty(contentDisposition)) - { - var index = contentDisposition.IndexOf(lookForFileName, StringComparison.CurrentCultureIgnoreCase); - if (index >= 0) - fileName = contentDisposition.Substring(index + lookForFileName.Length); - if (fileName.StartsWith("\"")) - { - var file = fileName.Substring(1, fileName.Length - 1); - var i = file.IndexOf("\"", StringComparison.CurrentCultureIgnoreCase); - if (i != -1) - { - fileName = file.Substring(0, i); - } - } - } - - return fileName; - } - private static bool CompareChecksum(string fileName, string checksum) { using (var hashAlgorithm = HashAlgorithm.Create(AutoUpdater.HashingAlgorithm))