Skip to content

Commit

Permalink
Do the same thing for AttachmentCollection.AddAsync()
Browse files Browse the repository at this point in the history
Also added unit tests.
  • Loading branch information
jstedfast committed Feb 7, 2024
1 parent 9f63257 commit a4bcf3e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
36 changes: 25 additions & 11 deletions MimeKit/AttachmentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,33 @@ MimeEntity CreateAttachment (ContentType contentType, bool autoDetected, string
return attachment;
}

async Task<MimeEntity> CreateAttachmentAsync (ContentType contentType, string path, Stream stream, bool copyStream, CancellationToken cancellationToken)
async Task<MimeEntity> CreateAttachmentAsync (ContentType contentType, bool autoDetected, string path, Stream stream, bool copyStream, CancellationToken cancellationToken)
{
var fileName = GetFileName (path);
MimeEntity attachment;
MimeEntity attachment = null;

if (contentType.IsMimeType ("message", "rfc822")) {
var message = await MimeMessage.LoadAsync (stream, cancellationToken).ConfigureAwait (false);
long position = stream.CanSeek ? stream.Position : 0;

if (!copyStream)
stream.Dispose ();
try {
var message = await MimeMessage.LoadAsync (stream, cancellationToken).ConfigureAwait (false);

if (!copyStream)
stream.Dispose ();

attachment = new MessagePart { Message = message };
} else {
attachment = new MessagePart { Message = message };
} catch (FormatException) {
if (autoDetected && stream.CanSeek) {
// If the contentType was auto-detected and the stream is seekable, fall back to attaching this content as a generic stream
contentType = new ContentType ("application", "octet-stream");
stream.Position = position;
} else {
throw;
}
}
}

if (attachment is null) {
MimePart part;

if (contentType.IsMimeType ("text", "*")) {
Expand Down Expand Up @@ -466,7 +480,7 @@ public async Task<MimeEntity> AddAsync (string fileName, Stream stream, ContentT
if (contentType is null)
throw new ArgumentNullException (nameof (contentType));

var attachment = await CreateAttachmentAsync (contentType, fileName, stream, true, cancellationToken).ConfigureAwait (false);
var attachment = await CreateAttachmentAsync (contentType, false, fileName, stream, true, cancellationToken).ConfigureAwait (false);

attachments.Add (attachment);

Expand Down Expand Up @@ -589,7 +603,7 @@ public async Task<MimeEntity> AddAsync (string fileName, Stream stream, Cancella
if (stream is null)
throw new ArgumentNullException (nameof (stream));

var attachment = await CreateAttachmentAsync (GetMimeType (fileName), fileName, stream, true, cancellationToken).ConfigureAwait (false);
var attachment = await CreateAttachmentAsync (GetMimeType (fileName), true, fileName, stream, true, cancellationToken).ConfigureAwait (false);

attachments.Add (attachment);

Expand Down Expand Up @@ -692,7 +706,7 @@ public async Task<MimeEntity> AddAsync (string fileName, ContentType contentType
throw new ArgumentNullException (nameof (contentType));

using (var stream = File.OpenRead (fileName)) {
var attachment = await CreateAttachmentAsync (contentType, fileName, stream, true, cancellationToken).ConfigureAwait (false);
var attachment = await CreateAttachmentAsync (contentType, false, fileName, stream, true, cancellationToken).ConfigureAwait (false);

attachments.Add (attachment);

Expand Down Expand Up @@ -791,7 +805,7 @@ public async Task<MimeEntity> AddAsync (string fileName, CancellationToken cance
throw new ArgumentException ("The specified file path is empty.", nameof (fileName));

using (var stream = File.OpenRead (fileName)) {
var attachment = await CreateAttachmentAsync (GetMimeType (fileName), fileName, stream, true, cancellationToken).ConfigureAwait (false);
var attachment = await CreateAttachmentAsync (GetMimeType (fileName), true, fileName, stream, true, cancellationToken).ConfigureAwait (false);

attachments.Add (attachment);

Expand Down
48 changes: 48 additions & 0 deletions UnitTests/AttachmentCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,54 @@ public async Task TestAddEmailMessageAsync ()
attachments.Clear (true);
}

[Test]
public void TestAddEmailMessageFallback ()
{
var fileName = Path.Combine (TestHelper.ProjectDir, "TestData", "images", "girl.jpg");
var attachments = new AttachmentCollection ();
MimeEntity attachment;

using (var stream = File.OpenRead (fileName))
attachment = attachments.Add ("message.eml", stream);

Assert.That (attachment.ContentType.MimeType, Is.EqualTo ("application/octet-stream"));
Assert.That (attachment.ContentType.Name, Is.EqualTo ("message.eml"));
Assert.That (attachment.ContentDisposition, Is.Not.Null);
Assert.That (attachment.ContentDisposition.Disposition, Is.EqualTo ("attachment"));
Assert.That (attachment.ContentDisposition.FileName, Is.EqualTo ("message.eml"));
Assert.That (attachments.Count, Is.EqualTo (1));

Assert.That (attachments.Contains (attachment), Is.True, "Contains");
Assert.That (attachments.IndexOf (attachment), Is.EqualTo (0), "IndexOf");
Assert.That (attachments.Remove (attachment), Is.True, "Remove");
Assert.That (attachments.Count, Is.EqualTo (0));
attachments.Clear (true);
}

[Test]
public async Task TestAddEmailMessageFallbackAsync ()
{
var fileName = Path.Combine (TestHelper.ProjectDir, "TestData", "images", "girl.jpg");
var attachments = new AttachmentCollection ();
MimeEntity attachment;

using (var stream = File.OpenRead (fileName))
attachment = await attachments.AddAsync ("message.eml", stream);

Assert.That (attachment.ContentType.MimeType, Is.EqualTo ("application/octet-stream"));
Assert.That (attachment.ContentType.Name, Is.EqualTo ("message.eml"));
Assert.That (attachment.ContentDisposition, Is.Not.Null);
Assert.That (attachment.ContentDisposition.Disposition, Is.EqualTo ("attachment"));
Assert.That (attachment.ContentDisposition.FileName, Is.EqualTo ("message.eml"));
Assert.That (attachments.Count, Is.EqualTo (1));

Assert.That (attachments.Contains (attachment), Is.True, "Contains");
Assert.That (attachments.IndexOf (attachment), Is.EqualTo (0), "IndexOf");
Assert.That (attachments.Remove (attachment), Is.True, "Remove");
Assert.That (attachments.Count, Is.EqualTo (0));
attachments.Clear (true);
}

[Test]
public void TestAddInlineEmailMessage ()
{
Expand Down

0 comments on commit a4bcf3e

Please sign in to comment.