Move the email call into SavePdfOperation for better error handling etc

This commit is contained in:
Ben Olden-Cooligan 2018-09-17 01:20:21 -04:00
parent f5f0273608
commit 9ed2f61324
6 changed files with 58 additions and 15 deletions

View File

@ -552,7 +552,7 @@ namespace NAPS2.Automation
};
int digits = (int)Math.Floor(Math.Log10(scanList.Count)) + 1;
string actualPath = fileNamePlaceholders.SubstitutePlaceholders(path, startTime, true, scanIndex++, scanList.Count > 1 ? digits : 0);
op.Start(actualPath, startTime, fileContents, pdfSettings, ocrParams, email);
op.Start(actualPath, startTime, fileContents, pdfSettings, ocrParams, email, null);
if (!await op.Success)
{
return false;

View File

@ -105,7 +105,7 @@ namespace NAPS2.ImportExport
subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, true, 0, 1);
}
var op = operationFactory.Create<SavePdfOperation>();
if (op.Start(subPath, now, images, pdfSettingsContainer.PdfSettings, ocrManager.DefaultParams, false))
if (op.Start(subPath, now, images, pdfSettingsContainer.PdfSettings, ocrManager.DefaultParams, false, null))
{
operationProgress.ShowProgress(op);
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using NAPS2.ImportExport.Email;
using NAPS2.Lang.Resources;
using NAPS2.Ocr;
using NAPS2.Operation;
@ -16,18 +17,20 @@ namespace NAPS2.ImportExport.Pdf
private readonly FileNamePlaceholders fileNamePlaceholders;
private readonly IPdfExporter pdfExporter;
private readonly IOverwritePrompt overwritePrompt;
private readonly IEmailProviderFactory emailProviderFactory;
public SavePdfOperation(FileNamePlaceholders fileNamePlaceholders, IPdfExporter pdfExporter, IOverwritePrompt overwritePrompt)
public SavePdfOperation(FileNamePlaceholders fileNamePlaceholders, IPdfExporter pdfExporter, IOverwritePrompt overwritePrompt, IEmailProviderFactory emailProviderFactory)
{
this.fileNamePlaceholders = fileNamePlaceholders;
this.pdfExporter = pdfExporter;
this.overwritePrompt = overwritePrompt;
this.emailProviderFactory = emailProviderFactory;
AllowCancel = true;
AllowBackground = true;
}
public bool Start(string fileName, DateTime dateTime, ICollection<ScannedImage> images, PdfSettings pdfSettings, OcrParams ocrParams, bool email)
public bool Start(string fileName, DateTime dateTime, ICollection<ScannedImage> images, PdfSettings pdfSettings, OcrParams ocrParams, bool email, EmailMessage emailMessage)
{
ProgressTitle = email ? MiscResources.EmailPdfProgress : MiscResources.SavePdfProgress;
var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime);
@ -53,9 +56,10 @@ namespace NAPS2.ImportExport.Pdf
var snapshots = images.Select(x => x.Preserve()).ToList();
RunAsync(async () =>
{
bool result = false;
try
{
return await pdfExporter.Export(subFileName, snapshots, pdfSettings, ocrParams, OnProgress, CancelToken);
result = await pdfExporter.Export(subFileName, snapshots, pdfSettings, ocrParams, OnProgress, CancelToken);
}
catch (UnauthorizedAccessException ex)
{
@ -83,7 +87,26 @@ namespace NAPS2.ImportExport.Pdf
snapshots.ForEach(s => s.Dispose());
GC.Collect();
}
return false;
if (result && email && emailMessage != null)
{
Status.StatusText = MiscResources.UploadingEmail;
Status.CurrentProgress = 0;
Status.MaxProgress = 1;
InvokeStatusChanged();
try
{
result = emailProviderFactory.Default.SendEmail(emailMessage);
}
catch (Exception ex)
{
Log.ErrorException(MiscResources.ErrorEmailing, ex);
InvokeError(MiscResources.ErrorEmailing, ex);
}
}
return result;
});
return true;

View File

@ -492,6 +492,15 @@ namespace NAPS2.Lang.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to An error occurred when trying to send the email..
/// </summary>
internal static string ErrorEmailing {
get {
return ResourceManager.GetString("ErrorEmailing", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to An error occurred when trying to save the file..
/// </summary>
@ -1104,6 +1113,15 @@ namespace NAPS2.Lang.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Uploading email....
/// </summary>
internal static string UploadingEmail {
get {
return ResourceManager.GetString("UploadingEmail", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Version {0}.
/// </summary>

View File

@ -468,4 +468,10 @@
<data name="AuthError" xml:space="preserve">
<value>An error occurred when trying to authorize.</value>
</data>
<data name="UploadingEmail" xml:space="preserve">
<value>Uploading email...</value>
</data>
<data name="ErrorEmailing" xml:space="preserve">
<value>An error occurred when trying to send the email.</value>
</data>
</root>

View File

@ -69,7 +69,7 @@ namespace NAPS2.WinForms
var subSavePath = fileNamePlaceholders.SubstitutePlaceholders(savePath, DateTime.Now);
var changeToken = changeTracker.State;
if (await ExportPDF(subSavePath, images, false))
if (await ExportPDF(subSavePath, images, false, null))
{
changeTracker.Saved(changeToken);
notify?.PdfSaved(subSavePath);
@ -79,13 +79,13 @@ namespace NAPS2.WinForms
return false;
}
public async Task<bool> ExportPDF(string filename, List<ScannedImage> images, bool email)
public async Task<bool> ExportPDF(string filename, List<ScannedImage> images, bool email, EmailMessage emailMessage)
{
var op = operationFactory.Create<SavePdfOperation>();
var pdfSettings = pdfSettingsContainer.PdfSettings;
pdfSettings.Metadata.Creator = MiscResources.NAPS2;
if (op.Start(filename, DateTime.Now, images, pdfSettings, ocrManager.DefaultParams, email))
if (op.Start(filename, DateTime.Now, images, pdfSettings, ocrManager.DefaultParams, email, emailMessage))
{
operationProgress.ShowProgress(op);
}
@ -163,11 +163,7 @@ namespace NAPS2.WinForms
{
string targetPath = Path.Combine(tempFolder.FullName, attachmentName);
var changeToken = changeTracker.State;
if (!await ExportPDF(targetPath, images, true))
{
// Cancel or error
return false;
}
var message = new EmailMessage
{
Attachments =
@ -180,7 +176,7 @@ namespace NAPS2.WinForms
}
};
if (emailProviderFactory.Default.SendEmail(message))
if (await ExportPDF(targetPath, images, true, message))
{
changeTracker.Saved(changeToken);
return true;