Changeset 6528
- Timestamp:
- 23 Jul 2011, 02:22:13 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/imagestorage.py
r6519 r6528 25 25 import hashlib 26 26 import os 27 import transaction 28 import warnings 27 29 from StringIO import StringIO 28 30 from ZODB.blob import Blob 29 31 from persistent import Persistent 30 32 from hurry.file.interfaces import IFileRetrieval 31 from zope.container.contained import Contained32 33 from waeup.sirp.image import WAeUPImageFile 33 34 from waeup.sirp.utils.helpers import cmp_files … … 45 46 """A basket holds a set of image files with same hash. 46 47 """ 48 47 49 def _del(self): 48 50 """Remove temporary files associated with local blobs. … … 71 73 72 74 def getInternalId(self, fd): 75 """Get the basket-internal id for the file stored in `fd`. 76 77 `fd` must be a file open for reading. If an (byte-wise) equal 78 file can be found in the basket, its internal id (basket id) 79 is returned, ``None`` otherwise. 80 """ 81 fd.seek(0) 73 82 for key, val in self.items(): 74 fd.seek(0)75 83 fd_stored = val.open('r') 84 file_len = os.stat(fd_stored.name)[6] 85 if file_len == 0: 86 # Nasty workaround. Blobs seem to suffer from being emptied 87 # accidentally. 88 site = grok.getSite() 89 if site is not None: 90 site.logger.warn( 91 'Empty Blob detected: %s' % fd_stored.name) 92 warnings.warn("EMPTY BLOB DETECTED: %s" % fd_stored.name) 93 fd_stored.close() 94 val.open('w').write(fd.read()) 95 return key 96 fd_stored.seek(0) 76 97 if cmp_files(fd, fd_stored): 77 98 fd_stored.close() … … 82 103 @property 83 104 def curr_id(self): 105 """The current basket id. 106 107 An integer number which is not yet in use. If there are 108 already `maxint` entries in the basket, a :exc:`ValueError` is 109 raised. The latter is _highly_ unlikely. It would mean to have 110 more than 2**32 hash collisions, i.e. so many files with the 111 same MD5 sum. 112 """ 84 113 num = 1 85 114 while True: … … 92 121 93 122 def storeFile(self, fd, filename): 94 internal_id = self.getInternalId(fd) 123 """Store the file in `fd` into the basket. 124 125 The file will be stored in a Blob. 126 """ 127 fd.seek(0) 128 internal_id = self.getInternalId(fd) # Moves file pointer! 95 129 if internal_id is None: 96 130 internal_id = self.curr_id 97 self[internal_id] = Blob(fd.read()) 131 fd.seek(0) 132 self[internal_id] = Blob() 133 transaction.commit() # Urgently needed to make the Blob 134 # persistent. Took me ages to find 135 # out that solution, which makes some 136 # design flaw in ZODB Blobs likely. 137 self[internal_id].open('w').write(fd.read()) 138 fd.seek(0) 139 self._p_changed = True 98 140 return internal_id 99 141 100 142 def retrieveFile(self, basket_id): 143 """Retrieve a file open for reading with basket id `basket_id`. 144 145 If there is no such id, ``None`` is returned. It is the 146 callers responsibility to close the open file. 147 """ 101 148 if basket_id in self.keys(): 102 149 return self[basket_id].open('r') … … 156 203 if storage is None: 157 204 raise ValueError('Cannot find an image storage') 205 result = storage.retrieveFile(data) 206 if result is None: 207 return StringIO(data) 158 208 return storage.retrieveFile(data) 159 209
Note: See TracChangeset for help on using the changeset viewer.