Skip to content

Commit

Permalink
Fix Tool Store download. (#2854)
Browse files Browse the repository at this point in the history
Use "ContentDispositionHeaderValue" to parse the content-disposition header value.
  • Loading branch information
nickshulman authored Feb 2, 2024
1 parent 7a308cd commit 1f52dda
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions pwiz_tools/Skyline/ToolsUI/ToolStoreDlg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Ionic.Zip;
using JetBrains.Annotations;
Expand Down Expand Up @@ -387,12 +387,30 @@ public string GetToolZipFile(ILongWaitBroker waitBroker, string packageIdentifie
Query = @"lsid=" + Uri.EscapeDataString(packageIdentifier)
};
byte[] toolZip = webClient.DownloadData(uri.Uri.AbsoluteUri);
string contentDisposition = webClient.ResponseHeaders.Get(@"Content-Disposition");
// contentDisposition is filename="ToolBasename.zip"
// ReSharper disable LocalizableElement
Match match = Regex.Match(contentDisposition, "^filename=\"(.+)\"$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
// ReSharper restore LocalizableElement
string downloadedFile = directory + match.Groups[1].Value;
string contentDispositionString = webClient.ResponseHeaders.Get(@"Content-Disposition");
string downloadedFile = null;
ContentDispositionHeaderValue.TryParse(contentDispositionString, out var contentDispositionHeaderValue);
try
{
if (contentDispositionHeaderValue?.FileNameStar != null)
{
downloadedFile = Path.Combine(directory, contentDispositionHeaderValue.FileNameStar);
}
else if (contentDispositionHeaderValue?.FileName != null)
{
downloadedFile = Path.Combine(directory, contentDispositionHeaderValue.FileName);
}
}
catch (Exception)
{
// ignore
}

if (downloadedFile == null)
{
// Something went wrong trying to decide the filename. Fall back to using a temp file name.
downloadedFile = FileStreamManager.Default.GetTempFileName(directory, @"Sky");
}
File.WriteAllBytes(downloadedFile, toolZip);
return downloadedFile;
}
Expand Down

0 comments on commit 1f52dda

Please sign in to comment.