Skip to content

Commit

Permalink
Now using ContentDisposition class to get file name from downloaded f…
Browse files Browse the repository at this point in the history
…ile.
  • Loading branch information
ravibpatel committed Jul 18, 2019
1 parent 027e677 commit 496c3d4
Showing 1 changed file with 8 additions and 34 deletions.
42 changes: 8 additions & 34 deletions AutoUpdater.NET/DownloadUpdateDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 496c3d4

Please sign in to comment.