Module: Azure::Storage::Blob::Serialization

Includes:
Common::Service::Serialization
Defined in:
blob/lib/azure/storage/blob/serialization.rb

Class Method Summary collapse

Class Method Details

.add_block(type, block_node, block_list) ⇒ Object



322
323
324
325
326
327
328
329
# File 'blob/lib/azure/storage/blob/serialization.rb', line 322

def self.add_block(type, block_node, block_list)
  block = Block.new do |b|
    b.name = Base64.strict_decode64(block_node.Name.text) if (block_node > "Name").any?
    b.size = block_node.Size.text.to_i if (block_node > "Size").any?
    b.type = type
  end
  block_list[type].push block
end

.blob_enumeration_results_from_xml(xml) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'blob/lib/azure/storage/blob/serialization.rb', line 132

def self.blob_enumeration_results_from_xml(xml)
  xml = slopify(xml)
  expect_node("EnumerationResults", xml)

  results = enumeration_results_from_xml(xml, Azure::Storage::Common::Service::EnumerationResults.new)

  return results unless (xml > "Blobs").any?

  if ((xml > "Blobs") > "Blob").any?
    if xml.Blobs.Blob.count == 0
      results.push(blob_from_xml(xml.Blobs.Blob))
    else
      xml.Blobs.Blob.each { |blob_node|
        results.push(blob_from_xml(blob_node))
      }
    end
  end

  if ((xml > "Blobs") > "BlobPrefix").any?
    if xml.Blobs.BlobPrefix.count == 0
      results.push(blob_prefix_from_xml(xml.Blobs.BlobPrefix))
    else
      xml.Blobs.BlobPrefix.each { |blob_prefix|
        results.push(blob_prefix_from_xml(blob_prefix))
      }
    end
  end

  results
end

.blob_from_headers(headers) ⇒ Object



187
188
189
190
191
192
193
194
# File 'blob/lib/azure/storage/blob/serialization.rb', line 187

def self.blob_from_headers(headers)
  Blob.new do |blob|
    blob.properties = blob_properties_from_headers(headers)
    blob. = (headers)
    blob.encrypted = headers[Azure::Storage::Common::HeaderConstants::REQUEST_SERVER_ENCRYPTED] || headers[Azure::Storage::Common::HeaderConstants::SERVER_ENCRYPTED]
    blob.encrypted = blob.encrypted.to_s == "true" unless blob.encrypted.nil?
  end
end

.blob_from_xml(xml) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'blob/lib/azure/storage/blob/serialization.rb', line 171

def self.blob_from_xml(xml)
  xml = slopify(xml)
  expect_node("Blob", xml)

  Blob.new do |blob|
    blob.name = xml.Name.text if (xml > "Name").any?
    blob.snapshot = xml.Snapshot.text if (xml > "Snapshot").any?

    blob. = (xml.Metadata) if (xml > "Metadata").any?
    if (xml > "Properties").any?
      blob.properties = blob_properties_from_xml(xml.Properties)
      blob.encrypted = xml.Properties.ServerEncrypted.text == "true" if (xml.Properties > "ServerEncrypted").any?
    end
  end
end

.blob_prefix_from_xml(xml) ⇒ Object



163
164
165
166
167
168
169
# File 'blob/lib/azure/storage/blob/serialization.rb', line 163

def self.blob_prefix_from_xml(xml)
  xml = slopify(xml)
  expect_node("BlobPrefix", xml)

  name = xml.Name.text if (xml > "Name").any?
  name
end

.blob_properties_from_headers(headers) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'blob/lib/azure/storage/blob/serialization.rb', line 230

def self.blob_properties_from_headers(headers)
  props = {}

  props[:last_modified] = headers["Last-Modified"]
  props[:etag] = headers["Etag"]
  props[:lease_status] = headers["x-ms-lease-status"]
  props[:lease_state] = headers["x-ms-lease-state"]
  props[:lease_duration] = headers["x-ms-lease-duration"]

  props[:content_length] = headers["x-ms-blob-content-length"] || headers["Content-Length"]
  props[:content_length] = props[:content_length].to_i if props[:content_length]

  props[:content_type] =  headers["x-ms-blob-content-type"] || headers["Content-Type"]
  props[:content_encoding] = headers["x-ms-blob-content-encoding"] || headers["Content-Encoding"]
  props[:content_language] = headers["x-ms-blob-content-language"] || headers["Content-Language"]
  props[:content_disposition] = headers["x-ms-blob-content-disposition"] || headers["Content-Disposition"]
  props[:content_md5] = headers["x-ms-blob-content-md5"] || headers["Content-MD5"]
  props[:range_md5] = headers["Content-MD5"] if headers["x-ms-blob-content-md5"] && headers["Content-MD5"]

  props[:cache_control] = headers["x-ms-blob-cache-control"] || headers["Cache-Control"]
  props[:sequence_number] = headers["x-ms-blob-sequence-number"].to_i if headers["x-ms-blob-sequence-number"]
  props[:blob_type] = headers["x-ms-blob-type"]

  props[:copy_id] = headers["x-ms-copy-id"]
  props[:copy_status] = headers["x-ms-copy-status"]
  props[:copy_source] = headers["x-ms-copy-source"]
  props[:copy_progress] = headers["x-ms-copy-progress"]
  props[:copy_completion_time] = headers["x-ms-copy-completion-time"]
  props[:copy_status_description] = headers["x-ms-copy-status-description"]

  props[:accept_ranges] = headers["Accept-Ranges"].to_i if headers["Accept-Ranges"]

  props[:append_offset] = headers["x-ms-blob-append-offset"].to_i if headers["x-ms-blob-append-offset"]
  props[:committed_count] = headers["x-ms-blob-committed-block-count"].to_i if headers["x-ms-blob-committed-block-count"]
  props[:incremental_copy] = headers["x-ms-incremental-copy"].to_s == "true" if headers["x-ms-incremental-copy"]

  props
