Upload just file headers?
I'd like to make a form that looks like the typical file upload form, but will upload ONLY the header of the chosen file. Anyone know how / if this can be done? -- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
Still haven't found an answer, after 4 hours searching the egroups archive. Just to clarify: I need to read the headers of the files BEFORE I upload the entire file, because the files may be very large and I need to be able to give a warning if the file does not meet certain criteria. Example: a jpeg file of 500x400 pixels, approx. 200 kb when the application, to be used in an application that needs much higher resolution in order to look good. I want to avoid wasting 50 seconds or so uploading this file just to find out that it's no good. If I could upload the first bit of the file (1k should be plenty), I could open it with PIL and check the size attribute. (I've verified this by doing head --bytes 500 foo.jpeg > test.jpeg and then opening test.jpeg in python - it works even though the file has been truncated.) I wrote:
I'd like to make a form that looks like the typical file upload form, but will upload ONLY the header of the chosen file. Anyone know how / if this can be done?
-- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
Hmm, well you certainly can't do it at the browser side. A well-formed HTTP header should contain a content-length attribute which you can examine. However, deciding whether to accept the file or not would I imagine not be possible inside zope since the whole POST operation is handled by ZServer, so you probably have to write a cgi script to do it. For example, if you wanted to stick with python, you could use the cgi module. You'd examine the header, and if you were happy, you could pass on the data in a new POST to zope. If you were unhappy, you could return a value from the cgi. This is all off the top of my head, and I'm sure there must be a nicer way of doing it than that. It's not nice having it all outside zope. but it should be a start, at least. seb
-----Original Message----- From: pw@bestweb.net [mailto:pw@bestweb.net]On Behalf Of Paul Winkler Sent: 11 October 2000 06:52 To: Zope mailing list Subject: [Zope] Re: Upload just file headers?
Still haven't found an answer, after 4 hours searching the egroups archive.
Just to clarify: I need to read the headers of the files BEFORE I upload the entire file, because the files may be very large and I need to be able to give a warning if the file does not meet certain criteria. Example: a jpeg file of 500x400 pixels, approx. 200 kb when the application, to be used in an application that needs much higher resolution in order to look good. I want to avoid wasting 50 seconds or so uploading this file just to find out that it's no good.
If I could upload the first bit of the file (1k should be plenty), I could open it with PIL and check the size attribute. (I've verified this by doing head --bytes 500 foo.jpeg > test.jpeg and then opening test.jpeg in python - it works even though the file has been truncated.)
I wrote:
I'd like to make a form that looks like the typical file upload form, but will upload ONLY the header of the chosen file. Anyone know how / if this can be done?
-- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Thanks for the reply! Seb Bacon wrote:
Hmm, well you certainly can't do it at the browser side.
Yes, I want to do it on the server.
A well-formed HTTP header should contain a content-length attribute which you can examine. However, deciding whether to accept the file or not would I imagine not be possible inside zope since the whole POST operation is handled by ZServer, so you probably have to write a cgi script to do it. For example, if you wanted to stick with python, you could use the cgi module. You'd examine the header, and if you were happy, you could pass on the data in a new POST to zope. If you were unhappy, you could return a value from the cgi.
Hmm... so I couldn't put this in an external method? I guess CGI would be okay. I should clarify: It's not actually the HTTP header I want to examine, it's the first few bytes of the data itself. I need to upload enough of the data to read some useful info, then stop ASAP.
This is all off the top of my head, and I'm sure there must be a nicer way of doing it than that. It's not nice having it all outside zope. but it should be a start, at least.
seb
-- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
Paul, I'm sorry if I'm dropping context here, but I'm not sure what you mean when you say "headers of files". Do you just want to check the size of files before you put them in the ZODB? ----- Original Message ----- From: "Paul Winkler" <slinkp23@yahoo.com> To: "Zope mailing list" <zope@zope.org> Sent: Wednesday, October 11, 2000 1:51 AM Subject: [Zope] Re: Upload just file headers?
Still haven't found an answer, after 4 hours searching the egroups archive.
Just to clarify: I need to read the headers of the files BEFORE I upload the entire file, because the files may be very large and I need to be able to give a warning if the file does not meet certain criteria. Example: a jpeg file of 500x400 pixels, approx. 200 kb when the application, to be used in an application that needs much higher resolution in order to look good. I want to avoid wasting 50 seconds or so uploading this file just to find out that it's no good.
If I could upload the first bit of the file (1k should be plenty), I could open it with PIL and check the size attribute. (I've verified this by doing head --bytes 500 foo.jpeg > test.jpeg and then opening test.jpeg in python - it works even though the file has been truncated.)
I wrote:
I'd like to make a form that looks like the typical file upload form, but will upload ONLY the header of the chosen file. Anyone know how / if this can be done?
-- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Another way to look at it: I want to start uploading the file, but stop the upload almost immediately - after I've got enough to read the file header, but long before the upload would normally finish. Chris McDonough wrote:
Paul,
I'm sorry if I'm dropping context here, but I'm not sure what you mean when you say "headers of files".
Do you just want to check the size of files before you put them in the ZODB?
That would be useful, but I need more information than that. I need to read some of the file's description of itself that's commonly kept in a header at the beginning of a file. For example, take a .gif or .jpeg file and throw away all but the first (let's say) 1 kb of the file. If you try to open this truncated file with an image editor (gimp, photoshop, whatever) you'll have problems. But useful information is still stored in that first part of the file, as we can see using python and PIL. Example, on linux: head --bytes 1000 foo.gif > foo_truncated.gif python ...
import Image test = Image.open('foo_truncated.gif', 'r') test.size (210, 210)
So in this case, we can find out that foo.gif is 210 x 210 pixels even if we only have access to the first 1 kb of the 12 kb file. If the file in question is 1 MB or so, this trick becomes really useful. Suppose your web application needs an image about 1000 x 1000 pixels. You could warn the user if their image is too small to look good, or so big it's a waste of their time. So basically what I want to do is start uploading the file, but stop quickly. Now then, does anyone know how to do it? Thanks again, Paul -- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
Here's an idea for you. I use some of the code below in my FileBase product to receive uploads, although, I've added some "code" I think might help you for grabbing just the header. Although I'm not sure if the file descriptor that infile claims from the form has already been loaded into either a StringIO or some other "buffer" (i.e. already received in full from client), it's possible that this is reading directly from the http stream coming from the client. infilename = REQUEST.form['attached_file'].filename # Get filename infile = REQUEST.form['attached_file'] # Get the file descriptor myheader = infile.read(500) # Read up to 500 bytes infile.close() # Close the stream This should be in an external method or product. I haven't tested whether or not this will only let the client upload the first 500 bytes, it's very possible zope takes in the entire file, and puts it into a buffer that is like a file descriptor (hence StringIO). Knight knight@phunc.com On Wed, 11 Oct 2000, Paul Winkler wrote:
Another way to look at it: I want to start uploading the file, but stop the upload almost immediately - after I've got enough to read the file header, but long before the upload would normally finish.
Chris McDonough wrote:
Paul,
I'm sorry if I'm dropping context here, but I'm not sure what you mean when you say "headers of files".
Do you just want to check the size of files before you put them in the ZODB?
That would be useful, but I need more information than that. I need to read some of the file's description of itself that's commonly kept in a header at the beginning of a file.
For example, take a .gif or .jpeg file and throw away all but the first (let's say) 1 kb of the file. If you try to open this truncated file with an image editor (gimp, photoshop, whatever) you'll have problems. But useful information is still stored in that first part of the file, as we can see using python and PIL. Example, on linux:
head --bytes 1000 foo.gif > foo_truncated.gif python ...
import Image test = Image.open('foo_truncated.gif', 'r') test.size (210, 210)
So in this case, we can find out that foo.gif is 210 x 210 pixels even if we only have access to the first 1 kb of the 12 kb file.
If the file in question is 1 MB or so, this trick becomes really useful. Suppose your web application needs an image about 1000 x 1000 pixels. You could warn the user if their image is too small to look good, or so big it's a waste of their time.
So basically what I want to do is start uploading the file, but stop quickly.
Now then, does anyone know how to do it?
Thanks again,
Paul
-- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Much thanks to Knight and to Peter Bengtsson (who replied off-list), I know have at least two possible avenues to explore. If I ever get this to work, I'll post it! -- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
Much thanks to Knight and to Peter Bengtsson (who replied off-list), I know have at least two possible avenues to explore.
If I ever get this to work, I'll post it!
Please do - we've got people uploading PowerPoints of up to 25-30 Megabytes (record was an attempt at 170 Mb) and we'd like to offer a software solution before we have to 'send the lads round'. Cheers Tone.
Actually, I replied on list, but the phenomina of receiving your CC before the list replicated my original reply occured. ;] Knight knight@phunc.com On Wed, 11 Oct 2000, Paul Winkler wrote:
Much thanks to Knight and to Peter Bengtsson (who replied off-list), I know have at least two possible avenues to explore.
If I ever get this to work, I'll post it!
-- ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
On Wed, 11 Oct 2000 15:41:16 -0700 (PDT), knight <knight@righteous.net> wrote:
infilename = REQUEST.form['attached_file'].filename # Get filename infile = REQUEST.form['attached_file'] # Get the file descriptor myheader = infile.read(500) # Read up to 500 bytes infile.close() # Close the stream
This should be in an external method or product.
I haven't tested whether or not this will only let the client upload the first 500 bytes
Im sure it wont
it's very possible zope takes in the entire file, and puts it into a buffer that is like a file descriptor (hence StringIO).
File uploads are spooled into a temporary file, and the request is only dispatched into Zope once the whole request has arrived. Anything smarter than that will require some ZServer hacking. Toby Dickenson tdickenson@geminidataloggers.com
On Wed, Oct 11, 2000 at 03:54:17PM -0400, Paul Winkler wrote:
For example, take a .gif or .jpeg file and throw away all but the first (let's say) 1 kb of the file. If you try to open this truncated file with an image editor (gimp, photoshop, whatever) you'll have problems. But useful information is still stored in that first part of the file, as we can see using python and PIL. Example, on linux:
head --bytes 1000 foo.gif > foo_truncated.gif python ...
import Image test = Image.open('foo_truncated.gif', 'r') test.size (210, 210)
So in this case, we can find out that foo.gif is 210 x 210 pixels even if we only have access to the first 1 kb of the 12 kb file.
This doesn't hold true for JPEG's. A JPEG encoded image consists of a set of segments, each encoding some aspect of the image. The image dimensions can be found in the frame header segment, but this segment may be preceded by tables, comments and application specific data. These extra segments could easily take up your 1K chunk. AP press for example, uses an application specific segment to encode image metadata such as subject, caption and photographer. If you have a look at the Image dimension detection code for JPEGs you'll see that it scans for a frame header (marked by C0, C1, C2 or C3 depending on the encoding used). Once found, it extracts the width and height. More information of the JPEG file encoding format can be found at: http://www.w3.org/Graphics/JPEG/itu-t81.pdf For a GIF file, the first 10 bytes should suffice, for a PNG you need the first 24. -- Martijn Pieters | Software Engineer mailto:mj@digicool.com | Digital Creations http://www.digicool.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
Martijn Pieters wrote:
This doesn't hold true for JPEG's. (snip)
Aha, thanks. I'll have to think of a way to deal with this... --PW ................. paul winkler .................. slinkP arts: music, sound, illustration, design, etc. web page: http://www.slinkp.com A member of ARMS: http://www.reacharms.com
participants (7)
-
Chris McDonough -
knight -
Martijn Pieters -
Paul Winkler -
Seb Bacon -
Toby Dickenson -
Tony McDonald