@@ -3,7 +3,7 @@ import Vapor
33public struct MailgunIncomingMessage : Content {
44 public static var defaultContentType : HTTPMediaType = . formData
55
6- public let recipients : String
6+ public let recipient : String
77 public let sender : String
88 public let from : String
99 public let subject : String
@@ -14,22 +14,62 @@ public struct MailgunIncomingMessage: Content {
1414 public let strippedHTML : String
1515 public let messageHeaders : String
1616 public let contentIdMap : String
17- public let attachments : [ Attachment ] ?
17+ public let attachments : [ File ]
1818
1919 enum CodingKeys : String , CodingKey {
20- case recipients
20+ case recipient
2121 case sender
2222 case from
2323 case subject
2424 case bodyPlain = " body-plain "
2525 case strippedText = " stripped-text "
26- case strippedSignature = " stripped-signiture "
26+ case strippedSignature = " stripped-signature "
2727 case bodyHTML = " body-html "
2828 case strippedHTML = " stripped-html "
2929 case messageHeaders = " message-headers "
3030 case contentIdMap = " content-id-map "
3131 case attachments
3232 }
33+
34+ struct DynamicAttachmentKey : CodingKey {
35+ var stringValue : String
36+
37+ init ? ( stringValue: String ) {
38+ guard stringValue. hasPrefix ( " attachment- " ) else { return nil }
39+ guard let lastKey = stringValue. components ( separatedBy: " - " ) . last,
40+ let _ = Int ( lastKey)
41+ else { return nil }
42+ self . stringValue = stringValue
43+ }
44+
45+ var intValue : Int ?
46+
47+ init ? ( intValue: Int ) {
48+ return nil
49+ }
50+ }
51+
52+ public init ( from decoder: Decoder ) throws {
53+ let container = try decoder. container ( keyedBy: CodingKeys . self)
54+ recipient = try container. decode ( String . self, forKey: . recipient)
55+ sender = try container. decode ( String . self, forKey: . sender)
56+ from = try container. decode ( String . self, forKey: . from)
57+ subject = try container. decode ( String . self, forKey: . subject)
58+ bodyPlain = try container. decode ( String . self, forKey: . bodyPlain)
59+ strippedText = try container. decode ( String . self, forKey: . strippedText)
60+ strippedSignature = try container. decodeIfPresent ( String . self, forKey: . strippedSignature)
61+ bodyHTML = try container. decode ( String . self, forKey: . bodyHTML)
62+ strippedHTML = try container. decode ( String . self, forKey: . strippedHTML)
63+ messageHeaders = try container. decode ( String . self, forKey: . messageHeaders)
64+ contentIdMap = try container. decode ( String . self, forKey: . contentIdMap)
65+
66+ var _attachments : [ File ] = [ ]
67+ let attachmentsContainer = try decoder. container ( keyedBy: DynamicAttachmentKey . self)
68+ try attachmentsContainer. allKeys. forEach { attachmentKey in
69+ _attachments. append ( try attachmentsContainer. decode ( File . self, forKey: attachmentKey) )
70+ }
71+ attachments = _attachments
72+ }
3373}
3474
3575extension MailgunIncomingMessage {
0 commit comments