My goals are to be able to fetch and display user uploaded images from
the DB in a way that won't bog down a high-traffic production server.
Something like what eBay or Amazon does in their search result pages.
I found an old thread on dynamic image generation that might be
relevant to what I'm doing. Couldn't reply to the old thread, so I
thought I'd make a new one. Here's the original:
http://groups.google.com/group/liftweb/browse_thread/thread/a4a04b145128e6f8/cb5e7026cb361fa6?lnk=gst&q=image#cb5e7026cb361fa6
I have a boat load of questions about David's code, and I can't find
any documentation that has a clear explanation of what's going on.
Here's the code:
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))
}
}
Questions
1. I see "unapply" it in the Req object's methods list in the API, but
I have NO idea what it's doing. Lift book has 1 sentence about it, but
I'm still lost.
2. What's the difference between DispatchPF and dispatch.append? I see
no documentation on what DispatchPF is, but from reading the API it
sounds like some sort of member object or something to be associated
with custom dispatches.
3. What is the "r @ Req" syntax? I'm having a hard time searching for
the syntax on a web search engine. Is it specific to Lift, or is it
something in Scala?
4. For the "servImage" method, what is the effect of the HTTP 304 code
if the image hasn't been modified? I looked it up and it's "Error Not
Modified". Will this image generation fail?
Other, more academic curiosities:
1. What's the advantage of using the lookup for the image class (GUID
vs primary key)? The image class in the original thread has lookup
extending MappedUniqueId, which is a randomly generated unique id. Is
there some security issue for using the Long PK of the image if I'm
going to be serving images (e.g. "
site.com/image/4", where 4 is the Id
of the image instance in the DB), or is having a indexed lookup field
inherently better?
2. What are the performance/compatibility issues with using
StreamingResponse vs InMemoryResponse for the LiftResponse? If I'm
going to be displaying these images I have on a search results page
with 30 or more images, should I be using one or the other? Is
StreamingResponse even compatible with generating images in this
scenario?
3. When the site I'm building moves to production, I'd like to
incorporate EHCache or some other (forum recommended ;) ) image
caching tools to ease the load on the production server. I'd also like
to use subdomains for image servers...something like
img1.site.com,
img2.site.com. I'm not sure how to go about it all really, have to do
more research on multiple image servers and such. Is the dynamic image
generation the best method to move forward, or is there another
alternative method to fetch and display user uploaded images from the
database?
Thanks,
Strom