end

.blob_properties_from_xml(xml) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'blob/lib/azure/storage/blob/serialization.rb', line 196

def self.blob_properties_from_xml(xml)
  xml = slopify(xml)
  expect_node("Properties", xml)

  props = {}

  props[:access_tier] = (xml > "AccessTier").text if (xml > "AccessTier").any?
  props[:access_tier_change_time] = (xml > "AccessTierChangeTime").text if (xml > "AccessTierChangeTime").any?
  props[:creation_Time] = (xml > "Creation-Time").text if (xml > "Creation-Time").any?
  props[:last_modified] = (xml > "Last-Modified").text if (xml > "Last-Modified").any?
  props[:etag] = xml.Etag.text if (xml > "Etag").any?
  props[:lease_status] = xml.LeaseStatus.text if (xml > "LeaseStatus").any?
  props[:lease_state] = xml.LeaseState.text if (xml > "LeaseState").any?
  props[:lease_duration] = xml.LeaseDuration.text if (xml > "LeaseDuration").any?
  props[:content_length] = (xml > "Content-Length").text.to_i if (xml > "Content-Length").any?
  props[:content_type] = (xml > "Content-Type").text if (xml > "Content-Type").any?
  props[:content_encoding] = (xml > "Content-Encoding").text if (xml > "Content-Encoding").any?
  props[:content_language] = (xml > "Content-Language").text if (xml > "Content-Language").any?
  props[:content_md5] = (xml > "Content-MD5").text if (xml > "Content-MD5").any?

  props[:cache_control] = (xml > "Cache-Control").text if (xml > "Cache-Control").any?
  props[:sequence_number] = (xml > "x-ms-blob-sequence-number").text.to_i if (xml > "x-ms-blob-sequence-number").any?
  props[:blob_type] = xml.BlobType.text if (xml > "BlobType").any?
  props[:copy_id] = xml.CopyId.text if (xml > "CopyId").any?
  props[:copy_status] = xml.CopyStatus.text if (xml > "CopyStatus").any?
  props[:copy_source] = xml.CopySource.text if (xml > "CopySource").any?
  props[:copy_progress] = xml.CopyProgress.text if (xml > "CopyProgress").any?
  props[:copy_completion_time] = xml.CopyCompletionTime.text if (xml > "CopyCompletionTime").any?
  props[:copy_status_description] = xml.CopyStatusDescription.text if (xml > "CopyStatusDescription").any?
  props[:incremental_copy] = xml.IncrementalCopy.text == "true" if (xml > "IncrementalCopy").any?

  props
end

.block_list_from_xml(xml) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'blob/lib/azure/storage/blob/serialization.rb', line 288

def self.block_list_from_xml(xml)
  xml = slopify(xml)
  expect_node("BlockList", xml)

  block_list = {
    committed: [],
    uncommitted: []
  }

  if ((xml > "CommittedBlocks") > "Block").any?
    if xml.CommittedBlocks.Block.count == 0
      add_block(:committed, xml.CommittedBlocks.Block, block_list)
    else
      xml.CommittedBlocks.Block.each { |block_node|
        add_block(:committed, block_node, block_list)
      }
    end
  end

  return block_list unless (xml > "UncommittedBlocks")

  if ((xml > "UncommittedBlocks") > "Block").any?
    if xml.UncommittedBlocks.Block.count == 0
      add_block(:uncommitted, xml.UncommittedBlocks.Block, block_list)
    else
      xml.UncommittedBlocks.Block.each { |block_node|
        add_block(:uncommitted, block_node, block_list)
      }
    end
  end

  block_list
end

.block_list_to_xml(block_list) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'blob/lib/azure/storage/blob/serialization.rb', line 269

def self.block_list_to_xml(block_list)
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.BlockList {
      block_list.each { |block|
        encoded_id = Base64.strict_encode64(block[0])
        case block[1]
        when :uncommitted
          xml.Uncommitted encoded_id
        when :committed
          xml.Committed encoded_id
        else
          xml.Latest encoded_id
        end
      }
    }
  end
  builder.to_xml
end

.container_enumeration_results_from_xml(xml) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'blob/lib/azure/storage/blob/serialization.rb', line 33

