Skip to content

Commit 1074a66

Browse files
authored
Use overrideOn2Xx when dowloading files. (#1800)
1 parent 5daed74 commit 1074a66

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/readme.graph.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,12 @@ directive:
648648
let outFileParameterRegex = /(^\s*)public\s*global::System\.String\s*OutFile\s*/gmi
649649
let streamResponseRegex = /global::System\.Threading\.Tasks\.Task<global::System\.IO\.Stream>\s*response/gmi
650650
let octetStreamSchemaResponseRegex = /global::System\.Threading\.Tasks\.Task<.*(OctetStreamSchema|GraphReport)>\s*response/gmi
651-
let overrideOnOkCallRegex = /(^\s*)(overrideOnOk\(\s*responseMessage\s*,\s*response\s*,\s*ref\s*_returnNow\s*\);)/gmi
651+
let overrideOn2XxCallRegex = /(^\s*)(overrideOn2Xx\(\s*responseMessage\s*,\s*response\s*,\s*ref\s*_returnNow\s*\);)/gmi
652652
if($.match(outFileParameterRegex) && $.match(streamResponseRegex)) {
653653
// Handle file download.
654-
$ = $.replace(overrideOnOkCallRegex, '$1$2\n$1using(var stream = await response){ this.WriteToFile(responseMessage, stream, this.GetProviderPath(OutFile, false), _cancellationTokenSource.Token); _returnNow = global::System.Threading.Tasks.Task<bool>.FromResult(true);}\n$1');
654+
$ = $.replace(overrideOn2XxCallRegex, '$1$2\n$1using(var stream = await response){ this.WriteToFile(responseMessage, stream, this.GetProviderPath(OutFile, false), _cancellationTokenSource.Token); _returnNow = global::System.Threading.Tasks.Task<bool>.FromResult(true);}\n$1');
655655
} else if ($.match(outFileParameterRegex) && $.match(octetStreamSchemaResponseRegex)){
656-
$ = $.replace(overrideOnOkCallRegex, '$1$2\n$1using(var stream = await responseMessage.Content.ReadAsStreamAsync()){ this.WriteToFile(responseMessage, stream, this.GetProviderPath(OutFile, false), _cancellationTokenSource.Token); _returnNow = global::System.Threading.Tasks.Task<bool>.FromResult(true);}\n$1');
656+
$ = $.replace(overrideOn2XxCallRegex, '$1$2\n$1using(var stream = await responseMessage.Content.ReadAsStreamAsync()){ this.WriteToFile(responseMessage, stream, this.GetProviderPath(OutFile, false), _cancellationTokenSource.Token); _returnNow = global::System.Threading.Tasks.Task<bool>.FromResult(true);}\n$1');
657657
}
658658
return $;
659659
}

tools/Custom/PSCmdletExtensions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace NamespacePrefixPlaceholder.PowerShell
1616

1717
internal static class PSCmdletExtensions
1818
{
19+
private static readonly char[] PathSeparators = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
20+
1921
/// <summary>
2022
/// Gets a resolved or unresolved path from PSPath.
2123
/// </summary>
@@ -121,20 +123,24 @@ private static void WriteToStream(this PSCmdlet cmdlet, Stream inputStream, Stre
121123
private static bool IsPathDirectory(string path)
122124
{
123125
if (path == null) throw new ArgumentNullException("path");
126+
bool isDirectory = false;
124127
path = path.Trim();
125128

126129
if (Directory.Exists(path))
127-
return true;
130+
isDirectory = true;
128131

129132
if (File.Exists(path))
130-
return false;
133+
isDirectory = false;
131134

132135
// If path has a trailing slash then it's a directory.
133-
if (new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }.Any(x => path.EndsWith(x.ToString())))
134-
return true;
136+
if (PathSeparators.Contains(path.Last()))
137+
isDirectory = true;
138+
139+
// If path has an extension then its a file.
140+
if (Path.HasExtension(path))
141+
isDirectory = false;
135142

136-
// If path has an extension then its a file; directory otherwise.
137-
return string.IsNullOrWhiteSpace(Path.GetExtension(path));
143+
return isDirectory;
138144
}
139145

140146
private static string GetFileName(HttpResponseMessage responseMessage)

0 commit comments

Comments
 (0)