From 25b5d8ebc6b0983782641e1288ef0ca081922c9a Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Tue, 21 Jul 2020 22:02:55 -0400 Subject: [PATCH] Move downloads to new controller --- WebCms/Controllers/DownloadController.cs | 111 ++++++++++++++++++ WebCms/Controllers/QueueController.cs | 78 +----------- WebCms/Controllers/ResultsController.cs | 18 +-- WebCms/Views/Queue/Details.cshtml | 6 +- WebCms/Views/Queue/Index.cshtml | 4 +- WebCms/Views/Results/Details.cshtml | 4 +- .../{Queue => Shared}/DownloadNotFound.cshtml | 0 WebCms/Views/Shared/_LeafInputActions.cshtml | 4 +- WebCms/Web.config | 2 +- WebCms/WebCms.csproj | 3 +- 10 files changed, 126 insertions(+), 104 deletions(-) create mode 100644 WebCms/Controllers/DownloadController.cs rename WebCms/Views/{Queue => Shared}/DownloadNotFound.cshtml (100%) diff --git a/WebCms/Controllers/DownloadController.cs b/WebCms/Controllers/DownloadController.cs new file mode 100644 index 0000000..3437692 --- /dev/null +++ b/WebCms/Controllers/DownloadController.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Web.Mvc; +using LeafWeb.Core.Entities; +using LeafWeb.Core.Utility; +using LeafWeb.WebCms.Models; +using LeafWeb.WebCms.Utility; +using Umbraco.Web.Mvc; + +namespace LeafWeb.WebCms.Controllers +{ + public class DownloadController : BaseController + { + [ActionLog] + public ActionResult Results(string token) + { + var leafInput = DataService.GetLeafInput(token); + + if (leafInput == null) + return View("DownloadNotFound"); + + var zip = leafInput.GetOutputFileZip(LeafOutputFileType.ToUser); + + var filename = leafInput.Identifier.FilterValidFilename() + ".zip"; + + return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; + } + + [MemberAuthorize(AllowGroup = "Authenticated,Administrator")] + public ActionResult Input(int id) + { + return GetInputZip(id); + } + + [MemberAuthorize(AllowGroup = "Authenticated,Administrator")] + public ActionResult OutputToUser(int id) + { + return GetOutputZip(id, LeafOutputFileType.ToUser); + } + + [MemberAuthorize(AllowGroup = "Administrator")] + public ActionResult OutputNotToUser(int id) + { + return GetOutputZip(id, LeafOutputFileType.NotToUser); + } + + [MemberAuthorize(AllowGroup = "Authenticated,Administrator")] + public ActionResult OutputCleanedInput(int id) + { + return GetOutputZip(id, LeafOutputFileType.CleanedInput); + } + + [MemberAuthorize(AllowGroup = "Authenticated,Administrator")] + public ActionResult ResultsInputZip(LeafDataQuery query) + { + return GetResults(query, LeafInput.GetInputFilesZip, $"LeafWeb_{DateTime.Now:yyyy-dd-MM--HH-mm-ss}_Input.zip"); + } + + [MemberAuthorize(AllowGroup = "Authenticated,Administrator")] + public ActionResult ResultsOutputZip(LeafDataQuery query) + { + return GetResults(query, LeafInput.GetOutputFilesZip_ToUser, $"LeafWeb_{DateTime.Now:yyyy-dd-MM--HH-mm-ss}_Output.zip"); + } + + private ActionResult GetOutputZip(int id, LeafOutputFileType type) + { + var leafInput = DataService.GetLeafInput(id); + + if (leafInput == null) + return View("DownloadNotFound"); + + var zip = leafInput.GetOutputFileZip(type); + + var filename = $"{leafInput.Identifier.FilterValidFilename()}_{type}.zip"; + + 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 }; + } + + private ActionResult GetResults(LeafDataQuery query, Func, byte[]> getZip, string filename) + { + var resultItems = + DataService.GetLeafInputsOrdered(); + + resultItems + = QueryFilter.Search(resultItems, query, Members.GetCurrentLoginStatus()?.Email); + + if (resultItems == null) + return View("DownloadNotFound"); + + var zip = getZip(resultItems); + + //var filename = $"LeafWeb_{DateTime.Now:yyyy-dd-MM--HH-mm-ss}_Input.zip"; + + return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; + } + } +} \ No newline at end of file diff --git a/WebCms/Controllers/QueueController.cs b/WebCms/Controllers/QueueController.cs index d526379..fa7580c 100644 --- a/WebCms/Controllers/QueueController.cs +++ b/WebCms/Controllers/QueueController.cs @@ -108,83 +108,7 @@ namespace LeafWeb.WebCms.Controllers return View(viewModel); } - public ActionResult DownloadInput(int id) - { - return GetInputZip(id); - } - - public ActionResult DownloadOutputToUser(int id) - { - return GetOutputZip(id, LeafOutputFileType.ToUser); - } - - public ActionResult DownloadOutputNotToUser(int id) - { - return GetOutputZip(id, LeafOutputFileType.NotToUser); - } - - public ActionResult DownloadOutputCleanedInput(int id) - { - return GetOutputZip(id, LeafOutputFileType.CleanedInput); - } - - public ActionResult DownloadResultsInputZip(LeafDataQuery query) - { - return GetResults(query, LeafInput.GetInputFilesZip, $"LeafWeb_{DateTime.Now:yyyy-dd-MM--HH-mm-ss}_Input.zip"); - } - - public ActionResult DownloadResultsOutputZip(LeafDataQuery query) - { - return GetResults(query, LeafInput.GetOutputFilesZip_ToUser, $"LeafWeb_{DateTime.Now:yyyy-dd-MM--HH-mm-ss}_Output.zip"); - } - - private ActionResult GetOutputZip(int id, LeafOutputFileType type) - { - var leafInput = DataService.GetLeafInput(id); - - if (leafInput == null) - return View("DownloadNotFound"); - - var zip = leafInput.GetOutputFileZip(type); - - var filename = $"{leafInput.Identifier.FilterValidFilename()}_{type}.zip"; - - 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 }; - } - - private ActionResult GetResults(LeafDataQuery query, Func, byte[]> getZip, string filename) - { - var resultItems = - DataService.GetLeafInputsOrdered(); - - resultItems - = QueryFilter.Search(resultItems, query, Members.GetCurrentLoginStatus()?.Email); - - if (resultItems == null) - return View("DownloadNotFound"); - - var zip = getZip(resultItems); - - //var filename = $"LeafWeb_{DateTime.Now:yyyy-dd-MM--HH-mm-ss}_Input.zip"; - - return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; - } - - [ActionLog] + [ActionLog] public ActionResult Delete(int id) { var leafInput = DataService.GetLeafInput(id); diff --git a/WebCms/Controllers/ResultsController.cs b/WebCms/Controllers/ResultsController.cs index eaf98aa..005d1d7 100644 --- a/WebCms/Controllers/ResultsController.cs +++ b/WebCms/Controllers/ResultsController.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using LeafWeb.Core.Entities; @@ -71,20 +72,5 @@ namespace LeafWeb.WebCms.Controllers select li; return View(viewModel); } - - [ActionLog] - public ActionResult Download(string token) - { - var leafInput = DataService.GetLeafInput(token); - - if (leafInput == null) - return View("DownloadNotFound"); - - var zip = leafInput.GetOutputFileZip(LeafOutputFileType.ToUser); - - var filename = leafInput.Identifier.FilterValidFilename() + ".zip"; - - return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; - } - } + } } \ No newline at end of file diff --git a/WebCms/Views/Queue/Details.cshtml b/WebCms/Views/Queue/Details.cshtml index 828bf02..95a2b46 100644 --- a/WebCms/Views/Queue/Details.cshtml +++ b/WebCms/Views/Queue/Details.cshtml @@ -12,16 +12,16 @@ Download