Ejemplo n.º 1
0
        async Task ExecuteAddBlobCommandAsync()
        {
            if (IsBusy)
            {
                return;
            }

            IProgressDialog savingDialog = null;

            try
            {
                //Check for Storage Permissions
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

                if (status != PermissionStatus.Granted)
                {
                    if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
                    {
                        MessagingService.Current.SendMessage <MessagingServiceAlert>(MessageKeys.Message, new MessagingServiceAlert
                        {
                            Title   = "Need storage access",
                            Message = "In order to upload new blobs, we need access to your phone's storage.",
                            Cancel  = "OK"
                        });
                    }

                    var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Storage });

                    status = results[Permission.Storage];
                }

                if (status == PermissionStatus.Granted)
                {
                    var result = await DependencyService.Get <IFilePicker>().PickFile();

                    if (result != null)
                    {
                        savingDialog = UserDialogs.Loading("Saving Blob");
                        savingDialog.Show();

                        var blob = Container.BaseContainer.GetBlockBlobReference(result.FileName);
                        if (await blob.ExistsAsync())
                        {
                            savingDialog.Hide();
                            MessagingService.Current.SendMessage <MessagingServiceQuestion>(MessageKeys.Question, new MessagingServiceQuestion
                            {
                                Negative    = "No",
                                Positive    = "Yes",
                                Question    = "A blob with the same name already exists in this Container, would you like to overwrite?",
                                Title       = "Overwrite blob?",
                                OnCompleted = (async(questionResult) =>
                                {
                                    if (questionResult)
                                    {
                                        try
                                        {
                                            savingDialog.Show();
                                            await blob.FetchAttributesAsync();
                                            byteCount -= blob.Properties.Length;
                                            await blob.UploadFromByteArrayAsync(result.DataArray, 0, result.DataArray.Length);
                                            byteCount += blob.Properties.Length;
                                            TotalBlobSize = FileSizeHelper.GetHumanizedSizeFromBytes(byteCount);
                                            App.Logger.Track(AppEvent.OverwriteBlob.ToString());
                                        }
                                        catch (Exception ex)
                                        {
                                            Logger.Report(ex);
                                            MessagingService.Current.SendMessage <MessagingServiceAlert>(MessageKeys.Message, new MessagingServiceAlert
                                            {
                                                Title = "Unable to Overwrite Blob",
                                                Message = "There was an issue trying to overwrite blob in storage.  Please try again.",
                                                Cancel = "OK"
                                            });
                                        }
                                        finally
                                        {
                                            if (savingDialog != null)
                                            {
                                                if (savingDialog.IsShowing)
                                                {
                                                    savingDialog.Hide();
                                                }
                                                savingDialog.Dispose();
                                            }
                                        }
                                    }
                                }
                                               )
                            });
                        }
                        else
                        {
                            await blob.UploadFromByteArrayAsync(result.DataArray, 0, result.DataArray.Length);

                            App.Logger.Track(AppEvent.CreatedBlob.ToString());

                            Blobs.Add((CloudBlob)blob);
                            byteCount += blob.Properties.Length;

                            BlobCount     = Blobs.Count.ToString();
                            TotalBlobSize = FileSizeHelper.GetHumanizedSizeFromBytes(byteCount);

                            SortBlobs();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Report(ex);
                MessagingService.Current.SendMessage <MessagingServiceAlert>(MessageKeys.Message, new MessagingServiceAlert
                {
                    Title   = "Unable to Save Blob",
                    Message = "There was an issue trying to save to blob storage.  Please try again.",
                    Cancel  = "OK"
                });
            }
            finally
            {
                if (savingDialog != null)
                {
                    if (savingDialog.IsShowing)
                    {
                        savingDialog.Hide();
                    }
                    savingDialog.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        async Task ExecuteTapBlobCommandAsync(CloudBlob blob)
        {
            if (blob == null)
            {
                return;
            }

            MessagingService.Current.Subscribe <MessageArgsDeleteBlob>(MessageKeys.DeleteBlob, async(m, argsDeleteBlob) =>
            {
                Navigation.PopAsync();
                IProgressDialog deletingDialog = UserDialogs.Loading("Deleting Blob");
                deletingDialog.Show();
                try
                {
                    var aseBlob = Blobs.Where(b => b.Name == argsDeleteBlob.BlobName &&
                                              b.Container.Name == argsDeleteBlob.ContainerName).FirstOrDefault();
                    if (aseBlob == null)
                    {
                        return;
                    }
                    await aseBlob.DeleteAsync();

                    App.Logger.Track(AppEvent.DeleteBlob.ToString());

                    Blobs.Remove(aseBlob);

                    byteCount    -= aseBlob.Properties.Length;
                    BlobCount     = Blobs.Count.ToString();
                    TotalBlobSize = FileSizeHelper.GetHumanizedSizeFromBytes(byteCount);

                    SortBlobs();
                }
                catch (Exception ex)
                {
                    Logger.Report(ex, "Method", "HandleMessageArgsDeleteBlob");
                    MessagingService.Current.SendMessage(MessageKeys.Error, ex);
                }
                finally
                {
                    if (deletingDialog != null)
                    {
                        if (deletingDialog.IsShowing)
                        {
                            deletingDialog.Hide();
                        }
                        deletingDialog.Dispose();
                    }
                }
            });

            try
            {
                var blobDetailsPage = new BlobDetailsPage(blob);

                App.Logger.TrackPage(AppPage.BlobDetails.ToString());
                await NavigationService.PushAsync(Navigation, blobDetailsPage);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Ex: " + ex.Message);
            }
        }