Skip to content

Commit

Permalink
Merge branch 'feature/improve_webhook_parser' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Aug 20, 2018
2 parents af56c3e + 4070239 commit dbaab9a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class InboundEmailAttachment
/// <value>
/// The content-type.
/// </value>
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
public string ContentType { get; set; }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/StrongGrid/Utilities/DiagnosticHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class DiagnosticHandler : IHttpFilter
{
#region FIELDS

private const string DIAGNOSTIC_ID_HEADER_NAME = "StrongGridDiagnosticId";
private const string DIAGNOSTIC_ID_HEADER_NAME = "StrongGrid-DiagnosticId";
private static readonly ILog _logger = LogProvider.For<DiagnosticHandler>();
private readonly IDictionary<WeakReference<HttpRequestMessage>, Tuple<StringBuilder, Stopwatch>> _diagnostics = new Dictionary<WeakReference<HttpRequestMessage>, Tuple<StringBuilder, Stopwatch>>();

Expand Down
19 changes: 17 additions & 2 deletions Source/StrongGrid/WebhookParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ public Event[] ParseWebhookEvents(string requestBody)
/// <returns>The <see cref="InboundEmail"/>.</returns>
public InboundEmail ParseInboundEmailWebhook(Stream stream)
{
// We need to be able to rewind the stream.
// Therefore, we must make a copy of the stream if it doesn't allow changing the position
if (!stream.CanSeek)
{
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
return ParseInboundEmailWebhook(ms);
}
}

// It's important to rewind the stream
stream.Position = 0;

// Parse the multipart content received from SendGrid
var parser = new MultipartFormDataParser(stream, Encoding.UTF8);

Expand Down Expand Up @@ -85,8 +99,9 @@ public InboundEmail ParseInboundEmailWebhook(Stream stream)
var file = parser.Files.FirstOrDefault(f => f.Name == prop.Name);
if (file != null)
{
attachment.ContentType = file.ContentType;
attachment.Data = file.Data;
if (string.IsNullOrEmpty(attachment.ContentType)) attachment.ContentType = file.ContentType;
if (string.IsNullOrEmpty(attachment.FileName)) attachment.FileName = file.FileName;
}
return attachment;
Expand Down Expand Up @@ -116,7 +131,7 @@ public InboundEmail ParseInboundEmailWebhook(Stream stream)
.Distinct()
.Select(encoding =>
{
stream.Position = 0;
stream.Position = 0; // It's important to rewind the stream
return new
{
Encoding = encoding,
Expand Down

0 comments on commit dbaab9a

Please sign in to comment.