-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblobpatch
264 lines (238 loc) · 9.51 KB
/
blobpatch
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
Index: Image.py
===================================================================
--- Image.py (revision 121465)
+++ Image.py (working copy)
@@ -49,6 +49,9 @@
from zope.lifecycleevent import ObjectModifiedEvent
from zope.lifecycleevent import ObjectCreatedEvent
+from ZODB.interfaces import IBlobStorage
+import ZODB.blob
+
manage_addFileForm = DTMLFile('dtml/imageAdd',
globals(),
Kind='File',
@@ -251,33 +254,48 @@
if isinstance(data, str):
RESPONSE.write(data[start:end])
return True
+
+ elif isinstance(data, ZODB.blob.Blob):
+ blob_file = self.data.open('r')
+ blob_file.seek(start, 0)
+ size = end - start
+ chunksize = 1<<16
+ position = start
+ while size > 0:
+ if (end - position) < chunksize:
+ chunksize = (end - position)
+ RESPONSE.write(blob_file.read(chunksize))
+ position += chunksize
+ size -= chunksize
+ return True
+
+ else: #handle pdata case
+ # Linked Pdata objects. Urgh.
+ pos = 0
+ while data is not None:
+ l = len(data.data)
+ pos = pos + l
+ if pos > start:
+ # We are within the range
+ lstart = l - (pos - start)
- # Linked Pdata objects. Urgh.
- pos = 0
- while data is not None:
- l = len(data.data)
- pos = pos + l
- if pos > start:
- # We are within the range
- lstart = l - (pos - start)
+ if lstart < 0: lstart = 0
- if lstart < 0: lstart = 0
+ # find the endpoint
+ if end <= pos:
+ lend = l - (pos - end)
- # find the endpoint
- if end <= pos:
- lend = l - (pos - end)
+ # Send and end transmission
+ RESPONSE.write(data[lstart:lend])
+ break
- # Send and end transmission
- RESPONSE.write(data[lstart:lend])
- break
+ # Not yet at the end, transmit what we have.
+ RESPONSE.write(data[lstart:])
- # Not yet at the end, transmit what we have.
- RESPONSE.write(data[lstart:])
+ data = data.next
- data = data.next
+ return True
- return True
-
else:
boundary = choose_boundary()
@@ -322,6 +340,20 @@
if isinstance(data, str):
RESPONSE.write(data[start:end])
+ elif isinstance(data, ZODB.blob.Blob):
+ blob_file = self.data.open('r')
+ blob_file.seek(start, 0)
+ size = end - start
+ chunksize = 1<<16
+ position = start
+ while size > 0:
+ if (end - position) < chunksize:
+ chunksize = (end - position)
+ RESPONSE.write(blob_file.read(chunksize))
+ position += chunksize
+ size -= chunksize
+ return True
+
else:
# Yippee. Linked Pdata objects. The following
# calculations allow us to fast-forward through the
@@ -364,7 +396,7 @@
# Do not keep the link references around.
del pdata_map
-
+
RESPONSE.write('\r\n--%s--\r\n' % boundary)
return True
@@ -386,7 +418,7 @@
# unfortunately.
self.ZCacheable_set(None)
return ''
-
+
if self.precondition and hasattr(self, str(self.precondition)):
# Grab whatever precondition was defined and then
# execute it. The precondition will raise an exception
@@ -396,11 +428,11 @@
c(REQUEST['PARENTS'][1],REQUEST)
else:
c()
-
+
if self._range_request_handler(REQUEST, RESPONSE):
# we served a chunk of content in response to a range request.
return ''
-
+
RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
RESPONSE.setHeader('Content-Type', self.content_type)
RESPONSE.setHeader('Content-Length', self.size)
@@ -414,18 +446,23 @@
# something implementing the IStreamIterator interface
# from a "FileCacheManager"
return result
-
+
self.ZCacheable_set(None)
-
- data=self.data
+ data = self.data
if isinstance(data, str):
RESPONSE.setBase(None)
return data
-
- while data is not None:
- RESPONSE.write(data.data)
- data=data.next
-
+
+ elif isinstance(data, ZODB.blob.Blob):
+ RESPONSE.setBase(None)
+ filename = data._p_blob_uncommitted or data.committed()
+ return filestream_iterator(filename, 'rb')
+
+ else:
+ while data is not None:
+ RESPONSE.write(data.data)
+ data=data.next
+
return ''
security.declareProtected(View, 'view_image_or_file')
@@ -440,7 +477,7 @@
""" Allow file objects to be searched.
"""
if self.content_type.startswith('text/'):
- return str(self.data)
+ return self.data.open('r').read() if isinstance(self.data, ZODB.blob.Blob) else str(self.data)
return ''
security.declarePrivate('update_data')
@@ -452,11 +489,23 @@
if content_type is not None: self.content_type=content_type
if size is None: size=len(data)
self.size=size
- self.data=data
+ if data:
+ if IBlobStorage.providedBy(self._p_jar.db().storage):
+ self.data = ZODB.blob.Blob(data)
+ else:
+ self.data = data
+ else:
+ self.data = ''
self.ZCacheable_invalidate()
self.ZCacheable_set(None)
self.http__refreshEtag()
+ def resave_to_blob(self):
+ "put the current data in a blob"
+ data = self.data
+ if data and not isinstance(data, ZODB.blob.Blob) and IBlobStorage.providedBy(self._p_jar.db().storage):
+ self.data = ZODB.blob.Blob(str(data))
+
security.declareProtected(change_images_and_files, 'manage_edit')
def manage_edit(self, title, content_type, precondition='',
filedata=None, REQUEST=None):
@@ -517,9 +566,15 @@
n=1 << 16
+ # Make sure we have an _p_jar, even if we are a new object, by
+ # doing a sub-transaction commit.
+ transaction.savepoint(optimistic=True)
+
if isinstance(file, str):
size=len(file)
- if size < n: return file, size
+ if size<n or IBlobStorage.providedBy(self._p_jar.db().storage):
+ #for blobs we don't have to cut anything up or if the size<n
+ return file,size
# Big string: cut it into smaller chunks
file = StringIO(file)
@@ -536,15 +591,17 @@
seek(0,2)
size=end=file.tell()
+ if IBlobStorage.providedBy(self._p_jar.db().storage):
+ seek(0)
+ return read(size), size
+
if size <= 2*n:
seek(0)
if size < n: return read(size), size
return Pdata(read(size)), size
- # Make sure we have an _p_jar, even if we are a new object, by
- # doing a sub-transaction commit.
- transaction.savepoint(optimistic=True)
+
if self._p_jar is None:
# Ugh
seek(0)
@@ -621,7 +678,7 @@
return self.content_type
- def __str__(self): return str(self.data)
+ def __str__(self): return self.data.open('r').read() if isinstance(self.data, ZODB.blob.Blob) else str(self.data)
def __len__(self): return 1
security.declareProtected(ftp_access, 'manage_FTPstat')
@@ -814,7 +871,13 @@
if size is None: size=len(data)
self.size=size
- self.data=data
+ if data:
+ if IBlobStorage.providedBy(self._p_jar.db().storage):
+ self.data = ZODB.blob.Blob(data)
+ else:
+ self.data = data
+ else:
+ self.data = ''
ct, width, height = getImageInfo(data)
if ct: