|
22 | 22 | using System.IO;
|
23 | 23 | using System.Linq;
|
24 | 24 | using System.Net;
|
| 25 | +using System.Net.Http.Headers; |
25 | 26 | using System.Reflection;
|
26 |
| -using System.Text.RegularExpressions; |
27 | 27 | using System.Windows.Forms;
|
28 | 28 | using Ionic.Zip;
|
29 | 29 | using JetBrains.Annotations;
|
@@ -387,12 +387,30 @@ public string GetToolZipFile(ILongWaitBroker waitBroker, string packageIdentifie
|
387 | 387 | Query = @"lsid=" + Uri.EscapeDataString(packageIdentifier)
|
388 | 388 | };
|
389 | 389 | 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 | + } |
396 | 414 | File.WriteAllBytes(downloadedFile, toolZip);
|
397 | 415 | return downloadedFile;
|
398 | 416 | }
|
|
0 commit comments