Add download links

This commit is contained in:
2016-05-06 11:19:01 -04:00
parent 972bb04291
commit f4ebeefad1
12 changed files with 109 additions and 29 deletions
+22
View File
@@ -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);
}
}
}
+41 -20
View File
@@ -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";
/// <summary>
/// Comma separated values
/// </summary>
@@ -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);
}