@@ -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