Skip to content

Instantly share code, notes, and snippets.

@andyczerwonka
Last active December 10, 2015 14:19
Show Gist options
  • Save andyczerwonka/4447120 to your computer and use it in GitHub Desktop.
Save andyczerwonka/4447120 to your computer and use it in GitHub Desktop.
I'm trying to serve an image from Play, where the image originates from Gravatar. This code doesn't seem to work, but the URL is correct.
import play.api._
import play.api.mvc._
import play.api.libs.ws.WS
import play.api.libs.concurrent.Execution.Implicits._
import java.io.File
object Application extends Controller {
def index(email: String) = Action {
Async {
WS.url("http://www.gravatar.com/avatar/" + hash(email) + "?s=32&r=pg&d=mm").withTimeout(5000).get
.map(image => Ok(image.ahcResponse.getResponseBodyAsBytes()).as("image/jpg"))
.recover {
case error =>
Ok.sendFile(content = new File("public/app/img/generic_user.png"), inline = true).as("image/png")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment