从我的好友 Tor 那里得到了答案!奇迹般有效!
class Parser @Inject()(mailRequestService: MailRequestService, surveyService: SurveyService) extends Controller {
val UTF8 = "UTF-8"
def parseMail = Action.async(rawFormData) { request =>
val charsets = extract(request.body.dataParts, "charsets", _.as[Charsets]).getOrElse(Charsets(Some(""), Some(""), Some(""), Some(""), Some("")))
val envelope = extract(request.body.dataParts, "envelope", _.as[Envelope]).getOrElse(Envelope(Nil, ""))
val sendgrid = SendgridMail(
extractString(request.body.dataParts, "text", charsets),
extractString(request.body.dataParts, "html", charsets),
extractString(request.body.dataParts, "from", charsets),
extractString(request.body.dataParts, "to", charsets),
charsets,
envelope
)
val simple = mailRequestService.createNewMail(sendgrid).map {
result =>
result.fold(
exception => throw new UnexpectedServiceException("Could not save sendgrid mail: "+sendgrid, exception),
mail => Ok("Success")
)
}
simple
}
def extractString(data: Map[String, Seq[Array[Byte]]], key: String, charsets: Charsets): Option[String] = {
play.Logger.info("data = " + data)
data.get(key).flatMap(_.headOption).map { firstValue =>
(charsets, key) match {
case (charset, "text") if charset.text.isDefined =>
val cset = java.nio.charset.Charset.forName(charset.text.get)
Some(new String(firstValue, cset))
case (charset, "html") if charset.html.isDefined =>
val cset = java.nio.charset.Charset.forName(charset.html.get)
Some(new String(firstValue, cset))
case (charset, "from") if charset.from.isDefined =>
val cset = java.nio.charset.Charset.forName(charset.from.get)
Some(new String(firstValue, cset))
case (charset, "to") if charset.to.isDefined =>
val cset = java.nio.charset.Charset.forName(charset.to.get)
Some(new String(firstValue, cset))
case _ => Some("")
}
}.getOrElse(Some(""))
}
/**
* 1. Retrieve value for key eg. envelope
* 2. Use flatmap to flatten the structure so that we do not get Option[Option[_] ]
* 3. Call map and use JsonParse on the String to get JsValue
* 4. Call map and use provided method eg _.as[Envelope] that results in T, in this case Envelope
* 5. RETURN!
*/
def extract[T](env: Map[String, Seq[Array[Byte]]], key: String, conv: JsValue => T): Option[T] = {
env.get(key).flatMap(_.headOption.map(a => new String(a, "UTF-8"))).map(Json.parse).map(conv)
}
def findSurveyByEmail(email: String): Future[Option[Survey]] = {
surveyService.findSurveyByEmail(email)
}
def handler: parse.Multipart.PartHandler[Part] = {
case headers @ PartInfoMatcher(partName) if !FileInfoMatcher.unapply(headers).isDefined =>
Traversable.takeUpTo[Array[Byte]](1024 * 100)
.transform(Iteratee.consume[Array[Byte]]().map(bytes => FilePart(partName, "", None, bytes)))
.flatMap { data =>
Cont({
case Input.El(_) => Done(data, Input.Empty)
case in => Done(data, in)
})
}
case headers => Done(BadPart(headers), Input.Empty)
}
def rawFormData[A]: BodyParser[RawDataFormData] = BodyParser("multipartFormData") { request =>
Multipart.multipartParser(handler)(request).map { errorOrParts =>
errorOrParts.right.map { parts =>
val data = parts.collect { case FilePart(key, _, _, value: Array[Byte]) => (key, value) }.groupBy(_._1).mapValues(_.map(_._2))
RawDataFormData(data)
}
}
}
}
case class RawDataFormData(dataParts: Map[String, Seq[Array[Byte]]])
谢谢托尔!