From f4ebeefad18c7349f8764541800656468b80ca40 Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Fri, 6 May 2016 11:19:01 -0400 Subject: [PATCH] Add download links --- Core/Core.csproj | 4 ++ Core/DAL/DataService.cs | 7 +++ Core/Entities/LeafInput.cs | 5 ++ Core/packages.config | 1 + Web/Controllers/LeafCharterController.cs | 7 ++- Web/Controllers/LeafInputController.cs | 1 + Web/Controllers/ResultsController.cs | 7 ++- Web/Services/DownloadUrlService.cs | 22 ++++++++ Web/Services/EmailNotificationService.cs | 61 +++++++++++++++-------- Web/Views/Results/DownloadNotFound.cshtml | 10 ++++ Web/Web.config | 11 ++-- Web/Web.csproj | 2 + 12 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 Web/Services/DownloadUrlService.cs create mode 100644 Web/Views/Results/DownloadNotFound.cshtml diff --git a/Core/Core.csproj b/Core/Core.csproj index e82b6ae..b257d4c 100644 --- a/Core/Core.csproj +++ b/Core/Core.csproj @@ -50,6 +50,10 @@ ..\packages\fasterflect.2.1.3\lib\net40\Fasterflect.dll True + + ..\packages\MlkPwgen.0.2.0.0\lib\net45\MlkPwgen.dll + True + ..\packages\NLog.4.2.3\lib\net45\NLog.dll True diff --git a/Core/DAL/DataService.cs b/Core/DAL/DataService.cs index f2c91a5..d4b0626 100644 --- a/Core/DAL/DataService.cs +++ b/Core/DAL/DataService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Data.Entity; using System.Linq; using LeafWeb.Core.Entities; +using MlkPwgen; namespace LeafWeb.Core.DAL { @@ -50,6 +51,11 @@ namespace LeafWeb.Core.DAL return _db.LeafInputs.FirstOrDefault(li => li.Id == id); } + public LeafInput GetLeafInput(string uniqueToken) + { + return _db.LeafInputs.FirstOrDefault(li => li.UniqueToken == uniqueToken); + } + public IQueryable GetLeafInputs(params LeafInputStatusType[] statuses) { return @@ -61,6 +67,7 @@ namespace LeafWeb.Core.DAL public void AddLeafInput(LeafInput leafInput) { leafInput.Added = DateTime.Now; + leafInput.UniqueToken = PasswordGenerator.Generate(12); _db.LeafInputs.Add(leafInput); SetLeafInputStatusNoUpdate(leafInput, LeafInputStatusType.Pending); _db.SaveChanges(); diff --git a/Core/Entities/LeafInput.cs b/Core/Entities/LeafInput.cs index 3e4852d..dc10524 100644 --- a/Core/Entities/LeafInput.cs +++ b/Core/Entities/LeafInput.cs @@ -70,5 +70,10 @@ namespace LeafWeb.Core.Entities return compressedFileStream.ToArray(); } } + + public int GetOutputFileSizeSum() + { + return OutputFiles.Sum(o => o.Contents.Length); + } } } \ No newline at end of file diff --git a/Core/packages.config b/Core/packages.config index 37b09c6..527df52 100644 --- a/Core/packages.config +++ b/Core/packages.config @@ -4,6 +4,7 @@ + \ No newline at end of file diff --git a/Web/Controllers/LeafCharterController.cs b/Web/Controllers/LeafCharterController.cs index 3e570d5..9cabba6 100644 --- a/Web/Controllers/LeafCharterController.cs +++ b/Web/Controllers/LeafCharterController.cs @@ -94,10 +94,13 @@ namespace LeafWeb.Web.Controllers private Bitmap CombineBitmaps(IList bitmaps, int columnCount = 2) { + if (!bitmaps.Any()) + return null; + // bitmaps assumed to have same dimensions, use the first one to define that var cellWidth = bitmaps[0].Width; var cellHeight = bitmaps[0].Height; - var width = cellWidth * 2; - var height = cellHeight * bitmaps.Count / 2; + var width = cellWidth * columnCount; + var height = cellHeight * bitmaps.Count / columnCount; var combinedBitmap = new Bitmap(width, height); using (var g = Graphics.FromImage(combinedBitmap)) diff --git a/Web/Controllers/LeafInputController.cs b/Web/Controllers/LeafInputController.cs index 5dd13da..c5655be 100644 --- a/Web/Controllers/LeafInputController.cs +++ b/Web/Controllers/LeafInputController.cs @@ -100,6 +100,7 @@ namespace LeafWeb.Web.Controllers return View("Index", viewModel); } + // Callback from Piscal public void NotifyComplete() { HangfireStartup.TriggerPiscalProcessQueue(); diff --git a/Web/Controllers/ResultsController.cs b/Web/Controllers/ResultsController.cs index 1e05f77..8119282 100644 --- a/Web/Controllers/ResultsController.cs +++ b/Web/Controllers/ResultsController.cs @@ -19,9 +19,12 @@ namespace LeafWeb.Web.Controllers } [ActionLog] - public FileContentResult DownloadResults(int id) + public ActionResult Download(string token) { - var leafInput = DataService.GetLeafInput(id); + var leafInput = DataService.GetLeafInput(token); + + if (leafInput == null) + return View("DownloadNotFound"); var zip = leafInput.GetOutputFileZip(); diff --git a/Web/Services/DownloadUrlService.cs b/Web/Services/DownloadUrlService.cs new file mode 100644 index 0000000..c936501 --- /dev/null +++ b/Web/Services/DownloadUrlService.cs @@ -0,0 +1,22 @@ +using System.Configuration; +using LeafWeb.Core.Entities; + +namespace LeafWeb.Web.Services +{ + public class DownloadUrlService + { + private readonly string _downloadUrl; + + public DownloadUrlService() + { + _downloadUrl = + ConfigurationManager.AppSettings["LeafWebUrl"] + + ConfigurationManager.AppSettings["ResultsDownloadPath"]; + } + + public string GetDownloadUrl(LeafInput leafInput) + { + return string.Format(_downloadUrl, leafInput.UniqueToken); + } + } +} \ No newline at end of file diff --git a/Web/Services/EmailNotificationService.cs b/Web/Services/EmailNotificationService.cs index 67fde4f..3b0cf20 100644 --- a/Web/Services/EmailNotificationService.cs +++ b/Web/Services/EmailNotificationService.cs @@ -17,6 +17,9 @@ namespace LeafWeb.Web.Services private readonly string _emailFromAddress; + private const string EmailSuccessSubject = "LeafWeb Results"; + private const string EmailErrorSubject = "LeafWeb processing error"; + /// /// Comma separated values /// @@ -26,10 +29,14 @@ namespace LeafWeb.Web.Services private readonly DataService _dataService; + private readonly DownloadUrlService _downloadUrlService; + public EmailNotificationService(DataService dataService) { _dataService = dataService; + _downloadUrlService = new DownloadUrlService(); + _smtpClient = new SmtpClient( ConfigurationManager.AppSettings["SmtpHost"], Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"])); @@ -66,35 +73,49 @@ namespace LeafWeb.Web.Services private void SendLeafWebSuccess(LeafInput leafInput) { - var fileStreams = - (from outputFile in - leafInput.OutputFiles - select Tuple.Create(outputFile, new MemoryStream(outputFile.Contents))).ToList(); - var body = $"Your leaf analysis job, {leafInput.Identifier}, has completed. "; body += FormatWarningMessage(leafInput); - - body += "Please see the attached results."; - var message = new MailMessage(_emailFromAddress, leafInput.Email, "LeafWeb results", body); - - try + if (true) { - foreach (var fileStream in fileStreams) - { - var attachment = new Attachment(fileStream.Item2, fileStream.Item1.Filename); - message.Attachments.Add(attachment); - } + var downloadUrl = _downloadUrlService.GetDownloadUrl(leafInput); + body += + "Download results with the following link:" + + Environment.NewLine + Environment.NewLine + + downloadUrl; + + var message = new MailMessage(_emailFromAddress, leafInput.Email, EmailSuccessSubject, body); SendMessage(message); } - finally + else { - // can't dispose those memory streams until the message is sent - foreach (var stream in fileStreams.Select(f => f.Item2)) + body += "Please see the attached results."; + + var message = new MailMessage(_emailFromAddress, leafInput.Email, EmailSuccessSubject, body); + + var fileStreams = + (from outputFile in + leafInput.OutputFiles + select Tuple.Create(outputFile, new MemoryStream(outputFile.Contents))).ToList(); + try { - stream.Dispose(); + foreach (var fileStream in fileStreams) + { + var attachment = new Attachment(fileStream.Item2, fileStream.Item1.Filename); + message.Attachments.Add(attachment); + } + + SendMessage(message); + } + finally + { + // can't dispose those memory streams until the message is sent + foreach (var stream in fileStreams.Select(f => f.Item2)) + { + stream.Dispose(); + } } } } @@ -109,7 +130,7 @@ namespace LeafWeb.Web.Services body += FormatWarningMessage(leafInput); - var message = new MailMessage(_emailFromAddress, leafInput.Email, "LeafWeb processing error", body); + var message = new MailMessage(_emailFromAddress, leafInput.Email, EmailErrorSubject, body); SendMessage(message); } diff --git a/Web/Views/Results/DownloadNotFound.cshtml b/Web/Views/Results/DownloadNotFound.cshtml new file mode 100644 index 0000000..031fe62 --- /dev/null +++ b/Web/Views/Results/DownloadNotFound.cshtml @@ -0,0 +1,10 @@ +@{ + ViewBag.Title = "Not Found"; +} + +

+ @ViewBag.Title +

+ +

The given download link was not found. Please contact administrator, referencing the following url:

+@Html.Raw(Request.Url) \ No newline at end of file diff --git a/Web/Web.config b/Web/Web.config index f5ce4a6..6ba5e22 100644 --- a/Web/Web.config +++ b/Web/Web.config @@ -25,13 +25,14 @@ - - - - - + + + + + + diff --git a/Web/Web.csproj b/Web/Web.csproj index cdc793b..d309321 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -960,6 +960,7 @@ + @@ -1020,6 +1021,7 @@ + Web.config