/// <summary> /// Downloads all required packages to update to a specified update target. /// </summary> /// <param name="username">The username.</param> /// <param name="token">The login token.</param> /// <param name="target">The update target.</param> /// <param name="progress">A progress object used to report the progress of the operation.</param> /// <param name="cancellationToken">A cancelation token that can be used to cancel the operation.</param> /// <returns>Returns a list of update package files.</returns> private static async Task <List <FileInfo> > DownloadUpdatePackagesAsync(string username, string token, UpdateTarget target, IProgress <double> progress, CancellationToken cancellationToken) { var packageFiles = new List <FileInfo>(); try { int stepCount = target.Steps.Count; int counter = 0; foreach (var step in target.Steps) { if (cancellationToken.IsCancellationRequested) { break; } var subProgress = new Progress <double>(value => progress.Report((1.0 / stepCount) * counter + (value / stepCount))); var packageFile = await UpdateWebsite.DownloadUpdateStepAsync(username, token, step, subProgress, cancellationToken); if (packageFile != null) { packageFiles.Add(packageFile); } counter++; } progress.Report(1); if (cancellationToken.IsCancellationRequested) { foreach (var file in packageFiles) { if (file.Exists) { file.Delete(); } } return(null); } return(packageFiles); } catch (Exception) { foreach (var file in packageFiles) { if (file.Exists) { file.Delete(); } } throw; } }
public async Task HandleAsync_WithCorrectCommand_ShouldUpdateWebsite() { // Arrange var websiteId = Guid.NewGuid(); SeedWebsite(websiteId); IServiceScope scope = CreateScope(); var handler = scope.ServiceProvider.GetService <IRequestHandler <UpdateWebsite, OperationResult <bool> > >(); // Act var request = new UpdateWebsite(websiteId, "mySiteEdited", "www.mysiteedited.com", new List <string> { "edit" }, new ImageManipulation("myImageEdited.png", "image/png", new byte[42]), "*****@*****.**", "654321"); var updateWebsiteOperation = await handler.Handle(request, CancellationToken.None); // Assert updateWebsiteOperation.IsSuccessful.Should().BeTrue(); updateWebsiteOperation.Errors.Should().BeNull(); updateWebsiteOperation.Should().BeOfType(typeof(OperationResult <bool>)); Website actualWebsite; using (var db = scope.ServiceProvider.GetService <WebsiteManagementDbContext>()) { db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; actualWebsite = db.Websites.Include(x => x.Image) .Include(x => x.Categories) .SingleOrDefault(x => x.Id == websiteId); } actualWebsite.Name.Should().Be("mySiteEdited"); actualWebsite.Url.Should().Be("www.mysiteedited.com"); actualWebsite.Email.Should().Be("*****@*****.**"); actualWebsite.Password.Should().Be("fN/7PRJLJ5F8/EHLJQJ5tA=="); actualWebsite.Image.Name.Should().Be("myImageEdited.png"); actualWebsite.Image.MimeType.Should().Be("image/png"); actualWebsite.Image.Blob.Length.Should().Be(42); actualWebsite.Categories.Count.Should().Be(1); actualWebsite.Categories[0].Value.Should().Be("edit"); actualWebsite.IsDeleted.Should().BeFalse(); }
private async Task UpdateSelectedVersion() { string token; if (GlobalCredentials.Instance.LogIn(Window, out token)) { UpdateInfo updateInfo = UpdateWebsite.GetUpdateInfo(GlobalCredentials.Instance.Username, token); List <UpdateStep> updateSteps = updateInfo.Package.Where(step => step.From >= SelectedVersion.Version).ToList(); if (updateSteps.Count > 0) { List <UpdateTarget> targets = FactorioUpdater.GetUpdateTargets(SelectedVersion, FactorioVersions, updateSteps); var updateListWindow = new UpdateListWindow() { Owner = Window }; var updateListViewModel = (UpdateListViewModel)updateListWindow.ViewModel; updateListViewModel.UpdateTargets = targets; bool?result = updateListWindow.ShowDialog(); if (result.HasValue && result.Value) { UpdateTarget target = updateListViewModel.SelectedTarget; var progressWindow = new ProgressWindow { Owner = Window }; var progressViewModel = (ProgressViewModel)progressWindow.ViewModel; progressViewModel.ActionName = App.Instance.GetLocalizedResourceString("UpdatingFactorioAction"); var cancellationSource = new CancellationTokenSource(); progressViewModel.CancelRequested += (sender, e) => cancellationSource.Cancel(); var progress = new Progress <double>(value => progressViewModel.Progress = value); var stageProgress = new Progress <UpdaterStageInfo>(value => { progressViewModel.CanCancel = value.CanCancel; progressViewModel.ProgressDescription = value.Description; }); try { Task closeWindowTask = null; try { Task updateTask = FactorioUpdater.ApplyUpdateAsync(SelectedVersion, GlobalCredentials.Instance.Username, token, target, progress, stageProgress, cancellationSource.Token); closeWindowTask = updateTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close)); progressWindow.ShowDialog(); await updateTask; } finally { if (closeWindowTask != null) { await closeWindowTask; } } } catch (HttpRequestException) { MessageBox.Show(Window, App.Instance.GetLocalizedMessage("InternetConnection", MessageType.Error), App.Instance.GetLocalizedMessageTitle("InternetConnection", MessageType.Error), MessageBoxButton.OK, MessageBoxImage.Error); } catch (CriticalUpdaterException) { MessageBox.Show(Window, App.Instance.GetLocalizedMessage("FactorioUpdaterCritical", MessageType.Error), App.Instance.GetLocalizedMessageTitle("FactorioUpdaterCritical", MessageType.Error), MessageBoxButton.OK, MessageBoxImage.Error); } } } else { MessageBox.Show(Window, App.Instance.GetLocalizedMessage("NoFactorioUpdate", MessageType.Information), App.Instance.GetLocalizedMessageTitle("NoFactorioUpdate", MessageType.Information), MessageBoxButton.OK, MessageBoxImage.Information); } } }
private async Task <List <UpdateStep> > GetUpdateSteps(string token) { var updateInfo = await UpdateWebsite.GetUpdateInfoAsync(GlobalCredentials.Instance.Username, token); return(updateInfo.Package.Where(step => step.From >= SelectedVersion.Version).ToList()); }