This repository has been archived by the owner on Apr 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#112, chore: Merging downstream changes into the branch
- Loading branch information
Showing
187 changed files
with
15,742 additions
and
5,086 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
51 changes: 51 additions & 0 deletions
51
api/src/main/scala/de/zalando/play/controllers/MultipartFormDataWritable.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package de.zalando.play.controllers | ||
|
||
import java.nio.file.{Files, Paths} | ||
|
||
import play.api.http.{HeaderNames, Writeable} | ||
import play.api.libs.Files.TemporaryFile | ||
import play.api.mvc.MultipartFormData.FilePart | ||
import play.api.mvc.{Codec, MultipartFormData} | ||
|
||
/** | ||
* @author slasch | ||
* @since 04.05.2016. | ||
* | ||
* taken from <a href="http://tech.fongmun.com/post/125479939452/test-multipartformdata-in-play">here</a> | ||
*/ | ||
object MultipartFormDataWritable { | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
val boundary = "--------ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" | ||
|
||
def formatDataParts(data: Map[String, Seq[String]]) = { | ||
val dataParts = data.flatMap { case (key, values) => | ||
values.map { value => | ||
val name = s""""$key"""" | ||
s"--$boundary\r\n${HeaderNames.CONTENT_DISPOSITION}: form-data; name=$name\r\n\r\n$value\r\n" | ||
} | ||
}.mkString("") | ||
Codec.utf_8.encode(dataParts) | ||
} | ||
|
||
def filePartHeader(file: FilePart[TemporaryFile]) = { | ||
val name = s""""${file.key}"""" | ||
val filename = s""""${file.filename}"""" | ||
val contentType = file.contentType.map { ct => | ||
s"${HeaderNames.CONTENT_TYPE}: $ct\r\n" | ||
}.getOrElse("") | ||
Codec.utf_8.encode(s"--$boundary\r\n${HeaderNames.CONTENT_DISPOSITION}: form-data; name=$name; filename=$filename\r\n$contentType\r\n") | ||
} | ||
|
||
val singleton = Writeable[MultipartFormData[TemporaryFile]]( | ||
transform = { form: MultipartFormData[TemporaryFile] => | ||
formatDataParts(form.dataParts) ++ | ||
form.files.flatMap { file => | ||
val fileBytes = Files.readAllBytes(Paths.get(file.ref.file.getAbsolutePath)) | ||
filePartHeader(file) ++ fileBytes ++ Codec.utf_8.encode("\r\n") | ||
} ++ | ||
Codec.utf_8.encode(s"--$boundary--") | ||
}, | ||
contentType = Some(s"multipart/form-data; boundary=$boundary") | ||
) | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.