-
-
Save sasajib/7001806efc473e69d6b5 to your computer and use it in GitHub Desktop.
Using custom multipart body parser that uses specified dir to save files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import java.io.File; | |
import java.util.List; | |
import play.mvc.BodyParser; | |
import play.mvc.BodyParser.Of; | |
import play.mvc.Controller; | |
import play.mvc.Http.MultipartFormData; | |
import play.mvc.Http.MultipartFormData.FilePart; | |
import play.mvc.Http.RequestBody; | |
import play.mvc.Result; | |
import utils.CustomParsers; | |
import views.html.index; | |
public class Application extends Controller { | |
public static class PermanentMultipartFormData implements BodyParser { | |
//location to save the files | |
private final File dir = new File("data"); | |
@Override | |
public play.api.mvc.BodyParser<RequestBody> parser(int maxLength) { | |
return CustomParsers.permanentMultipartFormData(dir, maxLength); | |
} | |
} | |
@Of(PermanentMultipartFormData.class) | |
public static Result upload() { | |
MultipartFormData md = request().body().asMultipartFormData(); | |
//do your thing | |
} | |
public static Result index() { | |
return ok(index.render("Your new application is ready.")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import java.io.File | |
import play.api.mvc._ | |
import play.mvc.Http.RequestBody | |
import play.api.mvc.BodyParsers._ | |
import parse._ | |
import play.core.j.JavaParsers.DefaultRequestBody | |
import play.api.libs.iteratee.Iteratee | |
import play.api.mvc.MultipartFormData._ | |
import play.api.libs.Files.TemporaryFile | |
import java.io.FileOutputStream | |
import scala.collection.JavaConverters._ | |
object CustomParsers { | |
case class MultipartRequestBody(multipart: MultipartFormData[File]) extends RequestBody { | |
override lazy val asMultipartFormData = { | |
new play.mvc.Http.MultipartFormData { | |
lazy val asFormUrlEncoded = { | |
multipart.asFormUrlEncoded.mapValues(_.toArray).asJava | |
} | |
lazy val getFiles = { | |
multipart.files.map { file => | |
new play.mvc.Http.MultipartFormData.FilePart( | |
file.key, file.filename, file.contentType.orNull, file.ref) | |
}.asJava | |
} | |
} | |
} | |
} | |
private def orDefault(maxLength: Int) = if (maxLength < 0) parse.DEFAULT_MAX_TEXT_LENGTH else maxLength | |
type PartHandler[A] = PartialFunction[Map[String, String], Iteratee[Array[Byte], A]] | |
def permanentMultipartFormData(dir: File, maxLength: Int): BodyParser[RequestBody] = { | |
parse.maxLength(orDefault(maxLength), parse.multipartFormData(handleFilePartAsPermanentFile(dir))).map { | |
_.fold( | |
_ => DefaultRequestBody(isMaxSizeExceeded = true), | |
multipart => | |
MultipartRequestBody(multipart)) | |
} | |
} | |
def handleFilePartAsPermanentFile(dir: File): PartHandler[FilePart[File]] = { | |
Multipart.handleFilePart { | |
case Multipart.FileInfo(partName, filename, contentType) => | |
val file = new File(dir, filename) | |
Iteratee.fold[Array[Byte], FileOutputStream](new java.io.FileOutputStream(file)) { (os, data) => | |
os.write(data) | |
os | |
}.map { os => | |
os.close() | |
file | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment