class DoUpload {private def saveFile(fp: FileParamHolder): Unit = {fp.file match {case null =>case x if x.length == 0 =>case x =>val blob = ImageBlob.create.image(x).saveMeImageInfo.create.name(fp.fileName).mimeType(fp.mimeType).blob(blob).saveMeS.notice("Thanks for the upload")S.redirectTo("/")}}def render(in: NodeSeq): NodeSeq =bind("upload", in, "file" -> SHtml.fileUpload(saveFile _))
}
--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Ended up doing this:
def show(xhtml: NodeSeq) : NodeSeq = {
if(S.post_?) {
val f : Option[FileParamHolder] = S.request match {
case Empty => Empty
case Full(req) =>
req.uploadedFiles.find(_.name == "file_upload")
}
if(f.isDefined) {
acceptFile(f.get)
}
}
return xhtml
}
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
Folks,
Lately there's been a bunch of chatter on the list about image upload and figuring out how to put the image files in the right place to serve them again.
I've written a simple example of image uploading, storing the image in the RDBMS and then serving the image. ï¿œYou can find the code at:
The file upload snippet is very simple:
class DoUpload {ᅵᅵprivate def saveFile(fp: FileParamHolder): Unit = {ᅵᅵ ᅵfp.file match {ᅵᅵ ᅵ ᅵcase null =>ᅵᅵ ᅵ ᅵcase x if x.length == 0 =>ᅵᅵ ᅵ ᅵcase x =>ᅵᅵ ᅵ ᅵ ᅵval blob = ImageBlob.create.image(x).saveMeᅵᅵ ᅵ ᅵ ᅵImageInfo.create.name(fp.fileName).mimeType(fp.mimeType).blob(blob).saveMeᅵᅵ ᅵ ᅵ ᅵS.notice("Thanks for the upload")ᅵᅵ ᅵ ᅵ ᅵS.redirectTo("/")ᅵᅵ ᅵ}ᅵᅵ}
ᅵᅵdef render(in: NodeSeq): NodeSeq =ᅵᅵbind("upload", in, "file" -> SHtml.fileUpload(saveFile _))
}
If the blob is uploaded, it's put in a row in ImageBlob and an ImageInfo row is created to store the name and mime type. ï¿œThere are two different rows so that one doesn't have to pull the whole blob back when you're checking for the upload date.
Serving the image is similarly concise:
ᅵᅵdef serveImage: LiftRules.DispatchPF = {ᅵᅵ ᅵcase req @ Req("images" :: _ :: Nil, _, GetRequest) if findFromRequest(req).isDefined =>ᅵᅵ ᅵ ᅵ() => {ᅵᅵ ᅵ ᅵ ᅵval info = findFromRequest(req).open_! // open is valid here because we just tested in the guard
ᅵᅵ ᅵ ᅵ ᅵ// Test for expirationᅵᅵ ᅵ ᅵ ᅵreq.testFor304(info.date, "Expires" -> toInternetDate(millis + 30.days)) orᅵᅵ ᅵ ᅵ ᅵ// load the blob and return itᅵᅵ ᅵ ᅵ ᅵinfo.blob.obj.map(blob => InMemoryResponse(blob.image, List(("Last-Modified", toInternetDate(info.date.is)),ᅵᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ("Expires", toInternetDate(millis + 30.days)),ᅵᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ ᅵ("Content-Type", info.mimeType.is)), Nil, ᅵ200))ᅵᅵ ᅵ ᅵ}ᅵᅵ}
If the request matches /images/xxx where xxx is a name in the ImageInfo table, check to see if we can return a 304 (not modified). ï¿œIf not, we pull the blob from the database and return it.
I hope this helps folks who are looking to manage and serve images.
Thanks,
David
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
Hi David,
Does that code still exist? Because the link is dead and I couldn't find it under your profile (http://github.com/dpp).
thanks.
Folks,
Lately there's been a bunch of chatter on the list about image upload and figuring out how to put the image files in the right place to serve them again.
I've written a simple example of image uploading, storing the image in the RDBMS and then serving the image. You can find the code at:
class DoUpload {
private def saveFile(fp: FileParamHolder): Unit = {
fp.file match {case null =>
case x if x.length == 0 =>
case x =>val blob = ImageBlob.create.image(x).saveMeImageInfo.create.name(fp.fileName).mimeType(fp.mimeType).blob(blob).saveMe
S.notice("Thanks for the upload")
S.redirectTo("/")}}
def render(in: NodeSeq): NodeSeq =
bind("upload", in, "file" -> SHtml.fileUpload(saveFile _))
}
If the blob is uploaded, it's put in a row in ImageBlob and an ImageInfo row is created to store the name and mime type. There are two different rows so that one doesn't have to pull the whole blob back when you're checking for the upload date.
Serving the image is similarly concise:
def serveImage: LiftRules.DispatchPF = {
case req @ Req("images" :: _ :: Nil, _, GetRequest) if findFromRequest(req).isDefined =>
() => {
val info = findFromRequest(req).open_! // open is valid here because we just tested in the guard
// Test for expiration
req.testFor304(info.date, "Expires" -> toInternetDate(millis + 30.days)) or
// load the blob and return it
info.blob.obj.map(blob => InMemoryResponse(blob.image, List(("Last-Modified", toInternetDate(info.date.is)),
("Expires", toInternetDate(millis + 30.days)),
If the request matches /images/xxx where xxx is a name in the ImageInfo table, check to see if we can return a 304 (not modified). If not, we pull the blob from the database and return it.
I hope this helps folks who are looking to manage and serve images.
Thanks,
David
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
--
You received this message because you are subscribed to the Google Groups "Lift" group.To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
To post to this group, send email to lif...@googlegroups.com.
--You received this message because you are subscribed to the Google Groups "Lift" group.To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
To post to this group, send email to lif...@googlegroups.com.
> On Mon, Nov 29, 2010 at 2:37 AM, Hannes <hannes...@gmx.li> wrote:
>
> Hi David,
>
> Does that code still exist? Because the link is dead and I couldn't find it under your profile (http://github.com/dpp).
>
> Then it's not in GitHub anymore.
For what it's worth, I have an image gallery based on the original image
upload example at
https://github.com/jimwise/shared/tree/master/gallery/
This is updated to work with 2.1, and includes a browseable gallery,
image rename and categorization, and image/category deletion.
--
Jim Wise
jw...@draga.com