Strip pour éviter que le firstChild soit un nœud text.
[Plinn.git] / File.py
diff --git a/File.py b/File.py
index 11bdafa..dfb22c4 100755 (executable)
--- a/File.py
+++ b/File.py
@@ -21,8 +21,8 @@
        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.
 
-$Id: File.py 1261 2008-01-07 01:34:23Z pin $
-$URL: http://svn.cri.ensmp.fr/svn/Plinn/branches/CMF-2.1/File.py $
+
+
 """
 
 from Globals import InitializeClass
 """
 
 from Globals import InitializeClass
@@ -30,21 +30,55 @@ from AccessControl import ClassSecurityInfo
 import OFS
 from zope.component.factory import Factory
 
 import OFS
 from zope.component.factory import Factory
 
-from Products.CMFDefault.File import File as BaseFile
+from Products.CMFDefault.File import File as CMFFile
+from Products.Photo.blobbases import File as BlobFile
+from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
 from Products.CMFCore.permissions import View, ModifyPortalContent
 from Products.CMFCore.utils import getToolByName
 from Products.CMFCore.permissions import View, ModifyPortalContent
 from Products.CMFCore.utils import getToolByName
+from hexagonit.swfheader import parse as parseswf
 
 
-class File(BaseFile) :
+class File(BlobFile, CMFFile) :
+#class File(CMFFile) :
        """ file class with portal_transforms support """
        
        security = ClassSecurityInfo()
        
        """ file class with portal_transforms support """
        
        security = ClassSecurityInfo()
        
-       _properties = BaseFile._properties + ({'id':'orig_name', 'type':'string', 'mode':'w', 'label':"Original Name"},)
+       _properties = CMFFile._properties + ({'id':'orig_name', 'type':'string', 'mode':'w', 'label':"Original Name"},)
        orig_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 
+        
+               DefaultDublinCoreImpl.__init__( self, title, subject, description
+                                                          , contributors, effective_date, expiration_date
+                                                          , format, language, rights )
+
 
        def __getattr__(self, name) :
 
        def __getattr__(self, name) :
-               try : return BaseFile.__getattr__(self, name)
+               try : return CMFFile.__getattr__(self, name)
                except :
                        selfAttrs = self.__dict__
                        if selfAttrs.has_key('_v_transform_cache') :
                except :
                        selfAttrs = self.__dict__
                        if selfAttrs.has_key('_v_transform_cache') :
@@ -53,19 +87,45 @@ class File(BaseFile) :
                                if cacheTuple :
                                        cacheData = cacheTuple[1]
                                
                                if cacheTuple :
                                        cacheData = cacheTuple[1]
                                
-                               subObDict = cacheData.getSubObjects()
-                               if subObDict.has_key(name) :
-                                       fileOb = OFS.Image.File(name, name, subObDict[name])
-                                       return fileOb
+                                       subObDict = cacheData.getSubObjects()
+                                       if subObDict.has_key(name) :
+                                               fileOb = OFS.Image.File(name, name, subObDict[name])
+                                               return fileOb
                                
                        raise AttributeError, name
        
                                
                        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
+
+               print self.absolute_url(), self.Format()
+               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
        security.declareProtected(ModifyPortalContent, 'edit')
        def edit(self, precondition='', file=''):
                orig_name = OFS.Image.cookId('', '', file)[0]
                if orig_name :
                        self.orig_name = orig_name
-               BaseFile.edit(self, precondition=precondition, file=file)
+               CMFFile.edit(self, precondition=precondition, file=file)
                if hasattr(self, '_v_transform_cache') :
                        del self._v_transform_cache
        
                if hasattr(self, '_v_transform_cache') :
                        del self._v_transform_cache
        
@@ -73,15 +133,18 @@ class File(BaseFile) :
        security.declareProtected(View, 'SearchableText')
        def SearchableText(self) :
                """ Return full text"""
        security.declareProtected(View, 'SearchableText')
        def SearchableText(self) :
                """ Return full text"""
-               baseSearchableText = BaseFile.SearchableText(self)
+               baseSearchableText = CMFFile.SearchableText(self)
                transformTool = getToolByName(self, 'portal_transforms', default=None)
                if transformTool is None :
                        return baseSearchableText
                else :
                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',
                        datastream_text =  transformTool.convertTo('text/plain',
-                                                                                                               str(self.data),
+                                                                                                               orig,
                                                                                                                mimetype = self.content_type
                                                                                                                )
                                                                                                                mimetype = self.content_type
                                                                                                                )
+                       f.close()
                        full_text = ''
                        if datastream_text is not None :
                                full_text = datastream_text.getData()
                        full_text = ''
                        if datastream_text is not None :
                                full_text = datastream_text.getData()
@@ -96,12 +159,14 @@ class File(BaseFile) :
                        return ''
                else :
                        filename = self.getId().replace(' ', '_')
                        return ''
                else :
                        filename = self.getId().replace(' ', '_')
+                       f = self.bdata.open()
+                       orig = f.read()
                        datastream = transformTool.convertTo('text/html',
                        datastream = transformTool.convertTo('text/html',
-                                                                                               str(self.data),
+                                                                                               orig,
                                                                                                object=self,
                                                                                                mimetype = self.content_type,
                                                                                                filename = filename)
                                                                                                object=self,
                                                                                                mimetype = self.content_type,
                                                                                                filename = filename)
-
+                       f.close()
                        if datastream is not None : return datastream.getData()
                        else : return ''
 
                        if datastream is not None : return datastream.getData()
                        else : return ''
 
@@ -129,10 +194,12 @@ class File(BaseFile) :
                """
                regTool = getToolByName(self, 'mimetypes_registry', default=None)
                if regTool :
                """
                regTool = getToolByName(self, 'mimetypes_registry', default=None)
                if regTool :
-                       mime = regTool(str(self.data), mimetype=self.content_type)[2]
+                       f = self.bdata.open()
+                       mime = regTool(f, mimetype=self.content_type)[2]
+                       f.close()
                        return mime.icon_path
                else :
                        return mime.icon_path
                else :
-                       return BaseFile.getIcon(self, relative_to_portal=relative_to_portal)
+                       return CMFFile.getIcon(self, relative_to_portal=relative_to_portal)
 
 
 InitializeClass(File)
 
 
 InitializeClass(File)