From dfe92218f4fc291e176284a13911aa0d6d0f0ae9 Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Tue, 28 Jun 2016 20:08:09 -0400 Subject: [PATCH] Add download input link --- Core/Entities/LeafInput.cs | 21 +++++++++++++++++++++ Web/Controllers/ResultsAdminController.cs | 19 +++++++++++++++++++ Web/Views/ResultsAdmin/Index.cshtml | 3 ++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Core/Entities/LeafInput.cs b/Core/Entities/LeafInput.cs index 565fbb7..36dfb26 100644 --- a/Core/Entities/LeafInput.cs +++ b/Core/Entities/LeafInput.cs @@ -72,6 +72,27 @@ namespace LeafWeb.Core.Entities return compressedFileStream.ToArray(); } } + + /// + /// Contains all input files in a zip + /// + public byte[] GetInputFileZip() + { + using (var compressedFileStream = new MemoryStream()) + { + using (var archive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, true)) + { + foreach (var inputFile in InputFiles) + { + var entry = archive.CreateEntry(inputFile.Filename); + using (var originalFileStream = new MemoryStream(inputFile.Contents)) + using (var entryStream = entry.Open()) + originalFileStream.CopyTo(entryStream); + } + } + return compressedFileStream.ToArray(); + } + } public int GetOutputFileSizeSum() { diff --git a/Web/Controllers/ResultsAdminController.cs b/Web/Controllers/ResultsAdminController.cs index 405c183..3656105 100644 --- a/Web/Controllers/ResultsAdminController.cs +++ b/Web/Controllers/ResultsAdminController.cs @@ -28,6 +28,11 @@ namespace LeafWeb.Web.Controllers return View(viewModel); } + public ActionResult DownloadInput(int id) + { + return GetInputZip(id); + } + public ActionResult DownloadOutputToUser(int id) { return GetOutputZip(id, LeafOutputFileType.ToUser); @@ -57,6 +62,20 @@ namespace LeafWeb.Web.Controllers return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; } + private ActionResult GetInputZip(int id) + { + var leafInput = DataService.GetLeafInput(id); + + if (leafInput == null) + return View("DownloadNotFound"); + + var zip = leafInput.GetInputFileZip(); + + var filename = $"{leafInput.Identifier.FilterValidFilename()}_Input.zip"; + + return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; + } + public ActionResult Delete(int id) { var leafInput = DataService.GetLeafInput(id); diff --git a/Web/Views/ResultsAdmin/Index.cshtml b/Web/Views/ResultsAdmin/Index.cshtml index 5b9bc23..816eaab 100644 --- a/Web/Views/ResultsAdmin/Index.cshtml +++ b/Web/Views/ResultsAdmin/Index.cshtml @@ -20,7 +20,8 @@ grid.Column("Total Results: " + Model.Count(), format: @
@Html.ActionLink("Details", "Details", new { id = item.LeafInputId }, new { @class = "btn btn-default btn-xs", role = "button" }) - @Html.ActionLink("Download", "DownloadOutputToUser", new { id = item.LeafInputId }, new { @class = "btn btn-default btn-xs", role = "button" }) + @Html.ActionLink("Dwnld", "DownloadOutputToUser", new { id = item.LeafInputId }, new { @class = "btn btn-default btn-xs", role = "button" }) + @Html.ActionLink("Dwnld Input", "DownloadInput", new { id = item.LeafInputId }, new { @class = "btn btn-default btn-xs", role = "button" }) @Html.ActionLink("Delete", "Delete", new { id = item.LeafInputId }, new { @class = "btn btn-default btn-xs", role="button" })
) ),