Index: /main/waeup.sirp/trunk/src/waeup/sirp/imagestorage.py
===================================================================
--- /main/waeup.sirp/trunk/src/waeup/sirp/imagestorage.py	(revision 6527)
+++ /main/waeup.sirp/trunk/src/waeup/sirp/imagestorage.py	(revision 6528)
@@ -25,9 +25,10 @@
 import hashlib
 import os
+import transaction
+import warnings
 from StringIO import StringIO
 from ZODB.blob import Blob
 from persistent import Persistent
 from hurry.file.interfaces import IFileRetrieval
-from zope.container.contained import Contained
 from waeup.sirp.image import WAeUPImageFile
 from waeup.sirp.utils.helpers import cmp_files
@@ -45,4 +46,5 @@
     """A basket holds a set of image files with same hash.
     """
+
     def _del(self):
         """Remove temporary files associated with local blobs.
@@ -71,7 +73,26 @@
 
     def getInternalId(self, fd):
+        """Get the basket-internal id for the file stored in `fd`.
+
+        `fd` must be a file open for reading. If an (byte-wise) equal
+        file can be found in the basket, its internal id (basket id)
+        is returned, ``None`` otherwise.
+        """
+        fd.seek(0)
         for key, val in self.items():
-            fd.seek(0)
             fd_stored = val.open('r')
+            file_len = os.stat(fd_stored.name)[6]
+            if file_len == 0:
+                # Nasty workaround. Blobs seem to suffer from being emptied
+                # accidentally.
+                site = grok.getSite()
+                if site is not None:
+                    site.logger.warn(
+                        'Empty Blob detected: %s' % fd_stored.name)
+                warnings.warn("EMPTY BLOB DETECTED: %s" % fd_stored.name)
+                fd_stored.close()
+                val.open('w').write(fd.read())
+                return key
+            fd_stored.seek(0)
             if cmp_files(fd, fd_stored):
                 fd_stored.close()
@@ -82,4 +103,12 @@
     @property
     def curr_id(self):
+        """The current basket id.
+
+        An integer number which is not yet in use. If there are
+        already `maxint` entries in the basket, a :exc:`ValueError` is
+        raised. The latter is _highly_ unlikely. It would mean to have
+        more than 2**32 hash collisions, i.e. so many files with the
+        same MD5 sum.
+        """
         num = 1
         while True:
@@ -92,11 +121,29 @@
 
     def storeFile(self, fd, filename):
-        internal_id = self.getInternalId(fd)
+        """Store the file in `fd` into the basket.
+
+        The file will be stored in a Blob.
+        """
+        fd.seek(0)
+        internal_id = self.getInternalId(fd) # Moves file pointer!
         if internal_id is None:
             internal_id = self.curr_id
-            self[internal_id] = Blob(fd.read())
+            fd.seek(0)
+            self[internal_id] = Blob()
+            transaction.commit() # Urgently needed to make the Blob
+                                 # persistent. Took me ages to find
+                                 # out that solution, which makes some
+                                 # design flaw in ZODB Blobs likely.
+            self[internal_id].open('w').write(fd.read())
+            fd.seek(0)
+            self._p_changed = True
         return internal_id
 
     def retrieveFile(self, basket_id):
+        """Retrieve a file open for reading with basket id `basket_id`.
+
+        If there is no such id, ``None`` is returned. It is the
+        callers responsibility to close the open file.
+        """
         if basket_id in self.keys():
             return self[basket_id].open('r')
@@ -156,4 +203,7 @@
         if storage is None:
             raise ValueError('Cannot find an image storage')
+        result = storage.retrieveFile(data)
+        if result is None:
+            return StringIO(data)
         return storage.retrieveFile(data)
 
