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)
Does this help?
Thanks,
David
On Wed, Mar 25, 2009 at 2:38 PM, Thomas Rynne <[email protected]>wrote:
>
> Hi,
> I want to dynamically generate images (actually a graph). I could
> write a seperate servlet for this but I'd like easy access to the
> Mapper classes and the logged in User etc so would prefer to write it
> as a method in lift.
>
> Is there a hook for this? I suppose I essentially want direct acccess
> to the httpresponse.
>
> I found some discussion of this here:
>
> http://groups.google.com/group/liftweb/browse_thread/thread/9d6f61f69a20765/d98a32e89e87d317
>
> but I don't know if it was ever implemented, and need some pointers/
> documentation if it has been.
>
> thanks for any advice,
> Thomas
>
> >
>
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---