-
Notifications
You must be signed in to change notification settings - Fork 0
/
_blobfields.py
1011 lines (866 loc) · 39.9 KB
/
_blobfields.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
################################################################################
# _blobfields.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################
# Imports.
from DateTime.DateTime import DateTime
from ZPublisher import HTTPRangeSupport,HTTPRequest
from OFS.CopySupport import absattr
from OFS.Image import Image, File
from cStringIO import StringIO
from mimetools import choose_boundary
import base64
import copy
import re
import time
import urllib
import warnings
import zExceptions
# Product Imports.
import _fileutil
import _globals
import pilutil
import standard
import zopeutil
__all__= ['MyBlob','MyImage','MyFile']
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
_blobfields.StringType:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
StringType=type('')
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
_blobfields.rfc1123_date:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def rfc1123_date(dt):
from wsgiref.handlers import format_date_time
stamp = time.mktime(time.gmtime(dt))
return format_date_time(stamp)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
_blobfields.recurse_downloadRessources:
Download from ZODB to file-system during Export.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def recurse_downloadRessources(self, base_path, REQUEST):
ressources = []
# Check Constraints.
root = getattr( self, '__root__', None)
if root is not None:
return ressources
# Attributes.
langs = self.getLangIds()
prim_lang = self.getPrimaryLanguage()
obj_attrs = self.getObjAttrs()
for key in obj_attrs.keys():
obj_attr = self.getObjAttr(key)
datatype = obj_attr['datatype_key']
if datatype in _globals.DT_BLOBS:
for lang in langs:
try:
if obj_attr['multilang'] or lang==prim_lang or (obj_attr['multilang']==0 and lang!=prim_lang):
req = {'lang':lang,'preview':'preview'}
obj_vers = self.getObjVersion(req)
blob = self._getObjAttrValue(obj_attr,obj_vers,lang)
if blob is not None:
filename = blob.getFilename()
filename = getLangFilename(self,filename,lang)
filename = '%s%s'%(base_path,filename)
filename = _fileutil.getOSPath(filename)
_fileutil.exportObj(blob,filename)
ressources.append( { 'filepath':filename, 'content_type':blob.getContentType()})
except:
standard.writeError(self,"[recurse_downloadRessources]: Can't export %s"%key)
elif datatype == _globals.DT_LIST:
for lang in langs:
try:
if obj_attr['multilang'] or lang==prim_lang or (obj_attr['multilang']==0 and lang!=prim_lang):
req = {'lang':lang,'preview':'preview'}
obj_vers = self.getObjVersion(req)
v = self._getObjAttrValue(obj_attr,obj_vers,lang)
i = 0
for r in v:
uu = []
if type(r) is dict:
for k in r.keys():
u = r[k]
if isinstance(u,MyImage) or isinstance(u,MyFile):
uu.append( u)
elif isinstance(r,MyImage) or isinstance(r,MyFile):
uu.append( r)
for u in uu:
filename = u.getFilename()
filename = getLangFilename(self,filename,lang)
filename = '%s@%i/%s'%(base_path,i,filename)
filename = _fileutil.getOSPath(filename)
_fileutil.exportObj(u,filename)
ressources.append( { 'filepath':filename, 'content_type':u.getContentType()})
i = i + 1
except:
standard.writeError(self,"[recurse_downloadRessources]: Can't export %s"%key)
# Process children.
for child in self.getChildNodes():
ressources.extend( recurse_downloadRessources( child, base_path+child.id+'/', REQUEST))
# Return list of ressources.
return ressources
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
_blobfields.createBlobField:
Create blob-field of desired object-type and initialize it with given file.
IN: clazz [C{MyImage}|C{MyFile}]
file [ZPublisher.HTTPRequest.FileUpload|dictionary]
OUT: blob [MyImage|MyFile]
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def createBlobField(self, objtype, file=''):
if _globals.is_str_type( file):
blob = uploadBlobField( self, objtype, file)
elif type( file) is dict:
data = file.get( 'data', '')
if type( data) is StringType:
data = StringIO( data)
blob = uploadBlobField( self, objtype, data, file.get('filename',''))
if file.get('content_type'):
blob.content_type = file.get('content_type')
else:
blob = uploadBlobField( self, objtype, file, file.filename)
return blob
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
_blobfields.uploadBlobField
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def uploadBlobField(self, clazz, file='', filename=''):
# bytes
try:
file = file.read()
except:
pass
# dataURI
f = None
if type(file) is str:
f = re.findall('^data:(.*?);base64,([\s\S]*)$',file)
if f:
mt = f[0][0]
file = base64.b64decode(f[0][1])
else:
mt, enc = standard.guess_content_type(filename,file)
# class
if clazz in [_globals.DT_IMAGE,'image'] or mt.startswith('image'):
clazz = MyImage
elif clazz in [_globals.DT_FILE,'file']:
clazz = MyFile
# init
blob = clazz( id='',title='',file='')
blob.update_data(file,content_type=mt,size=len(file))
blob.aq_parent = self
blob.mediadbfile = None
blob.filename = _fileutil.extractFilename( filename, undoable=True).encode('utf-8')
# Check size.
if self is not None:
maxlength_prop = 'ZMS.input.%s.maxlength'%['file','image'][isinstance(blob,MyImage)]
maxlength = self.getConfProperty(maxlength_prop,'')
if len(maxlength) > 0:
size = blob.get_size()
if int(size) > int(maxlength):
raise zExceptions.Forbidden('file upload size=%i > %s=%i' %(size,maxlength_prop,int(maxlength)))
return blob
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
_blobfields.getLangFilename:
Returns filename concatenated with language suffix.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def getLangFilename(self, filename, lang):
i = filename.rfind('.')
name = filename[:i]
name = name.replace(' ','_')
ext = filename[i+1:]
if len(self.getLangIds()) > 1 and lang is not None:
suffix = '_' + lang
if len(name) < len(suffix) or name[-len(suffix):] != suffix:
name += suffix
name += '.' + ext
return name
################################################################################
###
### THUMBNAILS
###
################################################################################
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
_blobfields.thumbnailImageFields:
Process image-fields and shrink superres to hires and hires to lores.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def thumbnailImageFields(self, lang, REQUEST):
message = ''
if pilutil.enabled():
obj_attrs = self.getObjAttrs()
for key in obj_attrs.keys():
obj_attr = self.getObjAttr(key)
datatype = obj_attr['datatype_key']
if datatype == _globals.DT_IMAGE:
message += thumbnailImage(self,'%ssuperres'%key,'%shires'%key,self.getConfProperty('InstalledProducts.pil.hires.thumbnail.max'),lang,REQUEST)
message += thumbnailImage(self,'%shires'%key,'%s'%key,self.getConfProperty('InstalledProducts.pil.thumbnail.max'),lang,REQUEST)
return message
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
_blobfields.thumbnailImage:
Process image-field and shrink attribute given by hiresKey to attribute given
by loresKey.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def thumbnailImage(self, hiresKey, loresKey, maxdim, lang, REQUEST):
message = ''
try:
if hiresKey in self.getObjAttrs().keys() and REQUEST.get('generate_preview_%s_%s'%(hiresKey,lang),0) == 1:
pilutil.generate_preview(self, hiresKey, loresKey, maxdim)
except:
standard.writeError( self, '[thumbnailImage]')
return message
################################################################################
################################################################################
###
### Class
###
################################################################################
################################################################################
class MyBlob:
# Documentation string.
__doc__ = """ZMS product module."""
# Version string.
__version__ = '0.1'
__class_name__ = '{{MyBlob}}'
def __str__(self):
return self.__class_name__
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# <BO> Copied from OFS/Image.py:
# Modifications: self._p_mtime -> self.aq_parent._p_mtime
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def _if_modified_since_request_handler(self, REQUEST, RESPONSE):
# HTTP If-Modified-Since header handling: return True if
# we can handle this request by returning a 304 response
header=REQUEST.get_header('If-Modified-Since', None)
if header is not None:
header=header.split( ';')[0]
# Some proxies seem to send invalid date strings for this
# header. If the date string is not valid, we ignore it
# rather than raise an error to be generally consistent
# with common servers such as Apache (which can usually
# understand the screwy date string as a lucky side effect
# of the way they parse it).
# This happens to be what RFC2616 tells us to do in the face of an
# invalid date.
try: mod_since=long(DateTime(header).timeTime())
except: mod_since=None
if mod_since is not None:
if self.aq_parent._p_mtime:
last_mod = long(self.aq_parent._p_mtime)
else:
last_mod = long(0)
if last_mod > 0 and last_mod <= mod_since:
RESPONSE.setHeader('Last-Modified',
rfc1123_date(self.aq_parent._p_mtime))
RESPONSE.setHeader('Content-Type', self.content_type)
RESPONSE.setHeader('Accept-Ranges', 'bytes')
RESPONSE.setStatus(304)
return True
def _range_request_handler(self, REQUEST, RESPONSE):
# HTTP Range header handling: return True if we've served a range
# chunk out of our data.
range = REQUEST.get_header('Range', None)
request_range = REQUEST.get_header('Request-Range', None)
if request_range is not None:
# Netscape 2 through 4 and MSIE 3 implement a draft version
# Later on, we need to serve a different mime-type as well.
range = request_range
if_range = REQUEST.get_header('If-Range', None)
if range is not None:
ranges = HTTPRangeSupport.parseRange(range)
if if_range is not None:
# Only send ranges if the data isn't modified, otherwise send
# the whole object. Support both ETags and Last-Modified dates!
if len(if_range) > 1 and if_range[:2] == 'ts':
# ETag:
if if_range != self.http__etag():
# Modified, so send a normal response. We delete
# the ranges, which causes us to skip to the 200
# response.
ranges = None
else:
# Date
date = if_range.split( ';')[0]
try: mod_since=long(DateTime(date).timeTime())
except: mod_since=None
if mod_since is not None:
if self.aq_parent._p_mtime:
last_mod = long(self.aq_parent._p_mtime)
else:
last_mod = long(0)
if last_mod > mod_since:
# Modified, so send a normal response. We delete
# the ranges, which causes us to skip to the 200
# response.
ranges = None
if ranges:
# Search for satisfiable ranges.
satisfiable = 0
for start, end in ranges:
if start < self.size:
satisfiable = 1
break
if not satisfiable:
RESPONSE.setHeader('Content-Range',
'bytes */%d' % self.size)
RESPONSE.setHeader('Accept-Ranges', 'bytes')
RESPONSE.setHeader('Last-Modified',
rfc1123_date(self.aq_parent._p_mtime))
RESPONSE.setHeader('Content-Type', self.content_type)
RESPONSE.setHeader('Content-Length', self.size)
RESPONSE.setStatus(416)
return True
ranges = HTTPRangeSupport.expandRanges(ranges, self.size)
if len(ranges) == 1:
# Easy case, set extra header and return partial set.
start, end = ranges[0]
size = end - start
RESPONSE.setHeader('Last-Modified',
rfc1123_date(self.aq_parent._p_mtime))
RESPONSE.setHeader('Content-Type', self.content_type)
RESPONSE.setHeader('Content-Length', size)
RESPONSE.setHeader('Accept-Ranges', 'bytes')
RESPONSE.setHeader('Content-Range',
'bytes %d-%d/%d' % (start, end - 1, self.size))
RESPONSE.setStatus(206) # Partial content
data = self.data
if type(data) is StringType:
RESPONSE.write(data[start:end])
return True
# 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
# find the endpoint
if end <= pos:
lend = l - (pos - end)
# Send and end transmission
RESPONSE.write(data[lstart:lend])
break
# Not yet at the end, transmit what we have.
RESPONSE.write(data[lstart:])
data = data.next
return True
else:
boundary = choose_boundary()
# Calculate the content length
size = (8 + len(boundary) + # End marker length
len(ranges) * ( # Constant lenght per set
49 + len(boundary) + len(self.content_type) +
len('%d' % self.size)))
for start, end in ranges:
# Variable length per set
size = (size + len('%d%d' % (start, end - 1)) +
end - start)
# Some clients implement an earlier draft of the spec, they
# will only accept x-byteranges.
draftprefix = (request_range is not None) and 'x-' or ''
RESPONSE.setHeader('Content-Length', size)
RESPONSE.setHeader('Accept-Ranges', 'bytes')
RESPONSE.setHeader('Last-Modified',
rfc1123_date(self.aq_parent._p_mtime))
RESPONSE.setHeader('Content-Type',
'multipart/%sbyteranges; boundary=%s' % (
draftprefix, boundary))
RESPONSE.setStatus(206) # Partial content
data = self.data
# The Pdata map allows us to jump into the Pdata chain
# arbitrarily during out-of-order range searching.
pdata_map = {}
pdata_map[0] = data
for start, end in ranges:
RESPONSE.write('\r\n--%s\r\n' % boundary)
RESPONSE.write('Content-Type: %s\r\n' %
self.content_type)
RESPONSE.write(
'Content-Range: bytes %d-%d/%d\r\n\r\n' % (
start, end - 1, self.size))
if type(data) is StringType:
RESPONSE.write(data[start:end])
else:
# Yippee. Linked Pdata objects. The following
# calculations allow us to fast-forward through the
# Pdata chain without a lot of dereferencing if we
# did the work already.
first_size = len(pdata_map[0].data)
if start < first_size:
closest_pos = 0
else:
closest_pos = (
((start - first_size) >> 16 << 16) +
first_size)
pos = min(closest_pos, max(pdata_map.keys()))
data = pdata_map[pos]
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
# find the endpoint
if end <= pos:
lend = l - (pos - end)
# Send and loop to next range
RESPONSE.write(data[lstart:lend])
break
# Not yet at the end, transmit what we have.
RESPONSE.write(data[lstart:])
data = data.next
# Store a reference to a Pdata chain link so we
# don't have to deref during this request again.
pdata_map[pos] = data
# Do not keep the link references around.
del pdata_map
RESPONSE.write('\r\n--%s--\r\n' % boundary)
return True
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# <EO> Copied from OFS/Image.py
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.equals
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def equals(self, ob):
"""
Indicates whether some other MyBlob-object is "equal to" this one.
"""
b = ob is not None
try:
if b:
attrs = self.__obj_attrs__
for attr in attrs:
if b and attr not in ['data', 'aq_parent']:
b = getattr( self, attr) == getattr( ob, attr)
except:
b = False
return b
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob._createCopy:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def _createCopy(self, aq_parent, key):
value = self._getCopy()
value.is_blob = True
value.aq_parent = aq_parent
value.key = key
value.lang = aq_parent.REQUEST.get( 'lang')
return value
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.__call__:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def __bobo_traverse__(self, TraversalRequest, name):
return self
__call____roles__ = None
def __call__(self, REQUEST=None, **kw):
""""""
if REQUEST is not None and REQUEST.has_key('path_to_handle'):
REQUEST['path_to_handle']=[]
RESPONSE = REQUEST.RESPONSE
parent = self.aq_parent
access = parent.hasAccess( REQUEST) or parent.getConfProperty( 'ZMS.blobfields.grant_public_access', 0) == 1
# Hook for custom access rules: return True/False, return 404 (Forbidden) if you want to perform redirect
if access:
# @deprecated
# @TODO log deprecation warning
try:
name = 'hasCustomAccess'
if hasattr(parent,name):
v = zopeutil.callObject(getattr(parent,name),parent)
if type( v) is bool:
access = access and v
elif type( v) is int and v == 404:
return ''
except:
standard.writeError(parent,'[__call__]: can\'t %s'%name)
# Raise unauthorized error.
else:
RESPONSE.setHeader('Expires', '-1')
RESPONSE.setHeader('Cache-Control', 'no-cache')
RESPONSE.setHeader('Pragma', 'no-cache')
raise zExceptions.Unauthorized
# Set custom response-headers:
# - explicit Last-Modified
# - explicit Content-Disposition and Content-Type
# - addtional Content-Disposition via ZMS_ADDITIONAL_CONTENT_DISPOSITION
try:
name = 'getCustomBlobResponseHeaders'
if hasattr(parent,name):
zopeutil.callObject(getattr(parent,name),parent)
except:
standard.writeError(parent,'[__call__]: can\'t %s'%name)
if not RESPONSE.getHeader('Last-Modified') and self._if_modified_since_request_handler(REQUEST, RESPONSE):
# we were able to handle this by returning a 304 (not modified)
# unfortunately, because the HTTP cache manager uses the cache
# API, and because 304 (not modified) responses are required to carry the Expires
# header for HTTP/1.1, we need to call ZCacheable_set here.
# This is nonsensical for caches other than the HTTP cache manager
# unfortunately.
self.ZCacheable_set(None)
return ''
if isinstance(self,MyImage) and self._range_request_handler(REQUEST, RESPONSE):
# we served a chunk of content in response to a range request.
return ''
# If blob-object came from call of py-attribute by path-handler check content-disposition and content-type.
if not (RESPONSE.getHeader('Content-Disposition') and RESPONSE.getHeader('Content-Type')):
standard.set_response_headers(self.getFilename(),self.getContentType(),self.get_size(),REQUEST)
if not (RESPONSE.getHeader('Last-Modified')):
RESPONSE.setHeader('Last-Modified', rfc1123_date(parent._p_mtime))
# Cacheable.
cacheable = not REQUEST.get('preview') == 'preview'
if cacheable:
cacheable = parent.hasPublicAccess()
if not cacheable:
RESPONSE.setHeader('Expires', '-1')
RESPONSE.setHeader('Cache-Control', 'no-cache')
# IE6 SSL Download bug:
# http://support.microsoft.com/kb/812935/en-us
# http://support.microsoft.com/kb/323308/en-us
if not REQUEST.get('URL','').startswith( 'https://'):
RESPONSE.setHeader('Pragma', 'no-cache')
if self.ZCacheable_isCachingEnabled():
result = self.ZCacheable_get(default=None)
if result is not None:
# We will always get None from RAMCacheManager and HTTP
# Accelerated Cache Manager but we will get
# something implementing the IStreamIterator interface
# from a "FileCacheManager"
return result
self.ZCacheable_set(None)
mediadb = parent.getMediaDb()
if mediadb is not None:
mediadbfile = self.getMediadbfile()
if mediadbfile is not None:
return mediadb.retrieveFileStreamIterator( mediadbfile, REQUEST)
RESPONSE.setBase(None)
return self.getData()
return self
index_html=None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getObjAttrs:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getObjAttrs__roles__ = None
def getObjAttrs(self, meta_type=None):
return {}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getDataURI
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getDataURI__roles__ = None
def getDataURI(self):
dataURI = 'data:%s;base64,%s'%(self.getContentType(),base64.encodestring(str(self.getData())))
return dataURI
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getData:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getData__roles__ = None
def getData(self, parent=None):
"""
Returns data.
"""
data = ''
mediadbfile = self.getMediadbfile()
if mediadbfile is not None:
if parent is None:
parent = self.aq_parent
mediadb = parent.getMediaDb()
if mediadb is not None:
try:
data = mediadb.retrieveFile( mediadbfile)
except:
standard.writeError( parent, "[getData]: can't retrieve file from mediadb: %s"%str(mediadbfile))
else:
data = str(getattr(self,'data',''))
return data
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getHref:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getHref__roles__ = None
def getHref(self, REQUEST):
"""
Returns absolute url.
@var REQUEST: the triggering request
@type REQUEST: ZPublisher.HTTPRequest
"""
parent = self.aq_parent
key = self.key
rownum = ''
i = key.find( ':')
if i > 0:
rownum = '/@%s'%key[ i+1:]
filename = getLangFilename( parent, self.getFilename(), self.lang)
filename = standard.url_encode( filename)
qs = ''
zms_version_key = 'ZMS_VERSION_%s'%parent.id
if REQUEST.get( zms_version_key, None) is not None:
qs = standard.qs_append( qs, zms_version_key,REQUEST.get( zms_version_key))
elif standard.isPreviewRequest( REQUEST):
qs = standard.qs_append( qs, 'preview', 'preview')
base = '/'.join(parent.getPhysicalPath())
href = base+rownum+'/'+filename+qs
return href
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.on_setobjattr:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def on_setobjattr(self):
# store data in media-db.
parent = self.aq_parent
if parent is not None:
mediadb = parent.getMediaDb()
if mediadb is not None and getattr(self,'mediadbfile') is None:
self.mediadbfile = mediadb.storeFile( self)
self.data = ''
# unset parent to avoid TypeError: Can't pickle objects in acquisition wrappers.
if parent.getType() != 'ZMSRecordSet':
self.aq_parent = None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getMediadbfile:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getMediadbfile__roles__ = None
def getMediadbfile(self):
"""
Returns mediadb-filename.
@rtype: C{string}
"""
return getattr(self,'mediadbfile',None)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getFilename:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getFilename__roles__ = None
def getFilename(self):
"""
Returns filename.
@rtype: C{string}
"""
filename = self.filename
while filename.startswith( '_'):
filename = filename[1:]
for ch in [ '+', '%', ' ', '!', '?', '#', '"', '(', ')','&' ]:
filename = filename.replace(ch,'')
if filename != self.filename and len( self.data) > 0:
self.filename = filename
return filename
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.get_size:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
get_size__roles__ = None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.get_real_size:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
get_real_size__roles__ = None
def get_real_size(self):
"""
Returns real size in ZODB.
@rtype: C{int}
"""
if self.mediadbfile is None:
return self.get_size()
else:
return len(self.mediadbfile)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getDataSizeStr:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getDataSizeStr__roles__ = None
def getDataSizeStr(self):
"""
Returns display string for file-size (KB).
@rtype: C{string}
@deprecated: Use standard.getDataSizeStr(len) instead!
"""
warnings.warn('Using MyBlob.getDataSizeStr() is deprecated.'
' Use standard.getDataSizeStr(len) instead.',
DeprecationWarning,
stacklevel=2)
return _fileutil.getDataSizeStr(self.get_size())
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getContentType:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getContentType__roles__ = None
def getContentType(self):
"""
Returns MIME-type (e.g. image/gif, text/xml).
@rtype: C{string}
"""
return self.content_type
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlob.getMimeTypeIconSrc:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getMimeTypeIconSrc__roles__ = None
def getMimeTypeIconSrc(self):
"""
Returns the absolute-url of an icon representing the MIME-type of this MyBlob-object.
@rtype: C{string}
@deprecated: Use zmscontext.getMimeTypeIconSrc(mt) instead!
"""
import _mimetypes
warnings.warn('Using MyBlob.getMimeTypeIconSrc() is deprecated.'
' Use zmscontext.getMimeTypeIconSrc(mt) instead.',
DeprecationWarning,
stacklevel=2)
return '/++resource++zms_/img/' + _mimetypes.dctMimeType.get( self.getContentType(), _mimetypes.content_unknown)
################################################################################
################################################################################
class MyImage(MyBlob,Image):
# Documentation string.
__doc__ = """ZMS product module."""
# Version string.
__version__ = '0.1'
__obj_attrs__ = ['content_type','size','data','filename','mediadbfile','width','height','aq_parent']
__xml_attrs__ = ['content_type','width','height']
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyImage._getCopy:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def _getCopy(self):
self.getFilename() # Normalize filename
ob = self
clone = MyImage(id='',title='',file='')
attrs = self.__obj_attrs__
for attr in attrs:
if hasattr(ob,attr):
setattr(clone,attr,getattr(ob,attr))
return clone
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyImage.toXml:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def toXml(self, sender=None, base_path='', data2hex=True):
data = ''
objtype = ''
filename = _fileutil.getOSPath(_fileutil.extractFilename(getattr(self,'filename','')))
if data2hex:
if getattr(self,'content_type','').find('text/') == 0:
data = '<![CDATA[%s]]>'%str(self.getData( sender))
else:
data = standard.bin2hex(self.getData( sender))
objtype = ' type="image"'
else:
filename = self.getFilename()
filename = getLangFilename(sender,filename,self.lang)
filename = '%s%s'%(base_path,filename)
xml = '\n<data'
xml += ' width="%s"'%str(getattr(self,'width',''))
xml += ' height="%s"'%str(getattr(self,'height',''))
xml += ' content_type="%s"'%str(getattr(self,'content_type',''))
xml += ' filename="%s"'%filename
xml += objtype + '>' + data
xml += '</data>'
return xml
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyImage.getWidth:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getWidth__roles__ = None
def getWidth(self):
w = self.width
if not w:
w = self.aq_parent.getConfProperty('ZMS.image.default.width',640)
return w
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyImage.getHeight:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getHeight__roles__ = None
def getHeight(self):
h = self.height
if not h:
h = self.aq_parent.getConfProperty('ZMS.image.default.height',400)
return h
################################################################################
################################################################################
class MyFile(MyBlob,File):
# Documentation string.
__doc__ = """ZMS product module."""
# Version string.
__version__ = '0.1'
__obj_attrs__ = ['content_type','size','data','filename','mediadbfile','aq_parent']
__xml_attrs__ = ['content_type']
__class_name__ = '{{MyFile}}'
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyFile._getCopy:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def _getCopy(self):
self.getFilename() # Normalize filename
ob = self
clone = MyFile(id='',title='',file='')
attrs = self.__obj_attrs__
for attr in attrs:
if hasattr(ob,attr):
setattr(clone,attr,getattr(ob,attr))
return clone
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyFile.toXml:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def toXml(self, sender=None, base_path='', data2hex=True):
data = ''
objtype = ''
filename = _fileutil.getOSPath(_fileutil.extractFilename(getattr(self,'filename','')))
if data2hex:
if getattr(self,'content_type','').find('text/') == 0:
data = '<![CDATA[%s]]>'%str(self.getData( sender))
else:
data = standard.bin2hex(self.getData( sender))
objtype = ' type="file"'
else:
filename = self.getFilename()
filename = getLangFilename(sender,filename,self.lang)
filename = '%s%s'%(base_path,filename)
xml = '\n<data'
xml += ' content_type="%s"'%str(getattr(self,'content_type',''))
xml += ' filename="%s"'%filename
xml += objtype + '>' + data
xml += '</data>'
return xml
################################################################################
################################################################################
class MyBlobWrapper:
# Documentation string.
__doc__ = """ZMS product module."""
# Version string.
__version__ = '0.1'
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlobWrapper.__init__:
Constructor
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def __init__(self, f):
self.f = f
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlobWrapper.getHref:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getHref__roles__ = None
def getHref(self, REQUEST):
return self.f.absolute_url()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlobWrapper.getFilename:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getFilename__roles__ = None
def getFilename(self):
return absattr( self.f.id)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlobWrapper.getDataURI:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getDataURI__roles__ = None
def getDataURI(self):
dataURI = 'data:%s;base64,%s'%(self.getContentType(),base64.encodestring(str(self.getData())))
return dataURI
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
MyBlobWrapper.getData:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
getData__roles__ = None