async Task <WorkspaceEntryInfo[]> IWorkspacesManager.LoadWorkspace(string workspaceUri, CancellationToken cancellation) { try { SetStatus(WorkspacesManagerStatus.LoadingWorkspace); SetLastError(null); WorkspaceDTO workspace = await backendAccess.GetWorkspace(workspaceUri, cancellation); await LoadEmbeddedStorageEntries(workspace, cancellation); SetCurrentWorkspace(new WorkspaceInfo() { Name = workspace.id, Annotation = workspace.annotation, WebUrl = workspace.selfLaunchUrl }); SetStatus(WorkspacesManagerStatus.LoadingWorkspaceData); await LoadArchivedStorageEntries(workspace.entriesArchiveUrl, cancellation); if (backendAccess.IsConfigured) { SetStatus(WorkspacesManagerStatus.AttachedToDownloadedWorkspace); } else { SetStatus(WorkspacesManagerStatus.Unavailable); } recentlyUsedEntities.RegisterRecentWorkspaceEntry(workspace.selfUrl, workspace.id, workspace.annotation); return(workspace .sources .Select(source => new WorkspaceEntryInfo() { Log = new RecentLogEntry(logProviderFactoryRegistry, source.connectionString, null, null), IsHiddenLog = source.hidden }) .ToArray()); } catch (Exception e) { tracer.Error(e, "failed to load ws '{0}'", workspaceUri); SetLastError(e.Message); if (backendAccess.IsConfigured) { SetStatus(WorkspacesManagerStatus.FailedToDownloadWorkspace); } else { SetStatus(WorkspacesManagerStatus.Unavailable); } return(new WorkspaceEntryInfo[] { }); } }
private async Task LoadEmbeddedStorageEntries(WorkspaceDTO workspace, CancellationToken cancellation) { foreach (var embeddedStorageEntry in workspace.embeddedStorageEntries) { var entry = storageManager.GetEntryById(embeddedStorageEntry.id); foreach (var embeddedStorageSection in embeddedStorageEntry.sections) { using (var sectionStream = new MemoryStream(Convert.FromBase64String(embeddedStorageSection.value))) await entry.LoadSectionFromSnapshot(embeddedStorageSection.id, sectionStream, cancellation); } } }
private async Task <CreatedWorkspaceDTO> CreateWorkspace( string name, string annotation, bool allowOverwrite, List <KeyValuePair <string, Stream> > entriesToArchive) { var dto = new WorkspaceDTO() { id = name, annotation = annotation, allowOverwrite = allowOverwrite }; var sources = logSources.Items.ToArray(); dto.sources.AddRange(sources.Select(source => new WorkspaceDTO.Source() { connectionString = new RecentLogEntry(source.Provider.Factory, source.Provider.ConnectionParams, source.Annotation, null).ToString(), hidden = !source.Visible })); var entries = sources.Select(logSource => logSource.LogSourceSpecificStorageEntry).ToArray(); var entriesArchiveFolderName = tempFilesManager.GenerateNewName(); Directory.CreateDirectory(entriesArchiveFolderName); foreach (var entry in entries) { var dtoEntry = new WorkspaceDTO.EmbeddedStorageEntry() { id = entry.Id }; dto.embeddedStorageEntries.Add(dtoEntry); var sections = entry.EnumSections(CancellationToken.None).ToArray(); foreach (var section in sections) { if (section.Key == "bookmarks" || section.Key == "settings") // todo: hardcoded strings { using (var stream = new MemoryStream()) { await entry.TakeSectionSnapshot(section.Id, stream); dtoEntry.sections.Add(new WorkspaceDTO.EmbeddedStorageSection() { id = section.Id, value = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, Base64FormattingOptions.None) }); } } else { var tempFileStream = new FileStream( Path.Combine(entriesArchiveFolderName, entriesToArchive.Count.ToString()), FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose); await entry.TakeSectionSnapshot(section.Id, tempFileStream); entriesToArchive.Add(new KeyValuePair <string, Stream>( entry.Id + Path.DirectorySeparatorChar + section.Id, tempFileStream)); } } } var createdWs = await backendAccess.CreateWorkspace(dto); return(createdWs); }