def self.container_enumeration_results_from_xml(xml)
  xml = slopify(xml)
  expect_node("EnumerationResults", xml)

  results = enumeration_results_from_xml(xml, Azure::Storage::Common::Service::EnumerationResults.new)

  return results unless (xml > "Containers").any? && ((xml > "Containers") > "Container").any?

  if xml.Containers.Container.count == 0
    results.push(container_from_xml(xml.Containers.Container))
  else
    xml.Containers.Container.each { |container_node|
      results.push(container_from_xml(container_node))
    }
  end

  results
end

.container_from_headers(headers) ⇒ Object



89
90
91
92
93
94
95
# File 'blob/lib/azure/storage/blob/serialization.rb', line 89

def self.container_from_headers(headers)
  Container::Container.new do |container|
    container.properties = container_properties_from_headers(headers)
    container.public_access_level = public_access_level_from_headers(headers)
    container. = (headers)
  end
end

.container_from_xml(xml) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'blob/lib/azure/storage/blob/serialization.rb', line 77

def self.container_from_xml(xml)
  xml = slopify(xml)
  expect_node("Container", xml)

  Container::Container.new do |container|
    container.name = xml.Name.text if (xml > "Name").any?
    container.properties = container_properties_from_xml(xml.Properties) if (xml > "Properties").any?
    container. = (xml.Metadata) if (xml > "Metadata").any?
    container.public_access_level = public_access_level_from_properties_xml(xml.Properties) if (xml > "Properties").any?
  end
end

.container_properties_from_headers(headers) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'blob/lib/azure/storage/blob/serialization.rb', line 112

def self.container_properties_from_headers(headers)
  props = {}

  props[:last_modified] = headers["Last-Modified"]
  props[:etag] = headers["Etag"]
  props[:lease_status] = headers["x-ms-lease-status"]
  props[:lease_state] = headers["x-ms-lease-state"]
  props[:lease_duration] = headers["x-ms-lease-duration"]

  props
end

.container_properties_from_xml(xml) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'blob/lib/azure/storage/blob/serialization.rb', line 97

def self.container_properties_from_xml(xml)
  xml = slopify(xml)
  expect_node("Properties", xml)

  props = {}

  props[:last_modified] = (xml > "Last-Modified").text if (xml > "Last-Modified").any?
  props[:etag] = xml.Etag.text if (xml > "Etag").any?
  props[:lease_status] = xml.LeaseStatus.text if (xml > "LeaseStatus").any?
  props[:lease_state] = xml.LeaseState.text if (xml > "LeaseState").any?
  props[:lease_duration] = xml.LeaseDuration.text if (xml > "LeaseDuration").any?

  props
end

.key_info_to_xml(start, expiry) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'blob/lib/azure/storage/blob/serialization.rb', line 52

def self.key_info_to_xml(start, expiry)
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.KeyInfo {
      xml.Start start.utc.iso8601
      xml.Expiry expiry.utc.iso8601
    }
  end
  builder.to_xml
end

.page_list_from_xml(xml) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'blob/lib/azure/storage/blob/serialization.rb', line 331

def self.page_list_from_xml(xml)
  xml = slopify(xml)
  expect_node("PageList", xml)

  page_list = []

  return page_list unless (xml > "PageRange").any?

  if xml.PageRange.count == 0
    page_list.push [xml.PageRange.Start.text.to_i, xml.PageRange.End.text.to_i]
  else
    xml.PageRange.each { |page_range|
      page_list.push [page_range.Start.text.to_i, page_range.End.text.to_i]
    }
  end

  page_list
end

.public_access_level_from_headers(headers) ⇒ Object



124
125
126
# File 'blob/lib/azure/storage/blob/serialization.rb', line 124

def self.public_access_level_from_headers(headers)
  headers["x-ms-blob-public-access"]
end

.public_access_level_from_properties_xml(xml) ⇒ Object



128
129
130
# File 'blob/lib/azure/storage/blob/serialization.rb', line 128

def self.public_access_level_from_properties_xml(xml)
  (xml > "PublicAccess").any? ? xml.PublicAccess.text : nil
end

.user_delegation_key_from_xml(xml) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'blob/lib/azure/storage/blob/serialization.rb', line 62

def self.user_delegation_key_from_xml(xml)
  xml = slopify(xml)
  expect_node("UserDelegationKey", xml)

  Azure::Storage::Common::Service::UserDelegationKey.new do |user_delegation_key|
    user_delegation_key.signed_oid = xml.SignedOid.text if (xml > "SignedOid").any?
    user_delegation_key.signed_tid = xml.SignedTid.text if (xml > "SignedTid").any?
    user_delegation_key.signed_start = xml.SignedStart.text if (xml > "SignedStart").any?
    user_delegation_key.signed_expiry = xml.SignedExpiry.text if (xml > "SignedExpiry").any?
    user_delegation_key.signed_service = xml.SignedService.text if (xml > "SignedService").any?
    user_delegation_key.signed_version = xml.SignedVersion.text if (xml > "SignedVersion").any?
    user_delegation_key.value = xml.Value.text if (xml > "Value").any?
  end
end