|
| 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 System; |
| 7 | + using System.Collections.ObjectModel; |
| 8 | + using System.IO; |
| 9 | + using System.Management.Automation; |
| 10 | + using System.Threading; |
| 11 | + using System.Threading.Tasks; |
| 12 | + |
| 13 | + internal static class PSCmdletExtensions |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Gets a resolved or unresolved path from PSPath. |
| 17 | + /// </summary> |
| 18 | + /// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param> |
| 19 | + /// <param name="filePath">The file path to get a provider path for.</param> |
| 20 | + /// <param name="isResolvedPath">Determines whether get a resolved or unresolved provider path.</param> |
| 21 | + /// <returns>The provider path from PSPath.</returns> |
| 22 | + internal static string GetProviderPath(this PSCmdlet cmdlet, string filePath, bool isResolvedPath) |
| 23 | + { |
| 24 | + string providerPath = null; |
| 25 | + ProviderInfo provider; |
| 26 | + try |
| 27 | + { |
| 28 | + var paths = new Collection<string>(); |
| 29 | + if (isResolvedPath) |
| 30 | + { |
| 31 | + paths = cmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath(filePath, out provider); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + paths.Add(cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(filePath, out provider, out _)); |
| 36 | + } |
| 37 | + |
| 38 | + if (provider.Name != "FileSystem" || paths.Count == 0) |
| 39 | + { |
| 40 | + cmdlet.ThrowTerminatingError(new ErrorRecord(new Exception("Invalid path."), string.Empty, ErrorCategory.InvalidArgument, filePath)); |
| 41 | + } |
| 42 | + if (paths.Count > 1) |
| 43 | + { |
| 44 | + cmdlet.ThrowTerminatingError(new ErrorRecord(new Exception("Multiple paths not allowed."), string.Empty, ErrorCategory.InvalidArgument, filePath)); |
| 45 | + } |
| 46 | + providerPath = paths[0]; |
| 47 | + } |
| 48 | + catch (Exception) |
| 49 | + { |
| 50 | + providerPath = filePath; |
| 51 | + } |
| 52 | + |
| 53 | + return providerPath; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Saves a stream to a file on disk. |
| 58 | + /// </summary> |
| 59 | + /// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param> |
| 60 | + /// <param name="inputStream">The stream to write to file.</param> |
| 61 | + /// <param name="filePath">The path to write the file to. This should include the file name and extension.</param> |
| 62 | + /// <param name="cancellationToken">A cancellation token that will be used to cancel the operation by the user.</param> |
| 63 | + internal static void WriteToFile(this PSCmdlet cmdlet, Stream inputStream, string filePath, CancellationToken cancellationToken) |
| 64 | + { |
| 65 | + using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) |
| 66 | + { |
| 67 | + cmdlet.WriteToStream(inputStream, fileStream, cancellationToken); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Write an input stream to an output stream. |
| 73 | + /// </summary> |
| 74 | + /// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param> |
| 75 | + /// <param name="inputStream">The stream to write to an output stream.</param> |
| 76 | + /// <param name="outputStream">The stream to write the input stream to.</param> |
| 77 | + /// <param name="cancellationToken">A cancellation token that will be used to cancel the operation by the user.</param> |
| 78 | + private static void WriteToStream(this PSCmdlet cmdlet, Stream inputStream, Stream outputStream, CancellationToken cancellationToken) |
| 79 | + { |
| 80 | + var copyTask = inputStream.CopyToAsync(outputStream); |
| 81 | + var record = new ProgressRecord(000000000, "WriteRequestProgressActivity", "WriteRequestProgressStatus"); |
| 82 | + try |
| 83 | + { |
| 84 | + do |
| 85 | + { |
| 86 | + record.StatusDescription = string.Format("Number of bytes processed {0}", outputStream.Position); |
| 87 | + cmdlet.WriteProgress(record); |
| 88 | + |
| 89 | + Task.Delay(1000, cancellationToken).Wait(cancellationToken); |
| 90 | + } while (!copyTask.IsCompleted && !cancellationToken.IsCancellationRequested); |
| 91 | + |
| 92 | + if (copyTask.IsCompleted) |
| 93 | + { |
| 94 | + record.StatusDescription = string.Format("Number of bytes processed {0}", outputStream.Position); |
| 95 | + cmdlet.WriteProgress(record); |
| 96 | + } |
| 97 | + } |
| 98 | + catch (OperationCanceledException) |
| 99 | + { |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments