Hi again.
I've now found some code that actually saves an uploaded file to disk.
Now, if I could only modify this code to also store the file to the
database.
The code:
var file : FileParamHolder = _
var tmpDir = "/videos"
def storeFile (xhtml : Group) : NodeSeq = {
def saveFile () {
val dir = new File(tmpDir, file.fileName)
val output = new FileOutputStream(dir)
output.write(file.file)
output.close()
//val im = new Image
//im.image(file.file)
}
bind("form", xhtml,
"file" -> SHtml.fileUpload(file = _),
"submit" -> SHtml.submit("Upload", saveFile))
}
I've found some code that should work to serve image files:
Model:
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 = 2000 //millis
}
object mimeType extends MappedString(this, 256)
}
object Image extends Image with LongKeyedMetaMapper[Image]
Snippet:
class 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 (true)//(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)
-------------------------
The lift rules thing above won't work in my code because ImageLogic
can't be found. How can I fix that ?
Anyway, could I use my code from the top like this:
var file : FileParamHolder = _
var tmpDir = "/videos"
def storeFile (xhtml : Group) : NodeSeq = {
def saveFile () {
val dir = new File(tmpDir, file.fileName)
val output = new FileOutputStream(dir)
val im = new Image
im.image(file.file)
}
bind("form", xhtml,
"file" -> SHtml.fileUpload(file = _),
"submit" -> SHtml.submit("Upload", saveFile))
}
I feel I'm getting a little closer on something working. But I must
admit that this is a little hard (much harder that Rail for
instance.....)
Any tip on the aproach above ?
On Jul 14, 10:36 am, Timothy Perrett <[email protected]> wrote:
> OK, so what persistence type are you using? Mapper or JPA?
>
> If you plan to upload large files you will need to use the streaming
> API by adding the following in Boot.scala
>
> LiftRules.handleMimeFile = (fieldName, contentType, fileName,
> inputStream) =>
> OnDiskFileParamHolder(fieldName, contentType, fileName,
> inputStream)
>
> Once you have your images in the database, you will also have to
> stream them back out and I would recommend implementing a cache to do
> so (we have SoftReferenceCache that is thread safe in the lift-util
> package).
>
> As for uploading to S3, you will of course have to upload the file
> first to the webserver (lift app) then asynchronously push that to S3.
> Perhaps checkout JetS3t (http://to.ly/jHm) - you could do something
> neat with actors here to keep the UI smooth / slick.
>
> If you look at the JetS3t docs you'll see its possible to sent byte
> array data, which you can get from your FileParamHolder like so:
>
> theUpload.is.map(v => v.file).openOr(Array())
>
> You'll have to figure out how all this is going to fit together for
> your specific circumstances, but you should be getting the general
> idea.
>
> HTH
>
> Cheers, Tim
>
> On Jul 13, 7:24 pm, kjetilge <[email protected]> wrote:
>
>
>
> > Thanks, I've now taken the upload part of the code and integrated it
> > in to my learning project. I can now choose a file for upload, the
> > file is uploaded but not stored, after the upload is complete the page
> > displays the file details. Very good :-)
> > I'm planning to build a video training site. I will need to upload
> > images and video.
>
> > I think perhaps it's a good idea to keep the images in the database
> > and the video as a file. I have a similar site developed in rails
> > (www.fagfilm.no-no that's not what you think it is...) that stores
> > the video on Amazoon S3. So actually i'd like to be able to upload the
> > video to S3.
>
> > On Jul 13, 6:12 pm, Timothy Perrett <[email protected]> wrote:
>
> > > Hi there again,
>
> > > Please keep discussions strictly on list - its the only way for everyone
> > > to
> > > benefit from the conversation we have and interject where they feel they
> > > want to. This means you get more input from more sources and doesn't mail
> > > people directly (my personal preference).
>
> > > If your interested in how one uploads a file in Lift, checkout the sample
> > > application here:http://to.ly/jtz
>
> > > Do you have any experience of developing with Java? Which would you
> > > rather,
> > > save to database, or save to file? The impl is quite different for each.
>
> > > Cheers, Tim
>
> > > > On 13/07/2009 16:20, "kjetilge" <[email protected]> wrote:
> > > > Thanks. Well, it helps, I've learned something new, but what would
> > > > really be useful is a link or two to sample code showing how this is
> > > > done in real life. I've just recently finished reading Programming in
> > > > Scala and most of the Lift Book and I cant remember anything from this
> > > > text that would directly help me with persisting an Array[Byte] to a
> > > > database or as a file.
>
> > > > I guess I could use Scala.io or java.io for saving files perhaps ? But
> > > > WHERE should the files go ? I come from Rails where finding out these
> > > > things cost 30 sec googling. I must say that finding relevant
> > > > tutorials and (working) sample code for basic tasks in Lift is very
> > > > hard, even for a determined wannabe Lift developer like myself :-)
>
> > > > On 13/07/2009 14:39, "Timothy Perrett" <[email protected]> wrote:
>
> > > > Hi there,
>
> > > > Generally speaking, that's now how uploading in lift works - any given
> > > > upload
> > > > goes into a FileParamHolder object then you can persist it however you
> > > > would
> > > > like (filesystem, database etc) as your essentially left with
> > > > Array[Byte]...
>
> > > > Does that help?
>
> > > > Cheers, Tim
>
> > > > On 13/07/2009 14:26, "kjetilge" <[email protected]> wrote:
>
> > > >> I'm a newbie and I'm having troble with finding a hint in the Liftbook
> > > >> on how to set the path for an upload folder in Lift. In the manual
> > > >> there is an example using a variable called tmpDir. Problem is, there
> > > >> is no explanation for what the value of this variable should be. Apart
> > > >> from that, I've at least managed to load an image in the browser after
> > > >> defining a pictures folder in main/resources/toserve/pictures by
> > > >> using:
> > > >> ResourceServer.allow {
> > > >> case "pictures" :: _ => true
> > > >> }
> > > >> Then I can do: <img src="/classpath/pictures/sykkel.jpg" alt="Sykkel"/
> > > >>> , and it works :-)
> > > >> But when I set the value tmpDir="/classpath/pictures/" and try to
> > > >> upload a file I get:
> > > >> java.io.FileNotFoundException: /classpath/pictures/Lift Book.pdf
>
> > > >> I'm obviously doing something terribly wrong. Does anybody know how to
> > > >> do this ?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---