| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Core; |
| | 8 | | using Azure.Core.Pipeline; |
| | 9 | | using Azure.Storage.Blobs.Specialized; |
| | 10 | |
|
| | 11 | | namespace Azure.Storage.Blobs.Models |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// An <see cref="Operation{Int64}"/> for tracking the status of a |
| | 15 | | /// <see cref="BlobBaseClient.StartCopyFromUriAsync(Uri, System.Collections.Generic.IDictionary{String, String}, Acc |
| | 16 | | /// request. Its <see cref="Operation{Int64}.Value"/> upon successful |
| | 17 | | /// completion will be the number of bytes copied. |
| | 18 | | /// </summary> |
| | 19 | | public class CopyFromUriOperation : Operation<long> |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// The client used to check for completion. |
| | 23 | | /// </summary> |
| | 24 | | private readonly BlobBaseClient _client; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// The CancellationToken to use for all status checking. |
| | 28 | | /// </summary> |
| | 29 | | private readonly CancellationToken _cancellationToken; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Whether the operation has completed. |
| | 33 | | /// </summary> |
| | 34 | | private bool _hasCompleted; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets the number of bytes copied by the operation. |
| | 38 | | /// </summary> |
| | 39 | | private long? _value; |
| | 40 | |
|
| | 41 | | private Response _rawResponse; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets a value indicating whether the operation has completed. |
| | 45 | | /// </summary> |
| 196 | 46 | | public override bool HasCompleted => _hasCompleted; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets a value indicating whether the operation completed and |
| | 50 | | /// successfully produced a value. The <see cref="Operation{Int64}.Value"/> |
| | 51 | | /// property is the number of bytes copied by the operation. |
| | 52 | | /// </summary> |
| 28 | 53 | | public override bool HasValue => _value.HasValue; |
| | 54 | |
|
| | 55 | | /// <inheritdoc /> |
| 180 | 56 | | public override string Id { get; } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Gets the number of bytes copied by the operation. |
| | 60 | | /// </summary> |
| 84 | 61 | | public override long Value => OperationHelpers.GetValue(ref _value); |
| | 62 | |
|
| | 63 | | /// <inheritdoc /> |
| 156 | 64 | | public override Response GetRawResponse() => _rawResponse; |
| | 65 | |
|
| | 66 | | /// <inheritdoc /> |
| | 67 | | public override ValueTask<Response<long>> WaitForCompletionAsync(CancellationToken cancellationToken = default) |
| 0 | 68 | | this.DefaultWaitForCompletionAsync(cancellationToken); |
| | 69 | |
|
| | 70 | | /// <inheritdoc /> |
| | 71 | | public override ValueTask<Response<long>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken can |
| 84 | 72 | | this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken); |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Initializes a new <see cref="CopyFromUriOperation"/> instance for |
| | 76 | | /// mocking. |
| | 77 | | /// </summary> |
| 0 | 78 | | protected CopyFromUriOperation() |
| | 79 | | { |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Initializes a new <see cref="CopyFromUriOperation"/> instance |
| | 84 | | /// </summary> |
| | 85 | | /// <param name="client"> |
| | 86 | | /// The client used to check for completion. |
| | 87 | | /// </param> |
| | 88 | | /// <param name="id">The ID of this operation.</param> |
| | 89 | | public CopyFromUriOperation(string id, BlobBaseClient client): |
| 0 | 90 | | this(client, id, null, CancellationToken.None) |
| | 91 | | { |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Initializes a new <see cref="CopyFromUriOperation"/> instance |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="client"> |
| | 98 | | /// The client used to check for completion. |
| | 99 | | /// </param> |
| | 100 | | /// <param name="copyId">The ID of this operation.</param> |
| | 101 | | /// <param name="initialResponse"> |
| | 102 | | /// Either the response from initiating the operation or getting the |
| | 103 | | /// status if we're creating an operation from an existing ID. |
| | 104 | | /// </param> |
| | 105 | | /// <param name="cancellationToken"> |
| | 106 | | /// Optional <see cref="CancellationToken"/> to propagate |
| | 107 | | /// notifications that the operation should be cancelled. |
| | 108 | | /// </param> |
| 180 | 109 | | internal CopyFromUriOperation( |
| 180 | 110 | | BlobBaseClient client, |
| 180 | 111 | | string copyId, |
| 180 | 112 | | Response initialResponse, |
| 180 | 113 | | CancellationToken cancellationToken) |
| | 114 | | { |
| 180 | 115 | | Id = copyId; |
| 180 | 116 | | _value = null; |
| 180 | 117 | | _rawResponse = initialResponse; |
| 180 | 118 | | _client = client; |
| 180 | 119 | | _cancellationToken = cancellationToken; |
| 180 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Check for the latest status of the copy operation. |
| | 124 | | /// </summary> |
| | 125 | | /// <param name="cancellationToken"> |
| | 126 | | /// Optional <see cref="CancellationToken"/> to propagate |
| | 127 | | /// notifications that the operation should be cancelled. |
| | 128 | | /// </param> |
| | 129 | | /// <returns>The <see cref="Response"/> with the status update.</returns> |
| | 130 | | public override Response UpdateStatus( |
| | 131 | | CancellationToken cancellationToken = default) => |
| 0 | 132 | | UpdateStatusAsync(false, cancellationToken).EnsureCompleted(); |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// Check for the latest status of the copy operation. |
| | 136 | | /// </summary> |
| | 137 | | /// <param name="cancellationToken"> |
| | 138 | | /// Optional <see cref="CancellationToken"/> to propagate |
| | 139 | | /// notifications that the operation should be cancelled. |
| | 140 | | /// </param> |
| | 141 | | /// <returns>The <see cref="Response"/> with the status update.</returns> |
| | 142 | | public override async ValueTask<Response> UpdateStatusAsync( |
| | 143 | | CancellationToken cancellationToken = default) => |
| 84 | 144 | | await UpdateStatusAsync(true, cancellationToken).ConfigureAwait(false); |
| | 145 | |
|
| | 146 | | /// <summary> |
| | 147 | | /// Check for the latest status of the copy operation. |
| | 148 | | /// </summary> |
| | 149 | | /// <param name="cancellationToken"> |
| | 150 | | /// Optional <see cref="CancellationToken"/> to propagate |
| | 151 | | /// notifications that the operation should be cancelled. |
| | 152 | | /// </param> |
| | 153 | | /// <param name="async" /> |
| | 154 | | /// <returns>The <see cref="Response"/> with the status update.</returns> |
| | 155 | | private async Task<Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken) |
| | 156 | | { |
| | 157 | | // Short-circuit when already completed (which improves mocking |
| | 158 | | // scenarios that won't have a client). |
| 84 | 159 | | if (HasCompleted) |
| | 160 | | { |
| 0 | 161 | | return GetRawResponse(); |
| | 162 | | } |
| | 163 | |
|
| | 164 | | // Use our original CancellationToken if the user didn't provide one |
| 84 | 165 | | if (cancellationToken == default) |
| | 166 | | { |
| 84 | 167 | | cancellationToken = _cancellationToken; |
| | 168 | | } |
| | 169 | |
|
| | 170 | | // Get the latest status |
| 84 | 171 | | Response<BlobProperties> update = async |
| 84 | 172 | | ? await _client.GetPropertiesAsync(cancellationToken: cancellationToken).ConfigureAwait(false) |
| 84 | 173 | | : _client.GetProperties(cancellationToken: cancellationToken); |
| | 174 | |
|
| | 175 | | // Check if the operation is no longer running |
| 84 | 176 | | if (Id != update.Value.CopyId || |
| 84 | 177 | | update.Value.CopyStatus != CopyStatus.Pending) |
| | 178 | | { |
| 84 | 179 | | _hasCompleted = true; |
| | 180 | | } |
| | 181 | |
|
| | 182 | | // Check if the operation succeeded |
| 84 | 183 | | if (Id == update.Value.CopyId && |
| 84 | 184 | | update.Value.CopyStatus == CopyStatus.Success) |
| | 185 | | { |
| 84 | 186 | | _value = update.Value.ContentLength; |
| | 187 | | } |
| | 188 | |
|
| | 189 | | // Save this update as the latest raw response indicating the state |
| | 190 | | // of the copy operation |
| 84 | 191 | | Response response = update.GetRawResponse(); |
| 84 | 192 | | _rawResponse = response; |
| 84 | 193 | | return response; |
| 84 | 194 | | } |
| | 195 | | } |
| | 196 | | } |