Following this post to implement stream based file upload using custom parser/Iteratee. Of course code in that post is old and doesn't compile anymore. Following is what I am trying with.
case class UploadIteratee(state: Symbol = 'Cont, input: Input[Array[Byte]] = Empty, received: Int = 0) extends Iteratee[Array[Byte], Either[Result, Int]] {
def fold[B](folder: Step[Array[Byte], Either[Result, Int]] => Future[B])(implicit ec: ExecutionContext): Future[B] = {
folder(
Step.Cont(in => in match {
case in: El[Array[Byte]] => copy(input = in, received = received + in.e.length)
case Empty => copy(input = in)
case EOF => copy(state = 'Done, input = in)
case _ => copy(state = 'Error, input = in)
}))
}
}
def send = Action(BodyParser(rh => new UploadIteratee).map(Right(_))) { request =>
Ok("Done")
}
Does that look right and sufficient to accept stream from file upload? I must be doing something silly to get following compile error.
type mismatch; found : controllers.MyController.UploadIteratee required:
play.api.libs.iteratee.Iteratee[Array[Byte],Either[play.api.mvc.SimpleResult,?]]
EDIT I am on Play 2.2.2. My bad that I was looking at play 2.3 source code. now following compiles
case class UploadIteratee(state: Symbol = 'Cont, input: Input[Array[Byte]] = Empty, received: Int = 0) extends Iteratee[Array[Byte], Either[SimpleResult, Int]] {
def fold[B](folder: Step[Array[Byte], Either[SimpleResult, Int]] => Future[B])(implicit ec: ExecutionContext): Future[B] = {
folder(
Step.Cont(in => in match {
case in: El[Array[Byte]] => copy(input = in, received = received + in.e.length)
case Empty => copy(input = in)
case EOF => copy(state = 'Done, input = in)
case _ => copy(state = 'Error, input = in)
}))
}
}
def send = Action(BodyParser(rh => new UploadIteratee).map(Right(_))) { request =>
Ok("Done")
}
wingedsubmariner : I am trying to write a body parser which takes in stream of data for file upload functionality. Subsequently I'll have to populate another data holding case class with each record received in the stream. My understanding about Iteratee
is still raw so any pointers will be appreciated.