Created
March 26, 2009 13:56
-
-
Save tjweir/86109 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| Here's some code to serve an image out of the database. Here's the Mapper | |
| definition: | |
| */ | |
| class Image extends LongKeyedMapper[Image] with IdPK { | |
| def getSingleton = Image | |
| object image extends MappedBinary(this) | |
| object lookup extends MappedUniqueId(this, 32) { | |
| override def dbIndexed_? = true | |
| } | |
| object saveTime extends MappedLong(this) { | |
| override def defaultValue = millis | |
| } | |
| object mimeType extends MappedString(this, 256) | |
| } | |
| object Image extends Image with LongKeyedMetaMapper[Image] | |
| //And here's the code that serves the image: | |
| object ImageLogic { | |
| object TestImage { | |
| def unapply(in: String): Option[Image] = | |
| Image.find(By(Image.lookup, in.trim)) | |
| } | |
| def matcher: LiftRules.DispatchPF = { | |
| case r @ Req("image_logic" :: TestImage(img) :: | |
| Nil, _, GetRequest) => () => servImage(img, r) | |
| } | |
| def servImage(img: Image, r: Req): Box[LiftResponse] = { | |
| if (r.testIfModifiedSince(img.saveTime)) | |
| Full(InMemoryResponse(new Array[Byte](0), | |
| List("Last-Modified" -> | |
| toInternetDate(img.saveTime.is)), Nil, 304)) | |
| else Full(InMemoryResponse(img.image.is, | |
| List("Last-Modified" -> | |
| toInternetDate(img.saveTime.is), | |
| "Content-Type" -> img.mimeType.is, | |
| "Content-Length" -> | |
| img.image.is.length.toString), Nil, 200)) | |
| } | |
| } | |
| //In Boot: | |
| LiftRules.dispatch.append(ImageLogic.matcher) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment