|
| 1 | +// ------------------------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. |
| 3 | +// ------------------------------------------------------------------------------ |
| 4 | +namespace Microsoft.Graph.PowerShell |
| 5 | +{ |
| 6 | + using Microsoft.Graph.PowerShell.Authentication.Common; |
| 7 | + using System; |
| 8 | + using System.Collections.ObjectModel; |
| 9 | + using System.IO; |
| 10 | + using System.Management.Automation; |
| 11 | + using System.Net.Http; |
| 12 | + using System.Threading; |
| 13 | + using System.Threading.Tasks; |
| 14 | + |
| 15 | + internal static class PSCmdletExtensions |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Gets a resolved or unresolved path from PSPath. |
| 19 | + /// </summary> |
| 20 | + /// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param> |
| 21 | + /// <param name="filePath">The file path to get a provider path for.</param> |
| 22 | + /// <param name="isResolvedPath">Determines whether get a resolved or unresolved provider path.</param> |
| 23 | + /// <returns>The provider path from PSPath.</returns> |
| 24 | + internal static string GetProviderPath(this PSCmdlet cmdlet, string filePath, bool isResolvedPath) |
| 25 | + { |
| 26 | + string providerPath = null; |
| 27 | + ProviderInfo provider; |
| 28 | + try |
| 29 | + { |
| 30 | + var paths = new Collection<string>(); |
| 31 | + if (isResolvedPath) |
| 32 | + { |
| 33 | + paths = cmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath(filePath, out provider); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + paths.Add(cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(filePath, out provider, out _)); |
| 38 | + } |
| 39 | + |
| 40 | + if (provider.Name != "FileSystem" || paths.Count == 0) |
| 41 | + { |
| 42 | + cmdlet.ThrowTerminatingError(new ErrorRecord(new Exception("Invalid path."), string.Empty, ErrorCategory.InvalidArgument, filePath)); |
| 43 | + } |
| 44 | + if (paths.Count > 1) |
| 45 | + { |
| 46 | + cmdlet.ThrowTerminatingError(new ErrorRecord(new Exception("Multiple paths not allowed."), string.Empty, ErrorCategory.InvalidArgument, filePath)); |
| 47 | + } |
| 48 | + providerPath = paths[0]; |
| 49 | + } |
| 50 | + catch (Exception) |
| 51 | + { |
| 52 | + providerPath = filePath; |
| 53 | + } |
| 54 | + |
| 55 | + return providerPath; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Saves a stream to a file on disk. |
| 60 | + /// </summary> |
| 61 | + /// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param> |
| 62 | + /// <param name="response">The HTTP response from the service.</param> |
| 63 | + /// <param name="inputStream">The stream to write to file.</param> |
| 64 | + /// <param name="filePath">The path to write the file to. This should include the file name and extension.</param> |
| 65 | + /// <param name="cancellationToken">A cancellation token that will be used to cancel the operation by the user.</param> |
| 66 | + internal static void WriteToFile(this PSCmdlet cmdlet, HttpResponseMessage response, Stream inputStream, string filePath, CancellationToken cancellationToken) |
| 67 | + { |
| 68 | + using (var fileProvider = ProtectedFileProvider.CreateFileProvider(filePath, FileProtection.ExclusiveWrite, new DiskDataStore())) |
| 69 | + { |
| 70 | + string downloadUrl = response?.RequestMessage?.RequestUri.ToString(); |
| 71 | + cmdlet.WriteToStream(inputStream, fileProvider.Stream, downloadUrl, cancellationToken); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Writes an input stream to an output stream. |
| 77 | + /// </summary> |
| 78 | + /// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param> |
| 79 | + /// <param name="inputStream">The stream to write to an output stream.</param> |
| 80 | + /// <param name="outputStream">The stream to write the input stream to.</param> |
| 81 | + /// <param name="cancellationToken">A cancellation token that will be used to cancel the operation by the user.</param> |
| 82 | + private static void WriteToStream(this PSCmdlet cmdlet, Stream inputStream, Stream outputStream, string downloadUrl, CancellationToken cancellationToken) |
| 83 | + { |
| 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."); |
| 89 | + try |
| 90 | + { |
| 91 | + do |
| 92 | + { |
| 93 | + cmdlet.WriteProgress(GetProgress(record, outputStream)); |
| 94 | + |
| 95 | + Task.Delay(1000, cancellationToken).Wait(cancellationToken); |
| 96 | + } while (!copyTask.IsCompleted && !cancellationToken.IsCancellationRequested); |
| 97 | + |
| 98 | + if (copyTask.IsCompleted) |
| 99 | + { |
| 100 | + cmdlet.WriteProgress(GetProgress(record, outputStream)); |
| 101 | + } |
| 102 | + } |
| 103 | + catch (OperationCanceledException) |
| 104 | + { |
| 105 | + } |
| 106 | + } |
| 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 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments