/
file.py
451 lines (353 loc) · 12.6 KB
/
file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# The implementations in this file are largely borrowed
# from zope.app.file and z3c.blobfile
# and are licensed under the ZPL.
from DateTime import DateTime
from logging import getLogger
from persistent import Persistent
from plone.namedfile.interfaces import INamedBlobFile
from plone.namedfile.interfaces import INamedBlobImage
from plone.namedfile.interfaces import INamedFile
from plone.namedfile.interfaces import INamedImage
from plone.namedfile.interfaces import IStorage
from plone.namedfile.utils import get_contenttype
from plone.namedfile.utils import get_exif
from plone.namedfile.utils import getImageInfo
from plone.namedfile.utils import rotate_image
from ZODB.blob import Blob
from zope.component import getUtility
from zope.interface import implementer
from zope.schema.fieldproperty import FieldProperty
from ZPublisher import HTTPRangeSupport
import piexif
import transaction
log = getLogger(__name__)
MAXCHUNKSIZE = 1 << 16
IMAGE_INFO_BYTES = 1024
MAX_INFO_BYTES = 1 << 18
class FileChunk(Persistent):
"""Wrapper for possibly large data"""
next = None
def __init__(self, data):
self._data = data
def __getslice__(self, i, j):
return self._data[i:j]
def __len__(self):
data = bytes(self)
return len(data)
def _get_contents(self):
next = self.next
if next is None:
return self._data
result = [self._data]
while next is not None:
self = next
result.append(self._data)
next = self.next
return b"".join(result)
__bytes__ = _get_contents
FILECHUNK_CLASSES = [FileChunk]
try:
from zope.app.file.file import FileChunk as zafFileChunk
FILECHUNK_CLASSES.append(zafFileChunk)
except ImportError:
pass
class ModifiedPropertyMixin:
@property
def modified(self):
if hasattr(self, "_modified"):
return self._modified / 1000
# Fall back to modification time in database.
return self._p_mtime
@implementer(INamedFile)
class NamedFile(Persistent, ModifiedPropertyMixin):
"""A non-BLOB file that stores a filename
Let's test the constructor:
>>> file = NamedFile()
>>> file.contentType
''
>>> file.data
''
>>> file = NamedFile('Foobar')
>>> file.contentType
''
>>> file.data
'Foobar'
>>> file = NamedFile('Foobar', 'text/plain')
>>> file.contentType
'text/plain'
>>> file.data
'Foobar'
>>> file = NamedFile(data='Foobar', contentType='text/plain')
>>> file.contentType
'text/plain'
>>> file.data
'Foobar'
Let's test the mutators:
>>> file = NamedFile()
>>> file.contentType = 'text/plain'
>>> file.contentType
'text/plain'
>>> file.data = 'Foobar'
>>> file.data
'Foobar'
>>> file.data = None
Traceback (most recent call last):
...
TypeError: Cannot set None data on a file.
Let's test large data input:
>>> file = NamedFile()
Insert as string:
>>> file.data = 'Foobar'*60000
>>> file.getSize()
360000
>>> file.data == 'Foobar'*60000
True
Insert data as FileChunk:
>>> fc = FileChunk('Foobar'*4000)
>>> file.data = fc
>>> file.getSize()
24000
>>> file.data == 'Foobar'*4000
True
Insert data from file object:
>>> import cStringIO
>>> sio = cStringIO.StringIO()
>>> sio.write('Foobar'*100000)
>>> sio.seek(0)
>>> file.data = sio
>>> file.getSize()
600000
>>> file.data == 'Foobar'*100000
True
Last, but not least, verify the interface:
>>> from zope.interface.verify import verifyClass
>>> INamedFile.implementedBy(NamedFile)
True
>>> verifyClass(INamedFile, NamedFile)
True
"""
filename = FieldProperty(INamedFile["filename"])
def __init__(self, data=b"", contentType="", filename=None):
if filename is not None and contentType in ("", "application/octet-stream"):
contentType = get_contenttype(filename=filename)
self.data = data
self.contentType = contentType
self.filename = filename
self._modified = DateTime().millis()
def _getData(self):
if isinstance(self._data, tuple(FILECHUNK_CLASSES)):
return bytes(self._data)
else:
return self._data
def _setData(self, data):
self._modified = DateTime().millis()
# Handle case when data is a string
if isinstance(data, str):
data = data.encode("UTF-8")
if isinstance(data, bytes):
self._data, self._size = FileChunk(data), len(data)
return
# Handle case when data is None
if data is None:
raise TypeError("Cannot set None data on a file.")
# Handle case when data is already a FileChunk
if isinstance(data, tuple(FILECHUNK_CLASSES)):
size = len(data)
self._data, self._size = data, size
return
# Handle case when data is a file object
seek = data.seek
read = data.read
seek(0, 2)
size = end = data.tell()
if size <= 2 * MAXCHUNKSIZE:
seek(0)
if size < MAXCHUNKSIZE:
self._data, self._size = read(size), size
return
self._data, self._size = FileChunk(read(size)), size
return
# Make sure we have an _p_jar, even if we are a new object, by
# doing a sub-transaction commit.
transaction.savepoint(optimistic=True)
jar = self._p_jar
if jar is None:
# Ugh
seek(0)
self._data, self._size = FileChunk(read(size)), size
return
# Now we're going to build a linked list from back
# to front to minimize the number of database updates
# and to allow us to get things out of memory as soon as
# possible.
next = None
while end > 0:
pos = end - MAXCHUNKSIZE
if pos < MAXCHUNKSIZE:
pos = 0 # we always want at least MAXCHUNKSIZE bytes
seek(pos)
data = FileChunk(read(end - pos))
# Woooop Woooop Woooop! This is a trick.
# We stuff the data directly into our jar to reduce the
# number of updates necessary.
jar.add(data)
# This is needed and has side benefit of getting
# the thing registered:
data.next = next
# Now make it get saved in a sub-transaction!
transaction.savepoint(optimistic=True)
# Now make it a ghost to free the memory. We
# don't need it anymore!
data._p_changed = None
next = data
end = pos
self._data, self._size = next, size
return
def getSize(self):
"""See `IFile`"""
return self._size
# See IFile.
data = property(_getData, _setData)
@implementer(INamedImage)
class NamedImage(NamedFile):
"""An non-BLOB image with a filename"""
filename = FieldProperty(INamedFile["filename"])
def __init__(self, data=b"", contentType="", filename=None):
self.contentType, self._width, self._height = getImageInfo(data)
self.filename = filename
self._setData(data)
# Allow override of the image sniffer
if contentType:
self.contentType = contentType
exif_data = get_exif(data)
if exif_data is not None:
log.debug(
"Image contains Exif Informations. "
"Test for Image Orientation and Rotate if necessary."
"Exif Data: %s",
exif_data,
)
orientation = exif_data["0th"].get(piexif.ImageIFD.Orientation, 1)
if 1 < orientation <= 8:
self.data, self._width, self._height, self.exif = rotate_image(
self.data
)
self.exif_data = exif_data
def _setData(self, data):
super()._setData(data)
contentType, self._width, self._height = getImageInfo(self._data)
if contentType:
self.contentType = contentType
def getImageSize(self):
"""See interface `IImage`"""
return (self._width, self._height)
data = property(NamedFile._getData, _setData)
@implementer(INamedBlobFile, HTTPRangeSupport.HTTPRangeInterface)
class NamedBlobFile(Persistent, ModifiedPropertyMixin):
"""A file stored in a ZODB BLOB, with a filename"""
filename = FieldProperty(INamedFile["filename"])
def __init__(self, data=b"", contentType="", filename=None):
if filename is not None and contentType in ("", "application/octet-stream"):
contentType = get_contenttype(filename=filename)
self.contentType = contentType
self._blob = Blob()
f = self._blob.open("w")
f.write(b"")
f.close()
self._setData(data)
self.filename = filename
self._modified = DateTime().millis()
def open(self, mode="r"):
if mode != "r" and "size" in self.__dict__:
del self.__dict__["size"]
return self._blob.open(mode)
def openDetached(self):
return open(self._blob.committed(), "rb")
def _setData(self, data):
if "size" in self.__dict__:
del self.__dict__["size"]
# Search for a storable that is able to store the data
dottedName = ".".join((data.__class__.__module__, data.__class__.__name__))
log.debug("Storage selected for data: %s", dottedName)
storable = getUtility(IStorage, name=dottedName)
storable.store(data, self._blob)
self._modified = DateTime().millis()
def _getData(self):
fp = self._blob.open("r")
data = fp.read()
fp.close()
return data
_data = property(_getData, _setData)
data = property(_getData, _setData)
@property
def size(self):
if "size" in self.__dict__:
return self.__dict__["size"]
with self._blob.open() as reader:
reader.seek(0, 2)
size = int(reader.tell())
self.__dict__["size"] = size
return size
def getSize(self):
return self.size
@implementer(INamedBlobImage)
class NamedBlobImage(NamedBlobFile):
"""An image stored in a ZODB BLOB with a filename"""
def __init__(self, data=b"", contentType="", filename=None):
super().__init__(data, contentType=contentType, filename=filename)
# Allow override of the image sniffer
if contentType:
self.contentType = contentType
exif_data = get_exif(self.data)
if exif_data is not None:
log.debug(
"Image contains Exif Informations. "
"Test for Image Orientation and Rotate if necessary."
"Exif Data: %s",
exif_data,
)
orientation = exif_data["0th"].get(piexif.ImageIFD.Orientation, 1)
if 1 < orientation <= 8:
try:
self.data, self._width, self._height, self.exif = rotate_image(
self.data
)
except KeyboardInterrupt:
raise
except Exception:
log.warning(
"Error rotating image %s based on exif data.",
filename,
exc_info=1,
)
else:
self.exif = exif_data
def _setData(self, data):
super()._setData(data)
firstbytes = self.getFirstBytes()
res = getImageInfo(firstbytes)
if res == ("image/jpeg", -1, -1) or res == ("image/tiff", -1, -1) or \
res == ("image/svg+xml", -1, -1):
# header was longer than firstbytes
start = len(firstbytes)
length = max(0, MAX_INFO_BYTES - start)
firstbytes += self.getFirstBytes(start, length)
res = getImageInfo(firstbytes)
contentType, self._width, self._height = res
if contentType:
self.contentType = contentType
data = property(NamedBlobFile._getData, _setData)
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.
"""
with self.open("r") as fp:
fp.seek(start)
firstbytes = fp.read(length)
return firstbytes
def getImageSize(self):
"""See interface `IImage`"""
if (self._width, self._height) != (-1, -1):
return (self._width, self._height)
contentType, self._width, self._height = getImageInfo(self.data)
return (self._width, self._height)