Skip to content

Commit 1f52dda

Browse files
authored
Fix Tool Store download. (#2854)
Use "ContentDispositionHeaderValue" to parse the content-disposition header value.
1 parent 7a308cd commit 1f52dda

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

pwiz_tools/Skyline/ToolsUI/ToolStoreDlg.cs

+25-7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
using System.IO;
2323
using System.Linq;
2424
using System.Net;
25+
using System.Net.Http.Headers;
2526
using System.Reflection;
26-
using System.Text.RegularExpressions;
2727
using System.Windows.Forms;
2828
using Ionic.Zip;
2929
using JetBrains.Annotations;
@@ -387,12 +387,30 @@ public string GetToolZipFile(ILongWaitBroker waitBroker, string packageIdentifie
387387
Query = @"lsid=" + Uri.EscapeDataString(packageIdentifier)
388388
};
389389
byte[] toolZip = webClient.DownloadData(uri.Uri.AbsoluteUri);
390-
string contentDisposition = webClient.ResponseHeaders.Get(@"Content-Disposition");
391-
// contentDisposition is filename="ToolBasename.zip"
392-
// ReSharper disable LocalizableElement
393-
Match match = Regex.Match(contentDisposition, "^filename=\"(.+)\"$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
394-
// ReSharper restore LocalizableElement
395-
string downloadedFile = directory + match.Groups[1].Value;
390+
string contentDispositionString = webClient.ResponseHeaders.Get(@"Content-Disposition");
391+
string downloadedFile = null;
392+
ContentDispositionHeaderValue.TryParse(contentDispositionString, out var contentDispositionHeaderValue);
393+
try
394+
{
395+
if (contentDispositionHeaderValue?.FileNameStar != null)
396+
{
397+
downloadedFile = Path.Combine(directory, contentDispositionHeaderValue.FileNameStar);
398+
}
399+
else if (contentDispositionHeaderValue?.FileName != null)
400+
{
401+
downloadedFile = Path.Combine(directory, contentDispositionHeaderValue.FileName);
402+
}
403+
}
404+
catch (Exception)
405+
{
406+
// ignore
407+
}
408+
409+
if (downloadedFile == null)
410+
{
411+
// Something went wrong trying to decide the filename. Fall back to using a temp file name.
412+
downloadedFile = FileStreamManager.Default.GetTempFileName(directory, @"Sky");
413+
}
396414
File.WriteAllBytes(downloadedFile, toolZip);
397415
return downloadedFile;
398416
}

0 commit comments

Comments
 (0)