[Seaside] File downloads etc

goran at krampe.se goran at krampe.se
Tue Jan 10 09:13:44 CET 2006


Hi guys!

Ok, this has surely been discussed a *hundred* times - but I just
started digging into Seaside for real so forgive me. And yes, I did
search a bit with Google first. :)

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 am interested in the "Seaside way"
here. And in my current project an external web server is a no-no. Using
HV2 would be an option but I am learning Seaside now.

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?

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).

WAImageTag>>fileName: aFileName
	"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 fileName: aFileName mimeType: (MIMEDocument
guessTypeFromExtension: (FileDirectory extensionFor: aFileName))

Note how we don't send the filestream itself but fetch all its contents
first - this is because otherwise the filestream gets put into the
WAResponse which never closes it. Darn. So perhaps it was never meant to
send a FileStream into #urlForDocument:.

Now, given how it works today (the WAResponse>>writeOn: will eventually
send #contents to the stream and write that) there is no real point in
sending the stream, so I read it all into memory myself, close the
filestream and send the ByteArray or String. The downside is of course
that we end up with all these loaded into the image, but for small
images etc it is not a big deal.

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). And yes, above we could be smarter regarding where to look for
files etc.

End usage with WACanvas:

MyComponent>>renderContentOn: html
	html image fileName: 'myimage.gif'

regards, Göran

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


More information about the Seaside mailing list