Example #1
0
        /// <summary>
        /// Zips the folder to backup and load it to one drive.
        /// </summary>
        /// <returns></returns>
        private async Task UploadBackupAsync()
        {
            BackupIsIndeterminate = true;
            BackupStatusText      = "Create backup ...";

            if (await localStorageService.GetFolderAsync(DataPath) == null)
            {
                throw new Exception("Local data path does not exit.");
            }

            try
            {
                await zipArchiveService.CompressAsync(DataPath, uploadZip);
            }
            catch
            {
                await new MessageDialog("Could not create a backup.", "OneDrive Backup").ShowAsync();
                BackupIsIndeterminate = false;
                BackupStatusText      = string.Empty;

                return;
            }

            var file = await localStorageService.GetFileAsync(uploadZip);

            using (var stream = await file.OpenStreamForReadAsync())
            {
                BackupStatusText = $"Uploading Backup (total: {stream.Length} bytes) ...";
            }

            bool upload = true;

            try
            {
                // Check if an item with the same name already exists.
                var item = await oneDriveService.GetItemByPathAsync(UploadPath);

                if (item != null)
                {
                    var dialog = new MessageDialog($"An backup with the name \"{UploadName}.bak\" already exists.", "OneDrive Backup");
                    dialog.Commands.Add(new UICommand("Override", null, "Override"));
                    dialog.Commands.Add(new UICommand("Cancel", null, "Cancel"));
                    if ((await dialog.ShowAsync()).Id.ToString() == "Cancel")
                    {
                        upload = false;
                    }
                }

                if (upload)
                {
                    var stream = await file.OpenStreamForReadAsync();

                    var result = await oneDriveService.UploadFileByPathAsync(UploadPath, stream);

                    if (result != null)
                    {
                        // TODO: Evaluate if using toast notifications instead.
                        await new MessageDialog("The backup was sucessfully created.", "OneDrive Backup").ShowAsync();
                    }
                    else
                    {
                        await new MessageDialog("An error occured while uploading a backup. Please try again later.", "OneDrive Backup").ShowAsync();
                    }
                }
            }
            catch (Exception)
            {
                await new MessageDialog("An error occured while uploading a backup. Please try again later.", "OneDrive Backup").ShowAsync();
            }

            UploadName            = string.Empty;
            BackupIsIndeterminate = false;
            BackupStatusText      = string.Empty;
        }