Skip to content

Commit

Permalink
Fix null/empty filename string when downloading package file (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryfu-msft authored Aug 4, 2022
1 parent 99cc214 commit 3ea6614
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/WingetCreateCore/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public static string ToEnumAttributeValue(this Enum enumVal)
var attributes = memInfo[0].GetCustomAttributes(typeof(EnumMemberAttribute), false);
EnumMemberAttribute attributeValue = (attributes.Length > 0) ? (EnumMemberAttribute)attributes[0] : null;
return attributeValue?.Value ?? enumVal.ToString();
}

/// <summary>
/// Returns null if the target string is empty.
/// </summary>
/// <param name="s">Target string value.</param>
/// <returns>Null if the string is empty; otherwise the original string value.</returns>
public static string NullIfEmpty(this string s)
{
return string.IsNullOrEmpty(s) ? null : s;
}

/// <summary>
Expand Down
12 changes: 7 additions & 5 deletions src/WingetCreateCore/Common/PackageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ public static async Task<string> DownloadFileAsync(string url, long? maxDownload
}

string urlFile = Path.GetFileName(url.Split('?').Last());
string contentDispositionFile = response.Content.Headers.ContentDisposition?.FileName?.Trim('"');
string contentDispositionFile = response.Content.Headers.ContentDisposition?.FileName?.Trim('"');
string requestUrlFileName = Path.GetFileName(response.RequestMessage?.RequestUri?.ToString());

if (!Directory.Exists(InstallerDownloadPath))
{
Directory.CreateDirectory(InstallerDownloadPath);
}

string targetFile = GetNumericFilename(Path.Combine(InstallerDownloadPath, contentDispositionFile ?? urlFile));

}

// If no relevant filename can be obtained for the installer download, use a temporary filename as last option.
string targetFileName = contentDispositionFile.NullIfEmpty() ?? urlFile.NullIfEmpty() ?? requestUrlFileName.NullIfEmpty() ?? Path.GetTempFileName();
string targetFile = GetNumericFilename(Path.Combine(InstallerDownloadPath, targetFileName));
using var targetFileStream = File.OpenWrite(targetFile);
var contentStream = await response.Content.ReadAsStreamAsync();

Expand Down

0 comments on commit 3ea6614

Please sign in to comment.