Last active
December 10, 2015 14:19
-
-
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.
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
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