Skip to content

Commit 3ca98d2

Browse files
committed
Update progress
1 parent b4fb82a commit 3ca98d2

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/readme.graph.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,11 @@ directive:
564564
return $;
565565
} else {
566566
let outFileParameterRegex = /(^\s*)public\s*global::System\.String\s*OutFile\s*/gmi
567-
if($.match(outFileParameterRegex)) {
568-
let overrideOnOkCallRegex = /(^\s*)(overrideOnOk\(\s*responseMessage\s*,\s*response\s*,\s*ref\s*_returnNow\s*\);)/gmi
567+
let streamResponseRegex = /global::System\.Threading\.Tasks\.Task<global::System\.IO\.Stream>\s*response/gmi
568+
if($.match(outFileParameterRegex) && $.match(streamResponseRegex)) {
569569
// Handle file download.
570-
$ = $.replace(overrideOnOkCallRegex, '$1$2\n$1using(var stream = await response){ this.WriteToFile(stream, this.GetProviderPath(OutFile, false), _cancellationTokenSource.Token); _returnNow = global::System.Threading.Tasks.Task<bool>.FromResult(true);}\n$1');
570+
let overrideOnOkCallRegex = /(^\s*)(overrideOnOk\(\s*responseMessage\s*,\s*response\s*,\s*ref\s*_returnNow\s*\);)/gmi
571+
$ = $.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');
571572
}
572573
return $;
573574
}
@@ -592,7 +593,7 @@ directive:
592593
593594
// Handle file upload.
594595
let processRecordCallRegex = /(^\s*)(asyncCommandRuntime\.Wait\(\s*ProcessRecordAsync\s*\(\))/gmi
595-
$ = $.replace(processRecordCallRegex, 'if (!MyInvocation.BoundParameters.ContainsKey(nameof(BodyParameter))){BodyParameter = GetFileAsStream() ?? BodyParameter;}\n$1$2');
596+
$ = $.replace(processRecordCallRegex, '$1if (!MyInvocation.BoundParameters.ContainsKey(nameof(BodyParameter))){BodyParameter = GetFileAsStream() ?? BodyParameter;}\n$1$2');
596597
}
597598
return $;
598599
}

tools/Custom/PSCmdletExtensions.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Microsoft.Graph.PowerShell
88
using System.Collections.ObjectModel;
99
using System.IO;
1010
using System.Management.Automation;
11+
using System.Net.Http;
1112
using System.Threading;
1213
using System.Threading.Tasks;
1314

@@ -58,47 +59,63 @@ internal static string GetProviderPath(this PSCmdlet cmdlet, string filePath, bo
5859
/// Saves a stream to a file on disk.
5960
/// </summary>
6061
/// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param>
62+
/// <param name="response">The HTTP response from the service.</param>
6163
/// <param name="inputStream">The stream to write to file.</param>
6264
/// <param name="filePath">The path to write the file to. This should include the file name and extension.</param>
6365
/// <param name="cancellationToken">A cancellation token that will be used to cancel the operation by the user.</param>
64-
internal static void WriteToFile(this PSCmdlet cmdlet, Stream inputStream, string filePath, CancellationToken cancellationToken)
66+
internal static void WriteToFile(this PSCmdlet cmdlet, HttpResponseMessage response, Stream inputStream, string filePath, CancellationToken cancellationToken)
6567
{
6668
using (var fileProvider = ProtectedFileProvider.CreateFileProvider(filePath, FileProtection.ExclusiveWrite, new DiskDataStore()))
6769
{
68-
cmdlet.WriteToStream(inputStream, fileProvider.Stream, cancellationToken);
70+
string downloadUrl = response?.RequestMessage?.RequestUri.ToString();
71+
cmdlet.WriteToStream(inputStream, fileProvider.Stream, downloadUrl, cancellationToken);
6972
}
7073
}
7174

7275
/// <summary>
73-
/// Write an input stream to an output stream.
76+
/// Writes an input stream to an output stream.
7477
/// </summary>
7578
/// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param>
7679
/// <param name="inputStream">The stream to write to an output stream.</param>
7780
/// <param name="outputStream">The stream to write the input stream to.</param>
7881
/// <param name="cancellationToken">A cancellation token that will be used to cancel the operation by the user.</param>
79-
private static void WriteToStream(this PSCmdlet cmdlet, Stream inputStream, Stream outputStream, CancellationToken cancellationToken)
82+
private static void WriteToStream(this PSCmdlet cmdlet, Stream inputStream, Stream outputStream, string downloadUrl, CancellationToken cancellationToken)
8083
{
81-
var copyTask = inputStream.CopyToAsync(outputStream);
82-
var record = new ProgressRecord(000000000, "WriteRequestProgressActivity", "WriteRequestProgressStatus");
84+
Task copyTask = inputStream.CopyToAsync(outputStream);
85+
ProgressRecord record = new ProgressRecord(
86+
activityId: 0,
87+
activity: $"Downloading {downloadUrl ?? "file"}",
88+
statusDescription: $"{outputStream.Position} of {outputStream.Length} bytes downloaded.");
8389
try
8490
{
8591
do
8692
{
87-
record.StatusDescription = string.Format("Number of bytes processed {0}", outputStream.Position);
88-
cmdlet.WriteProgress(record);
93+
cmdlet.WriteProgress(GetProgress(record, outputStream));
8994

9095
Task.Delay(1000, cancellationToken).Wait(cancellationToken);
9196
} while (!copyTask.IsCompleted && !cancellationToken.IsCancellationRequested);
9297

9398
if (copyTask.IsCompleted)
9499
{
95-
record.StatusDescription = string.Format("Number of bytes processed {0}", outputStream.Position);
96-
cmdlet.WriteProgress(record);
100+
cmdlet.WriteProgress(GetProgress(record, outputStream));
97101
}
98102
}
99103
catch (OperationCanceledException)
100104
{
101105
}
102106
}
107+
108+
/// <summary>
109+
/// Calculates and updates the progress record of the provided stream.
110+
/// </summary>
111+
/// <param name="currentProgressRecord">The <see cref="ProgressRecord"/> to update.</param>
112+
/// <param name="stream">The stream to calculate its progress.</param>
113+
/// <returns>An updated <see cref="ProgressRecord"/>.</returns>
114+
private static ProgressRecord GetProgress(ProgressRecord currentProgressRecord, Stream stream)
115+
{
116+
currentProgressRecord.StatusDescription = $"{stream.Position} of {stream.Length} bytes downloaded.";
117+
currentProgressRecord.PercentComplete = (int)Math.Round((double)(100 * stream.Position) / stream.Length);
118+
return currentProgressRecord;
119+
}
103120
}
104121
}

0 commit comments

Comments
 (0)