I would like to use Play2 to implement a server that receives a POST of files, process the files as they are received, and send the results to a GET request from another user while the POST is still transferring files. Is it possible to do this with Play2? I already use Play2 to authenticate users, so I would like to use Play2 to handle authentication of this transaction. I use Java, but if needed, I can use Scala. If so, what should I look into? If not, I guess I need to look into some Java NIO frameworks and implement another server.
I don't think this is possible in Java, but I've managed to put together a simple Scala version. You can still use Java for all other parts of you project.
But be aware that it's just a crude demo, focussing on the "simultaneous up- and download" part and ignoring everything else. I'm not even sure that this is the right way to do it, but it seems to work.
Basically, what you want to do is to use Concurrent.broadcast to create a input/output stream pair. The output part (Enumeratee) is streamed to the client. Then, you need a custom PartHandler which takes the uploaded data as it arrives, and feeds it into the the input part (Concurrent.Channel).
Please not that, in order for this example to work, you have to go to the download page first, then start uploading a file.
package controllers
import play.api._
import libs.iteratee.{Enumerator, Input, Iteratee, Concurrent}
import play.api.mvc._
import scala.collection.concurrent
object Application extends Controller {
val streams = new concurrent.TrieMap[String,(Enumerator[Array[Byte]], Concurrent.Channel[Array[Byte]])]
def index = Action {
Ok(views.html.index())
}
def download(id: String) = Action {
val (fileStream, fileInput) = Concurrent.broadcast[Array[Byte]]
streams += id -> (fileStream, fileInput)
Ok.stream {
fileStream.onDoneEnumerating(streams -= id)
}
}
def upload = Action(parse.multipartFormData(myPartHandler)) {
request => Ok("banana!")
}
def myPartHandler: BodyParsers.parse.Multipart.PartHandler[MultipartFormData.FilePart[Result]] = {
parse.Multipart.handleFilePart {
case parse.Multipart.FileInfo(partName, filename, contentType) =>
val (thisStream, thisInput) = streams(partName)
Iteratee.fold[Array[Byte], Concurrent.Channel[Array[Byte]]](thisInput) { (inp, data) =>
inp.push(data)
inp
}.mapDone { inp =>
inp.push(Input.EOF)
Ok("upload Done")
}
}
}
}
<p>
<a href="@routes.Application.download("123456")">Download</a>
</p>
<form action="@routes.Application.upload" method="post" enctype="multipart/form-data">
<input type="file" name="123456">
<input type="submit">
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With