@@ -231,56 +231,81 @@ protected virtual void DisplayUploadState(UploadState uploadState) {
231
231
}
232
232
}
233
233
234
- // Upload file text to Cloud Storage using a byte array.
235
- protected IEnumerator UploadBytes ( ) {
234
+ protected Task < StorageMetadata > UploadBytesAsync ( ) {
236
235
var storageReference = GetStorageReference ( ) ;
237
236
DebugLog ( String . Format ( "Uploading to {0} ..." , storageReference . Path ) ) ;
238
- var task = storageReference . PutBytesAsync (
237
+ return storageReference . PutBytesAsync (
239
238
Encoding . UTF8 . GetBytes ( fileContents ) , StringToMetadataChange ( fileMetadataChangeString ) ,
240
239
new StorageProgress < UploadState > ( DisplayUploadState ) ,
241
- cancellationTokenSource . Token , null ) ;
240
+ cancellationTokenSource . Token , null ) . ContinueWithOnMainThread ( task => {
241
+ DisplayUploadComplete ( task ) ;
242
+ return task ;
243
+ } ) . Unwrap ( ) ;
244
+ }
245
+
246
+ // Upload file text to Cloud Storage using a byte array.
247
+ protected IEnumerator UploadBytes ( ) {
248
+ var task = UploadBytesAsync ( ) ;
242
249
yield return new WaitForTaskCompletion ( this , task ) ;
243
- DisplayUploadComplete ( task ) ;
244
250
}
245
251
246
- // Upload file to Cloud Storage using a stream.
247
- protected IEnumerator UploadStream ( ) {
252
+ protected Task < StorageMetadata > UploadStreamAsync ( ) {
248
253
var storageReference = GetStorageReference ( ) ;
249
254
DebugLog ( String . Format ( "Uploading to {0} using stream..." , storageReference . Path ) ) ;
250
- var task = storageReference . PutStreamAsync (
255
+ return storageReference . PutStreamAsync (
251
256
new MemoryStream ( System . Text . Encoding . ASCII . GetBytes ( fileContents ) ) ,
252
257
StringToMetadataChange ( fileMetadataChangeString ) ,
253
258
new StorageProgress < UploadState > ( DisplayUploadState ) ,
254
- cancellationTokenSource . Token , null ) ;
259
+ cancellationTokenSource . Token , null ) . ContinueWithOnMainThread ( task => {
260
+ DisplayUploadComplete ( task ) ;
261
+ return task ;
262
+ } ) . Unwrap ( ) ;
263
+ }
264
+
265
+
266
+ // Upload file to Cloud Storage using a stream.
267
+ protected IEnumerator UploadStream ( ) {
268
+ var task = UploadStreamAsync ( ) ;
255
269
yield return new WaitForTaskCompletion ( this , task ) ;
256
- DisplayUploadComplete ( task ) ;
257
270
}
258
271
259
- // Upload a file from the local filesystem to Cloud Storage.
260
- protected IEnumerator UploadFromFile ( ) {
272
+ protected Task < StorageMetadata > UploadFromFileAsync ( ) {
261
273
var localFilenameUriString = PathToPersistentDataPathUriString ( localFilename ) ;
262
274
var storageReference = GetStorageReference ( ) ;
263
275
DebugLog ( String . Format ( "Uploading '{0}' to '{1}'..." , localFilenameUriString ,
264
276
storageReference . Path ) ) ;
265
- var task = storageReference . PutFileAsync (
277
+ return storageReference . PutFileAsync (
266
278
localFilenameUriString , StringToMetadataChange ( fileMetadataChangeString ) ,
267
279
new StorageProgress < UploadState > ( DisplayUploadState ) ,
268
- cancellationTokenSource . Token , null ) ;
280
+ cancellationTokenSource . Token , null ) . ContinueWithOnMainThread ( task => {
281
+ DisplayUploadComplete ( task ) ;
282
+ return task ;
283
+ } ) . Unwrap ( ) ;
284
+ }
285
+
286
+ // Upload a file from the local filesystem to Cloud Storage.
287
+ protected IEnumerator UploadFromFile ( ) {
288
+ var task = UploadFromFileAsync ( ) ;
269
289
yield return new WaitForTaskCompletion ( this , task ) ;
270
- DisplayUploadComplete ( task ) ;
271
290
}
272
291
273
- // Update the metadata on the file in Cloud Storage.
274
- protected IEnumerator UpdateMetadata ( ) {
292
+ protected Task < StorageMetadata > UpdateMetadataAsync ( ) {
275
293
var storageReference = GetStorageReference ( ) ;
276
294
DebugLog ( String . Format ( "Updating metadata of {0} ..." , storageReference . Path ) ) ;
277
- var task = storageReference . UpdateMetadataAsync ( StringToMetadataChange (
278
- fileMetadataChangeString ) ) ;
295
+ return storageReference . UpdateMetadataAsync ( StringToMetadataChange (
296
+ fileMetadataChangeString ) ) . ContinueWithOnMainThread ( task => {
297
+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
298
+ DebugLog ( "Updated metadata" ) ;
299
+ DebugLog ( MetadataToString ( task . Result , false ) + "\n " ) ;
300
+ }
301
+ return task ;
302
+ } ) . Unwrap ( ) ;
303
+ }
304
+
305
+ // Update the metadata on the file in Cloud Storage.
306
+ protected IEnumerator UpdateMetadata ( ) {
307
+ var task = UpdateMetadataAsync ( ) ;
279
308
yield return new WaitForTaskCompletion ( this , task ) ;
280
- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
281
- DebugLog ( "Updated metadata" ) ;
282
- DebugLog ( MetadataToString ( task . Result , false ) + "\n " ) ;
283
- }
284
309
}
285
310
286
311
// Write download state to the log.
@@ -291,28 +316,33 @@ protected virtual void DisplayDownloadState(DownloadState downloadState) {
291
316
}
292
317
}
293
318
294
- // Download from Cloud Storage into a byte array.
295
- protected IEnumerator DownloadBytes ( ) {
319
+ protected Task < byte [ ] > DownloadBytesAsync ( ) {
296
320
var storageReference = GetStorageReference ( ) ;
297
321
DebugLog ( String . Format ( "Downloading {0} ..." , storageReference . Path ) ) ;
298
- var task = storageReference . GetBytesAsync (
322
+ return storageReference . GetBytesAsync (
299
323
0 , new StorageProgress < DownloadState > ( DisplayDownloadState ) ,
300
- cancellationTokenSource . Token ) ;
324
+ cancellationTokenSource . Token ) . ContinueWithOnMainThread ( task => {
325
+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
326
+ DebugLog ( "Finished downloading bytes" ) ;
327
+ fileContents = System . Text . Encoding . Default . GetString ( task . Result ) ;
328
+ DebugLog ( String . Format ( "File Size {0} bytes\n " , fileContents . Length ) ) ;
329
+ }
330
+ return task ;
331
+ } ) . Unwrap ( ) ;
332
+ }
333
+
334
+ // Download from Cloud Storage into a byte array.
335
+ protected IEnumerator DownloadBytes ( ) {
336
+ var task = DownloadBytesAsync ( ) ;
301
337
yield return new WaitForTaskCompletion ( this , task ) ;
302
- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
303
- DebugLog ( "Finished downloading bytes" ) ;
304
- fileContents = System . Text . Encoding . Default . GetString ( task . Result ) ;
305
- DebugLog ( String . Format ( "File Size {0} bytes\n " , fileContents . Length ) ) ;
306
- }
307
338
}
308
339
309
- // Download from Cloud Storage using a stream.
310
- protected IEnumerator DownloadStream ( ) {
340
+ protected Task DownloadStreamAsync ( ) {
311
341
// Download the file using a stream.
312
342
fileContents = "" ;
313
343
var storageReference = GetStorageReference ( ) ;
314
344
DebugLog ( String . Format ( "Downloading {0} with stream ..." , storageReference . Path ) ) ;
315
- var task = storageReference . GetStreamAsync ( ( stream ) => {
345
+ return storageReference . GetStreamAsync ( ( stream ) => {
316
346
var buffer = new byte [ 1024 ] ;
317
347
int read ;
318
348
// Read data to render in the text view.
@@ -321,52 +351,70 @@ protected IEnumerator DownloadStream() {
321
351
}
322
352
} ,
323
353
new StorageProgress < DownloadState > ( DisplayDownloadState ) ,
324
- cancellationTokenSource . Token ) ;
354
+ cancellationTokenSource . Token ) . ContinueWithOnMainThread ( task => {
355
+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
356
+ DebugLog ( "Finished downloading stream\n " ) ;
357
+ }
358
+ return task ;
359
+ } ) . Unwrap ( ) ;
360
+ }
325
361
362
+ // Download from Cloud Storage using a stream.
363
+ protected IEnumerator DownloadStream ( ) {
364
+ var task = DownloadStreamAsync ( ) ;
326
365
yield return new WaitForTaskCompletion ( this , task ) ;
327
- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
328
- DebugLog ( "Finished downloading stream\n " ) ;
329
- }
366
+
330
367
}
331
368
332
369
// Get a local filesystem path from a file:// URI.
333
370
protected string FileUriStringToPath ( string fileUriString ) {
334
371
return Uri . UnescapeDataString ( ( new Uri ( fileUriString ) ) . PathAndQuery ) ;
335
372
}
336
373
337
- // Download from Cloud Storage to a local file.
338
- protected IEnumerator DownloadToFile ( ) {
374
+ protected Task DownloadToFileAsync ( ) {
339
375
var storageReference = GetStorageReference ( ) ;
340
376
var localFilenameUriString = PathToPersistentDataPathUriString ( localFilename ) ;
341
377
DebugLog ( String . Format ( "Downloading {0} to {1}..." , storageReference . Path ,
342
378
localFilenameUriString ) ) ;
343
- var task = storageReference . GetFileAsync (
379
+ return storageReference . GetFileAsync (
344
380
localFilenameUriString ,
345
381
new StorageProgress < DownloadState > ( DisplayDownloadState ) ,
346
- cancellationTokenSource . Token ) ;
347
- yield return new WaitForTaskCompletion ( this , task ) ;
348
- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
349
- var filename = FileUriStringToPath ( localFilenameUriString ) ;
350
- DebugLog ( String . Format ( "Finished downloading file {0} ({1})" , localFilenameUriString ,
351
- filename ) ) ;
352
- DebugLog ( String . Format ( "File Size {0} bytes\n " , ( new FileInfo ( filename ) ) . Length ) ) ;
353
- fileContents = File . ReadAllText ( filename ) ;
354
- }
382
+ cancellationTokenSource . Token ) . ContinueWithOnMainThread ( task => {
383
+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
384
+ var filename = FileUriStringToPath ( localFilenameUriString ) ;
385
+ DebugLog ( String . Format ( "Finished downloading file {0} ({1})" , localFilenameUriString ,
386
+ filename ) ) ;
387
+ DebugLog ( String . Format ( "File Size {0} bytes\n " , ( new FileInfo ( filename ) ) . Length ) ) ;
388
+ fileContents = File . ReadAllText ( filename ) ;
389
+ }
390
+ return task ;
391
+ } ) . Unwrap ( ) ;
355
392
}
356
393
357
- // Delete a remote file.
358
- protected IEnumerator Delete ( ) {
394
+ // Download from Cloud Storage to a local file.
395
+ protected IEnumerator DownloadToFile ( ) {
396
+ var task = DownloadToFileAsync ( ) ;
397
+ yield return new WaitForTaskCompletion ( this , task ) ;
398
+ }
399
+
400
+ protected Task DeleteAsync ( ) {
359
401
var storageReference = GetStorageReference ( ) ;
360
402
DebugLog ( String . Format ( "Deleting {0}..." , storageReference . Path ) ) ;
361
- var task = storageReference . DeleteAsync ( ) ;
403
+ return storageReference . DeleteAsync ( ) . ContinueWithOnMainThread ( task => {
404
+ if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
405
+ DebugLog ( String . Format ( "{0} deleted" , storageReference . Path ) ) ;
406
+ }
407
+ return task ;
408
+ } ) . Unwrap ( ) ;
409
+ }
410
+
411
+ // Delete a remote file.
412
+ protected IEnumerator Delete ( ) {
413
+ var task = DeleteAsync ( ) ;
362
414
yield return new WaitForTaskCompletion ( this , task ) ;
363
- if ( ! ( task . IsFaulted || task . IsCanceled ) ) {
364
- DebugLog ( String . Format ( "{0} deleted" , storageReference . Path ) ) ;
365
- }
366
415
}
367
416
368
- // Download and display Metadata for the storage reference.
369
- protected IEnumerator GetMetadata ( ) {
417
+ protected Task < StorageMetadata > GetMetadataAsync ( ) {
370
418
var storageReference = GetStorageReference ( ) ;
371
419
DebugLog ( String . Format ( "Bucket: {0}" , storageReference . Bucket ) ) ;
372
420
DebugLog ( String . Format ( "Path: {0}" , storageReference . Path ) ) ;
@@ -375,10 +423,17 @@ protected IEnumerator GetMetadata() {
375
423
storageReference . Parent . Path : "(root)" ) ) ;
376
424
DebugLog ( String . Format ( "Root Path: {0}" , storageReference . Root . Path ) ) ;
377
425
DebugLog ( String . Format ( "App: {0}" , storageReference . Storage . App . Name ) ) ;
378
- var task = storageReference . GetMetadataAsync ( ) ;
426
+ return storageReference . GetMetadataAsync ( ) . ContinueWithOnMainThread ( task => {
427
+ if ( ! ( task . IsFaulted || task . IsCanceled ) )
428
+ DebugLog ( MetadataToString ( task . Result , false ) + "\n " ) ;
429
+ return task ;
430
+ } ) . Unwrap ( ) ;
431
+ }
432
+
433
+ // Download and display Metadata for the storage reference.
434
+ protected IEnumerator GetMetadata ( ) {
435
+ var task = GetMetadataAsync ( ) ;
379
436
yield return new WaitForTaskCompletion ( this , task ) ;
380
- if ( ! ( task . IsFaulted || task . IsCanceled ) )
381
- DebugLog ( MetadataToString ( task . Result , false ) + "\n " ) ;
382
437
}
383
438
384
439
// Display the download URL for a storage reference.
0 commit comments