X-Git-Url: https://scm.cri.ensmp.fr/git/Plinn.git/blobdiff_plain/61413cff15e2ca1a1b698c5b82973d1713997225..cd19d4b7c2523b587f00e41b9401aec71166691d:/File.py diff --git a/File.py b/File.py index 22a1101..53a9154 100755 --- a/File.py +++ b/File.py @@ -18,8 +18,8 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # ####################################################################################### """ This module implements a portal-managed File class that's inherits of CMFDefault - File. If exists, portal_transforms is called to extract text content, and publish - attachments. + File. If exists, portal_transforms is called to extract text content, and publish + attachments. @@ -38,167 +38,160 @@ from Products.CMFCore.utils import getToolByName from hexagonit.swfheader import parse as parseswf class File(BlobFile, CMFFile) : -#class File(CMFFile) : - """ file class with portal_transforms support """ - - security = ClassSecurityInfo() - - _properties = CMFFile._properties + ({'id':'orig_name', 'type':'string', 'mode':'w', 'label':"Original Name"},) - orig_name = '' - - def __init__( self - , id - , title='' - , file='' - , content_type='' - , precondition='' - , subject=() - , description='' - , contributors=() - , effective_date=None - , expiration_date=None - , format=None - , language='en-US' - , rights='' - ): - BlobFile.__init__(self, id, title, file, content_type=content_type, precondition=precondition) - self._setId(id) - #delattr(self, '__name__') - # - # If no file format has been passed in, rely on what OFS.Image.File - # detected. Unlike Images, which have code to try and pick the content - # type out of the binary data, File objects only provide the correct - # type if a "hint" in the form of a filename extension is given. - if format is None: - format = self.content_type + """ file class with portal_transforms support """ + + security = ClassSecurityInfo() + + _properties = CMFFile._properties + ({'id':'orig_name', 'type':'string', 'mode':'w', 'label':"Original Name"},) + orig_name = '' + + def __init__( self + , id + , title='' + , file='' + , content_type='' + , precondition='' + , subject=() + , description='' + , contributors=() + , effective_date=None + , expiration_date=None + , format=None + , language='en-US' + , rights='' + ): + self._setId(id) + if format is None: + format = self.content_type or 'application/octet-stream' - DefaultDublinCoreImpl.__init__( self, title, subject, description - , contributors, effective_date, expiration_date - , format, language, rights ) - - - def __getattr__(self, name) : - try : return CMFFile.__getattr__(self, name) - except : - selfAttrs = self.__dict__ - if selfAttrs.has_key('_v_transform_cache') : - cache = selfAttrs['_v_transform_cache'] - cacheTuple = cache.get('text_html', None) # (time, value) - if cacheTuple : - cacheData = cacheTuple[1] - - subObDict = cacheData.getSubObjects() - if subObDict.has_key(name) : - fileOb = OFS.Image.File(name, name, subObDict[name]) - return fileOb - - raise AttributeError, name - - def manage_upload(self,file='',REQUEST=None): - ret = super(File, self).manage_upload(file=file, REQUEST=REQUEST) - - orig_name = OFS.Image.cookId('', '', file)[0] - if orig_name : - self.orig_name = orig_name - - if self.Format() == 'application/x-shockwave-flash' : - if file : - try : - swfmetadata = parseswf(file) - except IOError : - swfmetadata = {'width':600, 'height':600} - - for name in ('width', 'height') : - value = swfmetadata[name] - if self.hasProperty(name) : - self._updateProperty(name, value) - else : - self.manage_addProperty(name, value, 'int') - self.reindexObject() - return ret - - - - security.declareProtected(ModifyPortalContent, 'edit') - def edit(self, precondition='', file=''): - orig_name = OFS.Image.cookId('', '', file)[0] - if orig_name : - self.orig_name = orig_name - CMFFile.edit(self, precondition=precondition, file=file) - if hasattr(self, '_v_transform_cache') : - del self._v_transform_cache - - - security.declareProtected(View, 'SearchableText') - def SearchableText(self) : - """ Return full text""" - baseSearchableText = CMFFile.SearchableText(self) - transformTool = getToolByName(self, 'portal_transforms', default=None) - if transformTool is None : - return baseSearchableText - else : - f = self.bdata.open() - orig = f.read() - datastream_text = transformTool.convertTo('text/plain', - orig, - mimetype = self.content_type - ) - f.close() - full_text = '' - if datastream_text is not None : - full_text = datastream_text.getData() - - return baseSearchableText + full_text - - security.declareProtected(View, 'preview') - def preview(self) : - """Return HTML preview if it's possible or empty string """ - transformTool = getToolByName(self, 'portal_transforms', default = None) - if transformTool is None : - return '' - else : - filename = self.getId().replace(' ', '_') - f = self.bdata.open() - orig = f.read() - datastream = transformTool.convertTo('text/html', - orig, - object=self, - mimetype = self.content_type, - filename = filename) - f.close() - if datastream is not None : return datastream.getData() - else : return '' - - security.declareProtected(View, 'download') - def download(self, REQUEST, RESPONSE): - """Download this item. - - Calls OFS.Image.File.index_html to perform the actual transfer after - first setting Content-Disposition to suggest a filename. - - This method is deprecated, use the URL of this object itself. Because - the default view of a File object is to download, rather than view, - this method is obsolete. Also note that certain browsers do not deal - well with a Content-Disposition header. - - """ - - RESPONSE.setHeader('Content-Disposition', - 'attachment; filename=%s' % (self.orig_name or self.getId())) - return OFS.Image.File.index_html(self, REQUEST, RESPONSE) - - security.declarePublic('getIcon') - def getIcon(self, relative_to_portal=0): - """ return icon corresponding to mime-type - """ - regTool = getToolByName(self, 'mimetypes_registry', default=None) - if regTool : - f = self.bdata.open() - mime = regTool(f, mimetype=self.content_type)[2] - f.close() - return mime.icon_path - else : - return CMFFile.getIcon(self, relative_to_portal=relative_to_portal) + DefaultDublinCoreImpl.__init__( self, title, subject, description + , contributors, effective_date, expiration_date + , format, language, rights ) + BlobFile.__init__(self, id, title, file, content_type=content_type, precondition=precondition) + + + def __getattr__(self, name) : + try : return CMFFile.__getattr__(self, name) + except : + selfAttrs = self.__dict__ + if selfAttrs.has_key('_v_transform_cache') : + cache = selfAttrs['_v_transform_cache'] + cacheTuple = cache.get('text_html', None) # (time, value) + if cacheTuple : + cacheData = cacheTuple[1] + + subObDict = cacheData.getSubObjects() + if subObDict.has_key(name) : + fileOb = OFS.Image.File(name, name, subObDict[name]) + return fileOb + + raise AttributeError, name + + def manage_upload(self,file='',REQUEST=None): + ret = super(File, self).manage_upload(file=file, REQUEST=REQUEST) + + orig_name = OFS.Image.cookId('', '', file)[0] + if orig_name : + self.orig_name = orig_name + + if self.Format() == 'application/x-shockwave-flash' : + if file : + try : + swfmetadata = parseswf(file) + except IOError : + swfmetadata = {'width':600, 'height':600} + + for name in ('width', 'height') : + value = swfmetadata[name] + if self.hasProperty(name) : + self._updateProperty(name, value) + else : + self.manage_addProperty(name, value, 'int') + self.reindexObject() + return ret + + + + security.declareProtected(ModifyPortalContent, 'edit') + def edit(self, precondition='', file=''): + orig_name = OFS.Image.cookId('', '', file)[0] + if orig_name : + self.orig_name = orig_name + CMFFile.edit(self, precondition=precondition, file=file) + if hasattr(self, '_v_transform_cache') : + del self._v_transform_cache + + + security.declareProtected(View, 'SearchableText') + def SearchableText(self) : + """ Return full text""" + baseSearchableText = CMFFile.SearchableText(self) + transformTool = getToolByName(self, 'portal_transforms', default=None) + if transformTool is None : + return baseSearchableText + else : + f = self.bdata.open() + orig = f.read() + datastream_text = transformTool.convertTo('text/plain', + orig, + mimetype = self.content_type + ) + f.close() + full_text = '' + if datastream_text is not None : + full_text = datastream_text.getData() + + return baseSearchableText + full_text + + security.declareProtected(View, 'preview') + def preview(self) : + """Return HTML preview if it's possible or empty string """ + transformTool = getToolByName(self, 'portal_transforms', default = None) + if transformTool is None : + return '' + else : + filename = self.getId().replace(' ', '_') + f = self.bdata.open() + orig = f.read() + datastream = transformTool.convertTo('text/html', + orig, + object=self, + mimetype = self.content_type, + filename = filename) + f.close() + if datastream is not None : return datastream.getData() + else : return '' + + security.declareProtected(View, 'download') + def download(self, REQUEST, RESPONSE): + """Download this item. + + Calls OFS.Image.File.index_html to perform the actual transfer after + first setting Content-Disposition to suggest a filename. + + This method is deprecated, use the URL of this object itself. Because + the default view of a File object is to download, rather than view, + this method is obsolete. Also note that certain browsers do not deal + well with a Content-Disposition header. + + """ + + RESPONSE.setHeader('Content-Disposition', + 'attachment; filename=%s' % (self.orig_name or self.getId())) + return OFS.Image.File.index_html(self, REQUEST, RESPONSE) + + security.declarePublic('getIcon') + def getIcon(self, relative_to_portal=0): + """ return icon corresponding to mime-type + """ + regTool = getToolByName(self, 'mimetypes_registry', default=None) + if regTool : + f = self.bdata.open() + mime = regTool(f, mimetype=self.content_type)[2] + f.close() + return mime.icon_path + else : + return CMFFile.getIcon(self, relative_to_portal=relative_to_portal) InitializeClass(File) @@ -206,41 +199,41 @@ FileFactory = Factory(File) def addFile( dispatcher - , id - , title='' - , file='' - , content_type='' - , precondition='' - , subject=() - , description='' - , contributors=() - , effective_date=None - , expiration_date=None - , format='text/html' - , language='' - , rights='' - ): - """ - Add a File - """ - - # cookId sets the id and title if they are not explicity specified - id, title = OFS.Image.cookId(id, title, file) - - container = dispatcher.Destination() - - # Instantiate the object and set its description. - fobj = File( id, title=title, file='', content_type=content_type, - precondition=precondition, subject=subject, description=description, - contributors=contributors, effective_date=effective_date, - expiration_date=expiration_date, format=format, - language=language, rights=rights - ) - - # Add the File instance to self - container._setObject(id, fobj) - - # 'Upload' the file. This is done now rather than in the - # constructor because the object is now in the ZODB and - # can span ZODB objects. - container._getOb(id).manage_upload(file) \ No newline at end of file + , id + , title='' + , file='' + , content_type='' + , precondition='' + , subject=() + , description='' + , contributors=() + , effective_date=None + , expiration_date=None + , format='text/html' + , language='' + , rights='' + ): + """ + Add a File + """ + + # cookId sets the id and title if they are not explicity specified + id, title = OFS.Image.cookId(id, title, file) + + container = dispatcher.Destination() + + # Instantiate the object and set its description. + fobj = File( id, title=title, file='', content_type=content_type, + precondition=precondition, subject=subject, description=description, + contributors=contributors, effective_date=effective_date, + expiration_date=expiration_date, format=format, + language=language, rights=rights + ) + + # Add the File instance to self + container._setObject(id, fobj) + + # 'Upload' the file. This is done now rather than in the + # constructor because the object is now in the ZODB and + # can span ZODB objects. + container._getOb(id).manage_upload(file) \ No newline at end of file