Re: [Zope] Set MIME type using ZPT

2005-04-06 Thread Cliff Ford
srikanth wrote:
Hi,
   I tried this one. But the html page takes the parameter as a string
but doenst 
Execute the pyhton function. I got all the functions (External methods)
all set.

The img tag I used is:
span tal:define=imagename result/filenameImg
src=loadImage?filename=imagename/span
Have a look at the generated html source. You have not said wither the 
probem is a mal-formed image tag, or the image tag failing to fetch the 
image.

Also, pay attention to Tino's comments on seurity. I only ever use a 
Serial Number, not Filename - that comes from a database.

Don't forget that image tags should have width, height and alt attributes.
Cliff
Because I am getting the filename from the resultset.
Once again thanks for all your time and patience.
Any help would be gr8.
Ta.
Srikanth.
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of
Cliff Ford
Sent: Monday, April 04, 2005 11:15 PM
Cc: zope@zope.org
Subject: Re: [Zope] Set MIME type using ZPT
srikanth wrote:
Hi,
 I am using an external method to load an Image from the harddrive. 
The external method is as follows:

from email.MIMEImage import MIMEImage
##parameters=filename
def getDocument(filename):
 fname = '/mnt/'+filename;
input = open(fname,'r')
content = MIMEImage( input.read( ) )
input.close( )
return content
When I try to display the content in the webpage what I actually got 
is all raw data of the file rather the image. So how can I convert the

raw data to be dispalyed as image in the webpage. I am using ZPT to 
display the web page (image). If its dtml I could have used 
dtml-mime tag is there any equivalent to that in ZPT.

Any suggestion would be a gr8 help.

It is not clear exactly how you are using the Page Template. Typically 
the page would have an img tag that calls a python script that calls the

External Method. Remember the web browser fetches the image separately 
after the html has been received - so your img tag might look like this:
img src=getImage?filename=whatever ... and your getImage python 
script would look like this:

(type, encoding) = context.getMimeType(context.REQUEST.filename)
context.REQUEST.RESPONSE.setHeader('Content-Type', type)
context.REQUEST.RESPONSE.setHeader('Content-Disposition', 'inline;
return context.getDocumentCall(context.REQUEST.filename)
where getDocumentCall is the name of your External Method that calls the
getDocument External Method and getMimeType is another External Method 
that looks like this:

import mimetypes
def getMimeType(filename):
 return mimetypes.guess_type(filename)
and your own external method would look like this:
  ##parameters=filename
  def getDocument(filename):
fname = '/mnt/'+filename;
input = open(fname,'r')
content = input.read( )
input.close( )
return content
At the moment you seem to have skipped a step.
HTH
Cliff

Ta.
___
Zope maillist  -  Zope@zope.org 
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Set MIME type using ZPT

2005-04-05 Thread Dieter Maurer
srikanth wrote at 2005-4-5 13:11 -0700:
  I am using an external method to load an Image from the harddrive. The
external method is as follows:

from email.MIMEImage import MIMEImage
##parameters=filename
def getDocument(filename):
  fname = '/mnt/'+filename;
   input = open(fname,'r')
   content = MIMEImage( input.read( ) )
   input.close( )
   return content


When I try to display the content in the webpage what I actually got is
all raw data of the file rather the image. 
So how can I convert the raw data to be dispalyed as image in the
webpage. I am using ZPT to display the web page (image).

Apparently, you do not yet fully understand HTML and ZPT.

HTML (and therefore ZPT) is *NOT* intended to present
binary data (such as images).

I expect you do not understand MIMEImage as well.
I do not know what it is for but almost surely, it is not
for presentation via HTTP...

If its dtml I
could have used dtml-mime tag is there any equivalent to that in ZPT.

And it would not work. dtml-mime is for construction of an email message
from DTML and not for a webpage presentation.


Step back. Read a good book about Web publishing, HTML and HTTP.
Learn about how to present binary information in web pages
(i.e. via HTTP) -- you will need the Content-Type header
of HTTP responses.
Read about Zope's REQUEST and RESPONSE objects (in the Zope book or

  http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html)


You will *not* need a ZPT (nor DTML) for presentation
of binary content. You use then only for textual content.

-- 
Dieter
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Set MIME type using ZPT

2005-04-05 Thread Tino Wildenhain
Am Dienstag, den 05.04.2005, 13:11 -0700 schrieb srikanth:
 Hi,
 
   I am using an external method to load an Image from the harddrive. The
 external method is as follows:
 
 from email.MIMEImage import MIMEImage
 ##parameters=filename
 def getDocument(filename):
   fname = '/mnt/'+filename;
   input = open(fname,'r')
   content = MIMEImage( input.read( ) )
   input.close( )
   return content
 
 
 When I try to display the content in the webpage what I actually got is
 all raw data of the file rather the image. 
 So how can I convert the raw data to be dispalyed as image in the
 webpage. I am using ZPT to display the web page (image). If its dtml I
 could have used dtml-mime tag is there any equivalent to that in ZPT.

This is not a domain for templating languages like DTML or ZPT.
Images are read seperately by the client (get a sniffer like
shanes tcpwatch or tcpflow and see whats going on there)
So your external method has to set the headers.
Unfortunalely you open a huge security hole with your solution
and by not reading the python documentation carefully ;)
fname = '/mnt/'+filename; ---
if I put a filename like ../etc/passwd I start to have fun.

Ah, and image data for web pages are raw. So you dont need
mimeimage alltogether. Just open().read() 
Again, if you want performance, you should use:

from ZPublisher.Iterators import filestream_iterator

return filestream_iterator(filenameondisk,'rb')

Regards
Tino


___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )