[Seaside] File downloads etc

Avi Bryant avi.bryant at gmail.com
Tue Jan 10 10:31:15 CET 2006

On Jan 10, 2006, at 12:13 AM, goran at krampe.se wrote:
> The question is: How do people serve files with Seaside? Especially
> static files like images? And no, you don't need to reply if you use
> Apache or HV2 on the side :)

I use Apache on the side :)  But anyway...
> I started digging of course and found WADocumentHandler - but
> uncommented, as well as all the handler classes in fact, ehrm, <insert
> coughs here>. IMHO this is a lacking in Seaside - just a *teeeeny* bit
> too few class comments. :)  ...and why does  
> WADocumentHandler>>response
> send #text to the content stream btw?

Good question.  I assume there's some good reason, but I honestly  
don't remember what.

> Anyway, suppose we add these two methods to WAImageTag:
> WAImageTag>>fileName: aFileName mimeType: mimeString
> 	"Create a document handler for the file contents and insert
> 	a URL for it. The document will be served binary and with
> 	a non expiring cache."
> 	^self url: (canvas context
> 				urlForDocument: ((StandardFileStream readOnlyFileNamed: aFileName)
> binary; contentsOfEntireFile)
> 				mimeType: mimeString
> 				fileName: aFileName).

Hm, well, the problem with doing that is that although you're caching  
the contents in the image, you're also reading the file from disk  
every time, which does seem awfully wasteful.  The right thing to do  
here is to add a level of abstraction and create a new document  
class.  What #urlForDocument: is expecting is something with the  
following two properties:

- it responds reasonably to #asMIMEDocument
- it has a reasonable implementation of #= (most likely, it is #= to  
other things that produce the identical mime document)

Beyond that, you can be as smart as you like.  So, I would do  
something like:

- Create a FileDocument class that holds onto the file name or path
- Two FileDocuments with the same path are #=
- #asMIMEDocument reads in the file stream on demand.  It may cache  
it, with whatever caching policy seems reasonable, check the mtime of  
the file etc.

For bonus points, have #= take the mtime of the cached version into  
account so that two FileDocuments are *not* equal if their mtimes  
differ; this means a new URL will be generated every time the file on  
disk changes and so clients will immediately get the updated content.

> I assume these handlers stick around for the session lifetime?  
> Hmmm, ok
> even longer
> (http://lists.squeakfoundation.org/pipermail/seaside/2005-February/ 
> 00441
> 1.html).

Yes, they stick around effectively forever, so be careful with them...

> End usage with WACanvas:
> MyComponent>>renderContentOn: html
> 	html image fileName: 'myimage.gif'

Certainly can't complain about the final API :)

> PS. I assume WAImageTag>>resourceUrl: is for referring to stuff served
> by some other server? Or have I missed something?

Yep, that's the idea.


More information about the Seaside mailing list