Skip to content

Instantly share code, notes, and snippets.

@nicmarti
Created August 11, 2011 17:11
Show Gist options
  • Save nicmarti/1140203 to your computer and use it in GitHub Desktop.
Save nicmarti/1140203 to your computer and use it in GitHub Desktop.
Loading an image from the database with play! scala
/**
* Loads the specified documentInstance, the current authenticated user is retrieved
* to check that it's the document owner.
* Play! Framework with latest Scala module.
* Nicolas Martignole - 09/08/2011
*/
def loadImage(di:Option[Long]) = {
import play.utils.Scala.MayErr._
val result=(
for (
docInstanceId:Long <- di.toRight(Error("Parameter not found"));
val maybeUser = session("connected").flatMap(Webuser.lookup(_));
webuser<-maybeUser.toRight(Unauthorized) ;
val maybeDocInstance = DocumentInstance.find("id={id} and owner={owner}").on("id"->docInstanceId,"owner"->webuser.id()).first() ;
docInstance <- maybeDocInstance.toRight(NotFound("document instance not found"));
val zeFile:VirtualFile = VirtualFile.fromRelativePath(docInstance.url);
val _ = {
// why do I need a val _ ?
Http.Response.current().setContentTypeIfNotSet("image/jpeg");
}
) yield zeFile)
result.fold(identity, f=>f.inputstream())
}
@sadache
Copy link

sadache commented Aug 12, 2011

@glaforge are you sure you know what it is doing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment