public async Task DeleteLocalFilms()
        {
            if (!await _storageService.FileExistsAsync(FilmCacheFile))
            {
                return;
            }

            await _storageService.DeleteFileAsync(FilmCacheFile);

            _filmCache.ClearCache(FilmCacheFile);
        }
Beispiel #2
0
        /// <summary>
        /// Download the selected backup and replace the current data with it.
        /// </summary>
        /// <returns></returns>
        private async Task DownloadBackupAsync()
        {
            BackupIsIndeterminate = true;
            BackupStatusText      = "Perpare download ...";

            bool download = true;

            var dialog = new MessageDialog("Downloading a Backup will replace all local data.", "OneDrive Backup");

            dialog.Commands.Add(new UICommand("Download", null, "Download"));
            dialog.Commands.Add(new UICommand("Cancel", null, "Cancel"));
            if ((await dialog.ShowAsync()).Id.ToString() == "Cancel")
            {
                download = false;
            }

            if (download)
            {
                var localBackup = await localStorageService.GetFileAsync(uploadZip);

                if (localBackup == null)
                {
                    try
                    {
                        await zipArchiveService.CompressAsync(DataPath, uploadZip); // TODO: fix deprecation
                    }
                    catch
                    {
                        // TODO: Add error handling.
                    }
                }

                BackupStatusText = $"Download Backup (total: {SelectedOneDriveItem.Size} bytes) ...";

                try
                {
                    var backupStream = await oneDriveService.GetContentByIdAsync(SelectedOneDriveItem.Id);

                    if (backupStream != null)
                    {
                        // Delete folder
                        await localStorageService.DeleteFolderAsync(DataPath);

                        var folder = await localStorageService.CreateOrGetFileAsync(downloadZip);

                        using (backupStream)
                        {
                            using (var fileStream = await folder.OpenStreamForWriteAsync())
                            {
                                backupStream.Position = 0;
                                fileStream.Position   = 0;
                                await backupStream.CopyToAsync(fileStream);
                            }
                        }

                        try
                        {
                            await zipArchiveService.UncompressAsync(downloadZip); // TODO: fix deprecation
                        }
                        catch
                        {
                            await localStorageService.DeleteFolderAsync(DataPath);

                            await zipArchiveService.UncompressAsync(uploadZip); // TODO: fix deprecation

                            await new MessageDialog("An error occured while saving the backup. Old data have been restored.", "OneDrive Backup").ShowAsync();
                        }
                    }
                    else
                    {
                        await new MessageDialog("An error occured while downloading a backup. Please try again later.", "OneDrive Backup").ShowAsync();
                    }
                }
                catch (Exception)
                {
                    await new MessageDialog("An error occured while downloading a backup. Please try again later.", "OneDrive Backup").ShowAsync();
                }
            }

            await localStorageService.DeleteFileAsync(downloadZip);

            SelectedOneDriveItem = null;

            BackupIsIndeterminate = false;
            BackupStatusText      = string.Empty;
        }