Last active
October 24, 2018 08:02
-
-
Save rklaehn/d4d3ee43443b0f4741fb to your computer and use it in GitHub Desktop.
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
name := "file_upload_test" | |
version := "0.1-SNAPSHOT" | |
scalaVersion := "2.11.7" | |
val akka_http = "2.0.2" | |
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.6" | |
libraryDependencies += "com.typesafe.akka" %% "akka-kernel" % "2.3.6" | |
libraryDependencies += "com.typesafe.akka" %% "akka-http-core-experimental" % akka_http | |
libraryDependencies += "com.typesafe.akka" %% "akka-http-experimental" % akka_http | |
libraryDependencies += "com.typesafe.akka" %% "akka-stream-experimental" % akka_http |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<form action="resize" method="post" enctype="multipart/form-data"> | |
Select image to upload: | |
<input type="text" name="imageId"> | |
<input type="text" name="sizes"> | |
<input type="file" name="image" id="fileToUpload"> | |
<input type="submit" value="Upload Image" name="submit"> | |
</form> | |
</body> | |
</html> |
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
package akkahttptest | |
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.server.Route | |
import akka.stream.ActorMaterializer | |
import akka.http.scaladsl.server.Directives._ | |
object UploadHandler extends App { | |
implicit val system = ActorSystem() | |
implicit val materializer = ActorMaterializer() | |
val route: Route = { | |
(path("resize") & post) { | |
extractRequestContext { c ⇒ | |
formFields('imageId, 'sizes, 'rotate ? "normal") { (imageId, sizesStr, rotateStr) => | |
uploadedFile("image") { | |
case (meta, file) => { | |
complete { | |
// do something with file, sizes, rotate ... | |
s"$meta $file" | |
} | |
} | |
} | |
} | |
} | |
} | |
} ~ getFromResourceDirectory("web") | |
val bindingFuture = Http().bindAndHandle(route, "localhost", 12345) | |
System.in.read() | |
system.shutdown() | |
system.awaitTermination() | |
} |
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
package akkahttptest | |
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.server.{RequestContext, RouteResult, Directive, Route} | |
import akka.stream.ActorMaterializer | |
import akka.http.scaladsl.server.Directives._ | |
import scala.concurrent.Future | |
import scala.concurrent.duration._ | |
object UploadHandlerToStrict extends App { | |
implicit val system = ActorSystem() | |
implicit val materializer = ActorMaterializer() | |
implicit val ec = system.dispatcher | |
def toStrict(timeout: FiniteDuration): Directive[Unit] = { | |
def toStrict0(inner: Unit ⇒ Route): Route = { | |
val result: RequestContext ⇒ Future[RouteResult] = c ⇒ { | |
// call entity.toStrict (returns a future) | |
c.request.entity.toStrict(timeout).flatMap { strict ⇒ | |
// modify the context with the strictified entity | |
val c1 = c.withRequest(c.request.withEntity(strict)) | |
// call the inner route with the modified context | |
inner()(c1) | |
} | |
} | |
result | |
} | |
Directive[Unit](toStrict0) | |
} | |
val route: Route = { | |
(path("resize") & post) { | |
toStrict(1.second) { | |
formFields('imageId, 'sizes, 'rotate ? "normal") { (imageId, sizesStr, rotateStr) => | |
uploadedFile("image") { | |
case (meta, file) => { | |
complete { | |
// do something with file, sizes, rotate ... | |
s"$meta $file" | |
} | |
} | |
} | |
} | |
} | |
} | |
} ~ getFromResourceDirectory("web") | |
val bindingFuture = Http().bindAndHandle(route, "localhost", 12345) | |
System.in.read() | |
system.shutdown() | |
system.awaitTermination() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment