Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete the progress bar rendering in Invoke-WebRequest when downloading is complete or cancelled #18130

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ internal static void WriteToStream(Stream input, Stream output, PSCmdlet cmdlet,

Task copyTask = input.CopyToAsync(output, cancellationToken);

bool wroteProgress = false;
ProgressRecord record = new(
ActivityId,
WebCmdletStrings.WriteRequestProgressActivity,
Expand All @@ -313,23 +314,30 @@ internal static void WriteToStream(Stream input, Stream output, PSCmdlet cmdlet,
Utils.DisplayHumanReadableFileSize(output.Position),
totalDownloadSize);

if (contentLength != null && contentLength > 0)
if (contentLength > 0)
{
record.PercentComplete = Math.Min((int)(output.Position * 100 / (long)contentLength), 100);
}

cmdlet.WriteProgress(record);
}

if (copyTask.IsCompleted)
{
record.StatusDescription = StringUtil.Format(WebCmdletStrings.WriteRequestComplete, output.Position);
cmdlet.WriteProgress(record);
wroteProgress = true;
}
}
catch (OperationCanceledException)
{
}

if (wroteProgress)
{
// Write out the completion progress record only if we did render the progress.
record.StatusDescription = StringUtil.Format(
copyTask.IsCompleted
? WebCmdletStrings.WriteRequestComplete
: WebCmdletStrings.WriteRequestCancelled,
output.Position);
record.RecordType = ProgressRecordType.Completed;
cmdlet.WriteProgress(record);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
<value>The cmdlet cannot run because the following parameter is missing: Proxy. Provide a valid proxy URI for the Proxy parameter when using the ProxyCredential or ProxyUseDefaultCredentials parameters, then retry.</value>
</data>
<data name="ReadResponseComplete" xml:space="preserve">
<value>Reading web response stream completed. Downloaded: {0}</value>
<value>Reading web response stream completed. Bytes downloaded: {0}</value>
iSazonov marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="ReadResponseProgressActivity" xml:space="preserve">
<value>Reading web response stream</value>
Expand All @@ -219,6 +219,9 @@
<data name="WriteRequestComplete" xml:space="preserve">
<value>Web request completed. (Number of bytes processed: {0})</value>
</data>
<data name="WriteRequestCancelled" xml:space="preserve">
<value>Web request cancelled. (Number of bytes processed: {0})</value>
</data>
<data name="WriteRequestProgressActivity" xml:space="preserve">
<value>Web request status</value>
</data>
Expand Down