diff --git a/src/TopoMojo.Api/Features/Admin/AdminController.cs b/src/TopoMojo.Api/Features/Admin/AdminController.cs index ef84f83..b694677 100644 --- a/src/TopoMojo.Api/Features/Admin/AdminController.cs +++ b/src/TopoMojo.Api/Features/Admin/AdminController.cs @@ -84,8 +84,8 @@ public async Task> ImportWorkspaces() public async Task DownloadWorkspaces([FromBody] string[] ids) { string srcPath = fileUploadOptions.TopoRoot; - (var byteArray, var fileName) = await transferSvc.Download(ids, srcPath); - return File(byteArray, "application/zip", fileName); + var stream = await transferSvc.Download(ids, srcPath); + return File(stream, "application/zip", "topomojo-export.zip"); } /// diff --git a/src/TopoMojo.Api/Features/Admin/TransferService.cs b/src/TopoMojo.Api/Features/Admin/TransferService.cs index b732512..3284f91 100644 --- a/src/TopoMojo.Api/Features/Admin/TransferService.cs +++ b/src/TopoMojo.Api/Features/Admin/TransferService.cs @@ -1,534 +1,329 @@ // Copyright 2021 Carnegie Mellon University. All Rights Reserved. // Released under a 3 Clause BSD-style license. See LICENSE.md in the project root for license information. -using System; -using System.Collections.Generic; -using System.IO; using System.IO.Compression; -using System.Linq; using System.Text; using System.Text.Json; -using System.Threading.Tasks; using AutoMapper; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; +using TopoMojo.Api.Data; using TopoMojo.Api.Data.Abstractions; using TopoMojo.Api.Extensions; -namespace TopoMojo.Api.Services +namespace TopoMojo.Api.Services; + +public class TransferService( + IWorkspaceStore workspaceStore, + ITemplateStore templateStore, + ILogger logger, + IMapper mapper, + CoreOptions options + ) : _Service(logger, mapper, options) { - public class TransferService : _Service + private readonly JsonSerializerOptions jsonSerializerSettings = new() { - public TransferService ( - IUserStore userStore, - IWorkspaceStore workspaceStore, - ITemplateStore templateStore, - ILogger logger, - IMapper mapper, - CoreOptions options - ) : base(logger, mapper, options) - { - _workspaceStore = workspaceStore; - _templateStore = templateStore; - _userStore = userStore; - jsonSerializerSettings = new JsonSerializerOptions - { - WriteIndented = true, - ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve - }; - } + WriteIndented = true + }; + + public async Task> Import(string repoPath, string docPath) + { + List results = []; - private readonly IUserStore _userStore; - private readonly IWorkspaceStore _workspaceStore; - private readonly ITemplateStore _templateStore; - private JsonSerializerOptions jsonSerializerSettings; + var files = Directory.GetFiles(repoPath, "*export.zip", SearchOption.TopDirectoryOnly); - public async Task Export(string[] ids, string src, string dest) + foreach (string file in files) { - var list = new List(); + results.AddRange( + await ProcessZipfile(File.OpenRead(file), docPath) + ); - foreach (string id in ids) - { - var topo = await _workspaceStore.LoadWithParents(id); + File.Move(file, file + ".imported"); + } - if (topo != null) - list.Add(topo); + return results; + } - } + public async Task Export(string[] ids, string src, string dest) + { + string fn = DateTimeOffset.UtcNow.ToString("yyyyMMddHHmmss") + "_export.zip"; + string path = Path.Combine(dest, fn); + using FileStream writer = File.Create(path); + await writer.CopyToAsync( + await Download(ids, src) + ); + } - if (list.Count < 1) - return; + public async Task Download(string[] ids, string src) + { + List list = []; + List