[Checkins] SVN: z3c.blobfile/trunk/ Put a ceiling (64k) on the number of info bytes read and read those bytes in one shot

Laurence Rowe l at lrowe.co.uk
Fri Feb 11 13:29:53 EST 2011


Log message for revision 120286:
  Put a ceiling (64k) on the number of info bytes read and read those bytes in one shot

Changed:
  U   z3c.blobfile/trunk/CHANGES.txt
  U   z3c.blobfile/trunk/src/z3c/blobfile/image.py

-=-
Modified: z3c.blobfile/trunk/CHANGES.txt
===================================================================
--- z3c.blobfile/trunk/CHANGES.txt	2011-02-11 18:16:51 UTC (rev 120285)
+++ z3c.blobfile/trunk/CHANGES.txt	2011-02-11 18:29:53 UTC (rev 120286)
@@ -5,6 +5,9 @@
 0.1.5 (unreleased)
 ------------------
 
+- Put a ceiling on the number of info bytes read and read those bytes in one
+  shot.
+
 - Bug: Correctly detect the dimensions of JPEG images with the dimensions
   in a position greater than IMAGE_INFO_BYTES.
 

Modified: z3c.blobfile/trunk/src/z3c/blobfile/image.py
===================================================================
--- z3c.blobfile/trunk/src/z3c/blobfile/image.py	2011-02-11 18:16:51 UTC (rev 120285)
+++ z3c.blobfile/trunk/src/z3c/blobfile/image.py	2011-02-11 18:29:53 UTC (rev 120286)
@@ -31,6 +31,7 @@
 import interfaces
 
 IMAGE_INFO_BYTES = 1024
+MAX_INFO_BYTES = 1 << 16
 
 class Image(File):
     implements(interfaces.IBlobImage)
@@ -39,26 +40,26 @@
         super(Image, self)._setData(data)
         firstbytes = self.getFirstBytes()
         res = getImageInfo(firstbytes)
-        while res == ('image/jpeg', -1, -1):
+        if res == ('image/jpeg', -1, -1):
             # header was longer than firstbytes
-            firstbytes += self.getFirstBytes(len(firstbytes))
+            start = len(firstbytes)
+            length = max(0, MAX_INFO_BYTES - start)
+            firstbytes += self.getFirstBytes(start, length)
             res = getImageInfo(firstbytes)
-            if len(firstbytes) >= self.size:
-                break
         contentType, self._width, self._height = res
         if contentType:
             self.contentType = contentType
 
     data = property(File._getData, _setData)
 
-    def getFirstBytes(self, start=0):
+    def getFirstBytes(self, start=0, length=IMAGE_INFO_BYTES):
         """Returns the first bytes of the file.
         
         Returns an amount which is sufficient to determine the image type.
         """
         fp = self.open('r')
         fp.seek(start)
-        firstbytes = fp.read(IMAGE_INFO_BYTES)
+        firstbytes = fp.read(length)
         fp.close()
         return firstbytes
     



More information about the checkins mailing list