| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Net; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Text; |
| | 9 | | using Azure.Storage.Blobs.Models; |
| | 10 | | using Tags = System.Collections.Generic.IDictionary<string, string>; |
| | 11 | |
|
| | 12 | | namespace Azure.Storage.Blobs |
| | 13 | | { |
| | 14 | | internal static partial class BlobExtensions |
| | 15 | | { |
| | 16 | | internal static IDictionary<string, string> ToTagDictionary(this BlobTags blobTags) |
| | 17 | | { |
| 10680 | 18 | | if (blobTags?.BlobTagSet == null) |
| | 19 | | { |
| 10596 | 20 | | return null; |
| | 21 | | } |
| 84 | 22 | | Dictionary<string, string> tags = new Dictionary<string, string>(); |
| 504 | 23 | | foreach (BlobTag blobTag in blobTags.BlobTagSet) |
| | 24 | | { |
| 168 | 25 | | tags[blobTag.Key] = blobTag.Value; |
| | 26 | | } |
| | 27 | |
|
| 84 | 28 | | return tags; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | internal static BlobTags ToBlobTags(this Tags tags) |
| | 32 | | { |
| 200 | 33 | | BlobTags blobTags = new BlobTags(); |
| 944 | 34 | | foreach (KeyValuePair<string, string> tag in tags) |
| | 35 | | { |
| 272 | 36 | | blobTags.BlobTagSet.Add(new BlobTag |
| 272 | 37 | | { |
| 272 | 38 | | Key = tag.Key, |
| 272 | 39 | | Value = tag.Value |
| 272 | 40 | | }); |
| | 41 | | } |
| 200 | 42 | | return blobTags; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | internal static BlobItem ToBlobItem(this BlobItemInternal blobItemInternal) |
| | 46 | | { |
| 10608 | 47 | | if (blobItemInternal == null) |
| | 48 | | { |
| 0 | 49 | | return null; |
| | 50 | | } |
| | 51 | |
|
| 10608 | 52 | | return new BlobItem |
| 10608 | 53 | | { |
| 10608 | 54 | | Name = blobItemInternal.Name, |
| 10608 | 55 | | Deleted = blobItemInternal.Deleted, |
| 10608 | 56 | | Snapshot = blobItemInternal.Snapshot, |
| 10608 | 57 | | Properties = blobItemInternal.Properties, |
| 10608 | 58 | | VersionId = blobItemInternal.VersionId, |
| 10608 | 59 | | IsLatestVersion = blobItemInternal.IsCurrentVersion, |
| 10608 | 60 | | Metadata = blobItemInternal.Metadata?.Count > 0 |
| 10608 | 61 | | ? blobItemInternal.Metadata |
| 10608 | 62 | | : null, |
| 10608 | 63 | | Tags = blobItemInternal.BlobTags.ToTagDictionary(), |
| 10608 | 64 | | ObjectReplicationSourceProperties = blobItemInternal.ObjectReplicationMetadata?.Count > 0 |
| 10608 | 65 | | ? ParseObjectReplicationMetadata(blobItemInternal.ObjectReplicationMetadata) |
| 10608 | 66 | | : null |
| 10608 | 67 | | }; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | internal static IEnumerable<BlobItem> ToBlobItems(this IEnumerable<BlobItemInternal> blobItemInternals) |
| | 71 | | { |
| 280 | 72 | | if (blobItemInternals == null) |
| | 73 | | { |
| 0 | 74 | | return null; |
| | 75 | | } |
| | 76 | |
|
| 280 | 77 | | List<BlobItem> blobItems = new List<BlobItem>(); |
| 1384 | 78 | | foreach (BlobItemInternal blobItemInternal in blobItemInternals) |
| | 79 | | { |
| 412 | 80 | | blobItems.Add(blobItemInternal.ToBlobItem()); |
| | 81 | | } |
| 280 | 82 | | return blobItems; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | internal static string ToTagsString(this Tags tags) |
| | 86 | | { |
| 56 | 87 | | if (tags == null) |
| | 88 | | { |
| 0 | 89 | | return null; |
| | 90 | | } |
| | 91 | |
|
| 56 | 92 | | List<string> encodedTags = new List<string>(); |
| 312 | 93 | | foreach (KeyValuePair<string, string> tag in tags) |
| | 94 | | { |
| 100 | 95 | | encodedTags.Add($"{WebUtility.UrlEncode(tag.Key)}={WebUtility.UrlEncode(tag.Value)}"); |
| | 96 | | } |
| 56 | 97 | | return string.Join("&", encodedTags); |
| | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Creates a new BlobProperties object backed by BlobPropertiesInternal. |
| | 102 | | /// </summary> |
| | 103 | | /// <param name="properties"> |
| | 104 | | /// The BlobPropertiesInternal returned with the request. |
| | 105 | | /// </param> |
| | 106 | | internal static BlobProperties ToBlobProperties(this BlobPropertiesInternal properties) => |
| 1372 | 107 | | new BlobProperties() |
| 1372 | 108 | | { |
| 1372 | 109 | | LastModified = properties.LastModified, |
| 1372 | 110 | | CreatedOn = properties.CreatedOn, |
| 1372 | 111 | | Metadata = properties.Metadata, |
| 1372 | 112 | | ObjectReplicationDestinationPolicyId = properties.ObjectReplicationPolicyId, |
| 1372 | 113 | | ObjectReplicationSourceProperties = |
| 1372 | 114 | | properties.ObjectReplicationRules?.Count > 0 |
| 1372 | 115 | | ? BlobExtensions.ParseObjectReplicationIds(properties.ObjectReplicationRules) |
| 1372 | 116 | | : null, |
| 1372 | 117 | | BlobType = properties.BlobType, |
| 1372 | 118 | | CopyCompletedOn = properties.CopyCompletedOn, |
| 1372 | 119 | | CopyStatusDescription = properties.CopyStatusDescription, |
| 1372 | 120 | | CopyId = properties.CopyId, |
| 1372 | 121 | | CopyProgress = properties.CopyProgress, |
| 1372 | 122 | | CopySource = properties.CopySource, |
| 1372 | 123 | | CopyStatus = properties.CopyStatus, |
| 1372 | 124 | | IsIncrementalCopy = properties.IsIncrementalCopy, |
| 1372 | 125 | | DestinationSnapshot = properties.DestinationSnapshot, |
| 1372 | 126 | | LeaseDuration = properties.LeaseDuration, |
| 1372 | 127 | | LeaseState = properties.LeaseState, |
| 1372 | 128 | | LeaseStatus = properties.LeaseStatus, |
| 1372 | 129 | | ContentLength = properties.ContentLength, |
| 1372 | 130 | | ContentType = properties.ContentType, |
| 1372 | 131 | | ETag = properties.ETag, |
| 1372 | 132 | | ContentHash = properties.ContentHash, |
| 1372 | 133 | | ContentEncoding = properties.ContentEncoding, |
| 1372 | 134 | | ContentDisposition = properties.ContentDisposition, |
| 1372 | 135 | | ContentLanguage = properties.ContentLanguage, |
| 1372 | 136 | | CacheControl = properties.CacheControl, |
| 1372 | 137 | | BlobSequenceNumber = properties.BlobSequenceNumber, |
| 1372 | 138 | | AcceptRanges = properties.AcceptRanges, |
| 1372 | 139 | | BlobCommittedBlockCount = properties.BlobCommittedBlockCount, |
| 1372 | 140 | | IsServerEncrypted = properties.IsServerEncrypted, |
| 1372 | 141 | | EncryptionKeySha256 = properties.EncryptionKeySha256, |
| 1372 | 142 | | EncryptionScope = properties.EncryptionScope, |
| 1372 | 143 | | AccessTier = properties.AccessTier, |
| 1372 | 144 | | AccessTierInferred = properties.AccessTierInferred, |
| 1372 | 145 | | ArchiveStatus = properties.ArchiveStatus, |
| 1372 | 146 | | AccessTierChangedOn = properties.AccessTierChangedOn, |
| 1372 | 147 | | VersionId = properties.VersionId, |
| 1372 | 148 | | IsLatestVersion = properties.IsCurrentVersion, |
| 1372 | 149 | | TagCount = properties.TagCount, |
| 1372 | 150 | | ExpiresOn = properties.ExpiresOn, |
| 1372 | 151 | | IsSealed = properties.IsSealed, |
| 1372 | 152 | | RehydratePriority = properties.RehydratePriority |
| 1372 | 153 | | }; |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// Internal. Parses Object Replication Policy ID from Rule ID and sets the Policy ID for source blobs. |
| | 157 | | /// </summary> |
| | 158 | | /// <param name="OrIds"> |
| | 159 | | /// Unparsed Object Replication headers. |
| | 160 | | /// For source blobs, the dictionary will contain keys that contain the policy id and rule id separated |
| | 161 | | /// by a underscore (e.g. policyId_ruleId). The value of these keys will be the replication status (e.g. Complet |
| | 162 | | /// For destination blobs, the dictionary will contain one entry where the key will be "policy-id" |
| | 163 | | /// and the value will be the destination policy id. No parsing will be required for this. |
| | 164 | | /// </param> |
| | 165 | | /// <returns> |
| | 166 | | /// If the blob has object replication policy(s) applied and is the source blob, this method will return a |
| | 167 | | /// List of <see cref="ObjectReplicationPolicy"/>, which contains the Policy ID and the respective |
| | 168 | | /// rule(s) and replication status(s) for each policy. |
| | 169 | | /// If the blob has object replication policy applied and is the destination blob, |
| | 170 | | /// this method will return default as the policy id should be set in ObjectReplicationDestinationPolicyId |
| | 171 | | /// (e.g. <see cref="BlobProperties.ObjectReplicationDestinationPolicyId"/>,<see cref="BlobDownloadDetails.Objec |
| | 172 | | /// </returns> |
| | 173 | | internal static IList<ObjectReplicationPolicy> ParseObjectReplicationIds(this IDictionary<string, string> OrIds) |
| | 174 | | { |
| | 175 | | try |
| | 176 | | { |
| | 177 | | // If the dictionary contains a key with policy id, we are not required to do any parsing since |
| | 178 | | // the policy id should already be stored in the ObjectReplicationDestinationPolicyId. |
| 32 | 179 | | KeyValuePair<string, string> destPolicy = OrIds.Single(id => (id.Key == "policy-id")); |
| 8 | 180 | | return default; |
| | 181 | | } |
| 8 | 182 | | catch (Exception) |
| | 183 | | { |
| | 184 | | // If an exception is thrown by Single then we have confirmed that there's not a policy id already |
| | 185 | | // stored in the ObjectReplicationDestinationPolicyId and that we have the unparsed |
| | 186 | | // Object Replication headers from the source blob. |
| 8 | 187 | | } |
| 8 | 188 | | List<ObjectReplicationPolicy> OrProperties = new List<ObjectReplicationPolicy>(); |
| 32 | 189 | | foreach (KeyValuePair<string, string> status in OrIds) |
| | 190 | | { |
| 8 | 191 | | string[] parsedIds = status.Key.Split('_'); |
| 0 | 192 | | int policyIndex = OrProperties.FindIndex(policy => policy.PolicyId == parsedIds[0]); |
| 8 | 193 | | if (policyIndex > -1) |
| | 194 | | { |
| 0 | 195 | | OrProperties[policyIndex].Rules.Add(new ObjectReplicationRule() |
| 0 | 196 | | { |
| 0 | 197 | | RuleId = parsedIds[1], |
| 0 | 198 | | ReplicationStatus = (ObjectReplicationStatus)Enum.Parse(typeof(ObjectReplicationStatus), status. |
| 0 | 199 | | }); |
| | 200 | | } |
| | 201 | | else |
| | 202 | | { |
| 8 | 203 | | IList<ObjectReplicationRule> NewRuleStatus = new List<ObjectReplicationRule>(); |
| 8 | 204 | | NewRuleStatus.Add(new ObjectReplicationRule() |
| 8 | 205 | | { |
| 8 | 206 | | RuleId = parsedIds[1], |
| 8 | 207 | | ReplicationStatus = (ObjectReplicationStatus)Enum.Parse(typeof(ObjectReplicationStatus), status. |
| 8 | 208 | | }); |
| 8 | 209 | | OrProperties.Add(new ObjectReplicationPolicy() |
| 8 | 210 | | { |
| 8 | 211 | | PolicyId = parsedIds[0], |
| 8 | 212 | | Rules = NewRuleStatus |
| 8 | 213 | | }); |
| | 214 | | } |
| | 215 | | } |
| 8 | 216 | | return OrProperties; |
| 8 | 217 | | } |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Internal. Parses Object Replication Policy ID from Rule ID and sets the Policy ID for source blobs. |
| | 221 | | /// </summary> |
| | 222 | | /// <param name="OrMetadata"> |
| | 223 | | /// Unparsed Object Replication headers. |
| | 224 | | /// For source blobs, the dictionary will contain keys that are prefixed with "or-" and followed by the |
| | 225 | | /// policy id and rule id separated by a underscore (e.g. or-policyId_ruleId). |
| | 226 | | /// The value of this metadata key will be the replication status (e.g. Complete, Failed). |
| | 227 | | /// </param> |
| | 228 | | /// <returns> |
| | 229 | | /// If the blob has object replication policy(s) applied and is the source blob, this method will return a |
| | 230 | | /// List of <see cref="ObjectReplicationPolicy"/>, which contains the Policy ID and the respective |
| | 231 | | /// rule(s) and replication status(s) for each policy. |
| | 232 | | /// </returns> |
| | 233 | | internal static IList<ObjectReplicationPolicy> ParseObjectReplicationMetadata(this IDictionary<string, string> O |
| | 234 | | { |
| 84 | 235 | | List<ObjectReplicationPolicy> OrProperties = new List<ObjectReplicationPolicy>(); |
| 336 | 236 | | foreach (KeyValuePair<string, string> status in OrMetadata) |
| | 237 | | { |
| 84 | 238 | | string[] parsedIds = status.Key.Split('_'); |
| 84 | 239 | | if (parsedIds[0].StartsWith("or-", System.StringComparison.InvariantCulture)) |
| | 240 | | { |
| 84 | 241 | | parsedIds[0] = parsedIds[0].Substring("or-".Length); |
| | 242 | | } |
| 0 | 243 | | int policyIndex = OrProperties.FindIndex(policy => policy.PolicyId == parsedIds[0]); |
| 84 | 244 | | if (policyIndex > -1) |
| | 245 | | { |
| 0 | 246 | | OrProperties[policyIndex].Rules.Add(new ObjectReplicationRule() |
| 0 | 247 | | { |
| 0 | 248 | | RuleId = parsedIds[1], |
| 0 | 249 | | ReplicationStatus = (ObjectReplicationStatus)Enum.Parse(typeof(ObjectReplicationStatus), status. |
| 0 | 250 | | }); |
| | 251 | | } |
| | 252 | | else |
| | 253 | | { |
| 84 | 254 | | IList<ObjectReplicationRule> NewRuleStatus = new List<ObjectReplicationRule>(); |
| 84 | 255 | | NewRuleStatus.Add(new ObjectReplicationRule() |
| 84 | 256 | | { |
| 84 | 257 | | RuleId = parsedIds[1], |
| 84 | 258 | | ReplicationStatus = (ObjectReplicationStatus)Enum.Parse(typeof(ObjectReplicationStatus), status. |
| 84 | 259 | | }); |
| 84 | 260 | | OrProperties.Add(new ObjectReplicationPolicy() |
| 84 | 261 | | { |
| 84 | 262 | | PolicyId = parsedIds[0], |
| 84 | 263 | | Rules = NewRuleStatus |
| 84 | 264 | | }); |
| | 265 | | } |
| | 266 | | } |
| 84 | 267 | | return OrProperties; |
| | 268 | | } |
| | 269 | |
|
| | 270 | | internal static TaggedBlobItem ToBlobTagItem(this FilterBlobItem filterBlobItem) |
| | 271 | | { |
| 42 | 272 | | if (filterBlobItem == null) |
| | 273 | | { |
| 0 | 274 | | return null; |
| | 275 | | } |
| | 276 | |
|
| 42 | 277 | | return new TaggedBlobItem |
| 42 | 278 | | { |
| 42 | 279 | | BlobName = filterBlobItem.BlobName, |
| 42 | 280 | | BlobContainerName = filterBlobItem.BlobContainerName |
| 42 | 281 | | }; |
| | 282 | | } |
| | 283 | |
|
| | 284 | | internal static List<TaggedBlobItem> ToBlobTagItems(this IEnumerable<FilterBlobItem> filterBlobItems) |
| | 285 | | { |
| 12 | 286 | | if (filterBlobItems == null) |
| | 287 | | { |
| 0 | 288 | | return null; |
| | 289 | | } |
| | 290 | |
|
| 12 | 291 | | List<TaggedBlobItem> list = new List<TaggedBlobItem>(); |
| | 292 | |
|
| 108 | 293 | | foreach (FilterBlobItem filterBlobItem in filterBlobItems) |
| | 294 | | { |
| 42 | 295 | | list.Add(filterBlobItem.ToBlobTagItem()); |
| | 296 | | } |
| | 297 | |
|
| 12 | 298 | | return list; |
| | 299 | | } |
| | 300 | |
|
| | 301 | | internal static RehydratePriority? ToRehydratePriority(this string rehydratePriority) |
| | 302 | | { |
| 0 | 303 | | if (rehydratePriority == null) |
| | 304 | | { |
| 0 | 305 | | return null; |
| | 306 | | } |
| | 307 | |
|
| 0 | 308 | | if (rehydratePriority == RehydratePriority.High.ToString()) |
| | 309 | | { |
| 0 | 310 | | return RehydratePriority.High; |
| | 311 | | } |
| | 312 | | else |
| | 313 | | { |
| 0 | 314 | | return RehydratePriority.Standard; |
| | 315 | | } |
| | 316 | | } |
| | 317 | | } |
| | 318 | | } |