[Zope] Newbie Question: Returning random images for use in <img src=''> links on other server
Oliver Bleutgen
myzope@gmx.net
Wed, 21 Nov 2001 12:45:19 +0100
Ben Chapman wrote:
> Thank you to Oliver Bleutgen for giving me the solution to this problem.
> I wanted to have a link to a zope server from a non-zope server that
> would return a random zope stored image.
>
> Here's the code that I finally used:
>
> <dtml-call expr="REQUEST.set('rand_image_id',
> _.whrandom.choice(files.objectIds(['Image','File'])))">
> <dtml-call
> "RESPONSE.setHeader('Content-Type',_.getitem(rand_image_id,0).content_type)">
>
> <dtml-call
> "RESPONSE.setHeader('Content-Length',_.getitem(rand_image_id,0).size)">
> <dtml-return "_.getitem(rand_image_id,0).data">
> <dtml-call "RESPONSE.setHeader('Expires','Thu, 01 Aug 2000 09:00:00 GMT')">
>
> Put this snippet in a DTML Document named showimages in the 'files'
> directory of your zope, along with the images and files that you wish to
> display. Then link to the code as <img
> src="http://myzopeserver/content/files/showimage"> and you'll get random
> images on your non-zope site. Caching issues are not yet addressed,
> except by returning the Expires header to the browser.
>
> If the name of the directory is not 'files', change the name after the
> '_.whrandom.choice(' to point to the correct name. If I understood
> better what I was doing, I would have it point to something that stands
> for 'current directory'; however, this is still beyond me, and (at least
> with Zope 2.1.6, if you do
> _.whrandom.choice(objectIds(['Image','File']))) and omit the name of the
> folder, the call fails. 'Self' does not work either.
>
> Thanks! As usual, improvements to this solution are welcome!
>
Ben, I thought about it, and maybe the following is a better solution:
<dtml-call "REQUEST.set('rand_img_id',_.whrandom.choice(objectIds('Image')))">
<dtml-raise "RESPONSE.redirect(_.getitem(rand_img_id,0).absolute_url())">
Explanation: Because of caching it might happen that people get a cached image,
because proxies will not be able to distinguish you images - the URI stays constant.
I'm not sure about that, but it's possible.
The above code just does a redirect to the right image, and therefore the browser
will go to the "real" URI of every image. That also make it now possible for
proxies to cache you image.
The code outside of zope stays the same.
HTH,
oliver