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 Microsoft.Ajax.Utilities; 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) { var leafInput = DataService.GetLeafInput(id); // check leafinput email matches current user if (!Permissions.IsCurrentUserAdministrator() && !leafInput.DoesBelongToUser(HttpContext.User.Identity.Name)) { return View("PermissionDenied"); } return GetInputZip(leafInput); } [MemberAuthorize(AllowGroup = "Authenticated,Administrator")] public ActionResult OutputToUser(int id) { var leafInput = DataService.GetLeafInput(id); // check leafinput email matches current user if (!Permissions.IsCurrentUserAdministrator() && !leafInput.DoesBelongToUser(HttpContext.User.Identity.Name)) { return View("PermissionDenied"); } return GetOutputZip(leafInput, LeafOutputFileType.ToUser); } [MemberAuthorize(AllowGroup = "Administrator")] public ActionResult OutputNotToUser(int id) { return GetOutputZip(id, LeafOutputFileType.NotToUser); } [MemberAuthorize(AllowGroup = "Administrator")] public ActionResult OutputCleanedInput(int id) { return GetOutputZip(id, LeafOutputFileType.CleanedInput); } [MemberAuthorize(AllowGroup = "Administrator")] public ActionResult ResultsInputZip(LeafDataQuery query) { return GetResults(query, LeafInput.GetInputFilesZip, $"LeafWeb_{DateTime.Now:yyyy-dd-MM--HH-mm-ss}_Input.zip"); } [MemberAuthorize(AllowGroup = "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) => GetOutputZip(DataService.GetLeafInput(id), type); private ActionResult GetOutputZip(LeafInput leafInput, LeafOutputFileType type) { if (leafInput == null) return View("DownloadNotFound"); var zip = leafInput.GetOutputFileZip(type); var suffix = type == LeafOutputFileType.ToUser ? "_Output" : $"_Output_{type}"; var filename = $"{leafInput.Identifier.FilterValidFilename()}{suffix}.zip"; return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; } private ActionResult GetInputZip(int id) => GetInputZip(DataService.GetLeafInput(id)); private ActionResult GetInputZip(LeafInput leafInput) { 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 }; } } }