Commit 67d7bcdf authored by Bartek Górny's avatar Bartek Górny

Added caching and serving mimetypes while converting and generating files

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@8972 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 36fc5bc0
...@@ -37,13 +37,16 @@ from Products.ERP5.Document.File import File ...@@ -37,13 +37,16 @@ from Products.ERP5.Document.File import File
from Products.ERP5Type.XMLObject import XMLObject from Products.ERP5Type.XMLObject import XMLObject
from DateTime import DateTime from DateTime import DateTime
import xmlrpclib, base64 import xmlrpclib, base64
# to overwrite WebDAV methods
from Products.CMFDefault.File import File as CMFFile
enc=base64.encodestring enc=base64.encodestring
dec=base64.decodestring dec=base64.decodestring
class ConvertionError(Exception):pass class ConvertionError(Exception):pass
class OOoDocument(File, XMLObject): #class OOoDocument(File):
class OOoDocument(XMLObject,File):
""" """
A file document able to convert OOo compatible files to A file document able to convert OOo compatible files to
any OOo supported format, to capture metadata and to any OOo supported format, to capture metadata and to
...@@ -104,9 +107,16 @@ class OOoDocument(File, XMLObject): ...@@ -104,9 +107,16 @@ class OOoDocument(File, XMLObject):
cached_time={} cached_time={}
# generated files (cache) # generated files (cache)
cached_data={} cached_data={}
# mime types for cached formats XXX to be refactored
cached_mime={}
# XXX the above craves for a separate class, but I'm not sure how to handle # XXX the above craves for a separate class, but I'm not sure how to handle
# it in ZODB, so for now let it be # it in ZODB, so for now let it be
#def __init__(self,*args,**kwargs):
#XMLObject.__init__(self,*args,**kwargs)
#File.__init__(self,*args,**kwargs)
#self.__dav_collection__=0
security.declareProtected(Permissions.ModifyPortalContent,'clearCache') security.declareProtected(Permissions.ModifyPortalContent,'clearCache')
def clearCache(self): def clearCache(self):
""" """
...@@ -115,13 +125,14 @@ class OOoDocument(File, XMLObject): ...@@ -115,13 +125,14 @@ class OOoDocument(File, XMLObject):
""" """
self.cached_time={} self.cached_time={}
self.cached_data={} self.cached_data={}
self.cached_mime={}
def _getServerCoordinates(self): def _getServerCoordinates(self):
""" """
Returns OOo conversion server data from some Returns OOo conversion server data from some
preferences. NOT IMPLEMENTED YET - XXX preferences. NOT IMPLEMENTED YET - XXX
""" """
return '192.168.0.3',8080 return '127.0.0.1',8080
def _mkProxy(self): def _mkProxy(self):
sp=xmlrpclib.ServerProxy('http://%s:%d' % self._getServerCoordinates(),allow_none=True) sp=xmlrpclib.ServerProxy('http://%s:%d' % self._getServerCoordinates(),allow_none=True)
...@@ -164,7 +175,7 @@ class OOoDocument(File, XMLObject): ...@@ -164,7 +175,7 @@ class OOoDocument(File, XMLObject):
def cached_getTargetFormatItemList(mimetype): def cached_getTargetFormatItemList(mimetype):
sp=self._mkProxy() sp=self._mkProxy()
allowed=sp.getAllowedTargets(mimetype) allowed=sp.getAllowedTargets(mimetype)
return allowed return [[y,x] for x,y in allowed] # have to reverse tuple order
cached_getTargetFormatItemList = CachingMethod(cached_getTargetFormatItemList, cached_getTargetFormatItemList = CachingMethod(cached_getTargetFormatItemList,
id = "OOoDocument_getTargetFormatItemList" ) id = "OOoDocument_getTargetFormatItemList" )
...@@ -188,10 +199,10 @@ class OOoDocument(File, XMLObject): ...@@ -188,10 +199,10 @@ class OOoDocument(File, XMLObject):
""" """
if not self.hasOOfile(): return False if not self.hasOOfile(): return False
allowed=self.getTargetFormatList() allowed=self.getTargetFormatItemList()
self.log('allowed',allowed) self.log('allowed',allowed)
if allowed is None: return False if allowed is None: return False
return (format in allowed) return (format in [x[1] for x in allowed])
security.declareProtected(Permissions.ModifyPortalContent,'editMetadata') security.declareProtected(Permissions.ModifyPortalContent,'editMetadata')
def editMetadata(self,newmeta): def editMetadata(self,newmeta):
...@@ -283,6 +294,8 @@ class OOoDocument(File, XMLObject): ...@@ -283,6 +294,8 @@ class OOoDocument(File, XMLObject):
""" """
Checks whether we have an initial file Checks whether we have an initial file
""" """
print 'IS INSTANCE'
print isinstance(self,object)
_marker=[] _marker=[]
if getattr(self,'data',_marker) is not _marker: # XXX - use propertysheet accessors if getattr(self,'data',_marker) is not _marker: # XXX - use propertysheet accessors
return getattr(self,'data') is not None return getattr(self,'data') is not None
...@@ -315,6 +328,7 @@ class OOoDocument(File, XMLObject): ...@@ -315,6 +328,7 @@ class OOoDocument(File, XMLObject):
XXX - we should not create a snapshot if some error happened at conversion XXX - we should not create a snapshot if some error happened at conversion
is this checked ? is this checked ?
XXX - error at conversion raises an exception, so it should be ok
""" """
if self.hasSnapshot(): if self.hasSnapshot():
if REQUEST is not None: if REQUEST is not None:
...@@ -333,7 +347,7 @@ class OOoDocument(File, XMLObject): ...@@ -333,7 +347,7 @@ class OOoDocument(File, XMLObject):
'''getSnapshot''' '''getSnapshot'''
if not self.hasSnapshot(): if not self.hasSnapshot():
self.createSnapshot() self.createSnapshot()
return self.getSnapshot() # XXX - use propertysheet accessors return self.snapshot # XXX - use propertysheet accessors
security.declareProtected(Permissions.ManagePortal,'deleteSnapshot') security.declareProtected(Permissions.ManagePortal,'deleteSnapshot')
def deleteSnapshot(self): def deleteSnapshot(self):
...@@ -354,7 +368,7 @@ class OOoDocument(File, XMLObject): ...@@ -354,7 +368,7 @@ class OOoDocument(File, XMLObject):
return self.returnMessage('can not convert to '+format+' for some reason') return self.returnMessage('can not convert to '+format+' for some reason')
try: try:
self.makeFile(format) self.makeFile(format)
return self.cached_data[format] return self.cached_mime[format],self.cached_data[format]
except ConvertionError,e: except ConvertionError,e:
return self.returnMessage(str(e)) return self.returnMessage(str(e))
...@@ -412,7 +426,7 @@ class OOoDocument(File, XMLObject): ...@@ -412,7 +426,7 @@ class OOoDocument(File, XMLObject):
raise ConvertionError('needs conversion') raise ConvertionError('needs conversion')
if self.isFileChanged(format): if self.isFileChanged(format):
try: try:
self.cached_data[format]=self._makeFile(format) self.cached_mime[format],self.cached_data[format]=self._makeFile(format)
self._p_changed=1 # XXX not sure it is necessary self._p_changed=1 # XXX not sure it is necessary
except xmlrpclib.Fault,e: except xmlrpclib.Fault,e:
if REQUEST is not None: if REQUEST is not None:
...@@ -434,8 +448,9 @@ class OOoDocument(File, XMLObject): ...@@ -434,8 +448,9 @@ class OOoDocument(File, XMLObject):
""" """
# real version: # real version:
sp=self._mkProxy() sp=self._mkProxy()
meta,file=sp.run_generate(self.getOriginalFilename(),enc(self._unpackData(self.oo_data)),format) mime,file=sp.run_generate(self.getOriginalFilename(),enc(self._unpackData(self.oo_data)),format)
return Pdata(dec(file)) self.log('_makeFile',mime)
return mime,Pdata(dec(file))
security.declareProtected(Permissions.View,'getCacheInfo') security.declareProtected(Permissions.View,'getCacheInfo')
def getCacheInfo(self): def getCacheInfo(self):
...@@ -462,5 +477,18 @@ class OOoDocument(File, XMLObject): ...@@ -462,5 +477,18 @@ class OOoDocument(File, XMLObject):
s+='</table>' s+='</table>'
return s return s
# make sure to call the right edit methods
_edit=File._edit
edit=File.edit
# BG copied from File in case
index_html = CMFFile.index_html
PUT = CMFFile.PUT
security.declareProtected('FTP access', 'manage_FTPget', 'manage_FTPstat', 'manage_FTPlist')
manage_FTPget = CMFFile.manage_FTPget
manage_FTPlist = CMFFile.manage_FTPlist
manage_FTPstat = CMFFile.manage_FTPstat
# vim: syntax=python shiftwidth=2 # vim: syntax=python shiftwidth=2
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment