Commit e9a0beac authored by Romain Courteaud's avatar Romain Courteaud

By JPS.

Fix caching issue with converted image.
Restore _setFile virtual accessor.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@12139 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent abd2d6b3
......@@ -30,6 +30,8 @@ from DateTime import DateTime
from operator import add
from AccessControl import ClassSecurityInfo, getSecurityManager
from Acquisition import aq_base
from Globals import PersistentMapping
from Products.CMFCore.utils import getToolByName
from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.XMLObject import XMLObject
......@@ -58,11 +60,11 @@ class ConversionCacheMixin:
* Implement ZODB BLOB
"""
# time of generation of various formats
_cached_time = {}
_cached_time = None # Defensive programming - prevent caching to RAM
# generated files (cache)
_cached_data = {}
_cached_data = None # Defensive programming - prevent caching to RAM
# mime types for cached formats XXX to be refactored
_cached_mime = {}
_cached_mime = None # Defensive programming - prevent caching to RAM
# Declarative security
security = ClassSecurityInfo()
......@@ -74,15 +76,26 @@ class ConversionCacheMixin:
Clear cache (invoked by interaction workflow upon file upload
needed here to overwrite class attribute with instance attrs
"""
self._cached_time = {}
self._cached_data = {}
self._cached_mime = {}
self._cached_time = PersistentMapping()
self._cached_data = PersistentMapping()
self._cached_mime = PersistentMapping()
security.declareProtected(Permissions.View, 'updateConversionCache')
def updateConversionCache(self):
aself = aq_base(self)
if not hasattr(aself, '_cached_time'):
self._cached_time = PersistentMapping()
if not hasattr(aself, '_cached_data'):
self._cached_data = PersistentMapping()
if not hasattr(aself, '_cached_mime'):
self._cached_mime = PersistentMapping()
security.declareProtected(Permissions.View, 'hasConversion')
def hasConversion(self, **format):
"""
Checks whether we have a version in this format
"""
self.updateConversionCache()
return self._cached_data.has_key(makeSortedTuple(format))
security.declareProtected(Permissions.View, 'getCacheTime')
......@@ -90,10 +103,12 @@ class ConversionCacheMixin:
"""
Checks when if ever was the file produced
"""
self.updateConversionCache()
return self._cached_time.get(makeSortedTuple(format), 0)
security.declareProtected(Permissions.ModifyPortalContent, 'updateConversion')
def updateConversion(self, **format):
self.updateConversionCache()
self._cached_time[makeSortedTuple(format)] = DateTime()
security.declareProtected(Permissions.ModifyPortalContent, 'setConversion')
......@@ -102,6 +117,7 @@ class ConversionCacheMixin:
Saves a version of the document in a given format; records mime type
and conversion time (which is right now).
"""
self.updateConversionCache()
tformat = makeSortedTuple(format)
if mime is not None:
self._cached_mime[tformat] = mime
......@@ -121,6 +137,7 @@ class ConversionCacheMixin:
so that it does it all by itself; this'd eliminate the need for setConversion public method)
XXX-BG: I'm not sure now what I meant by this...
"""
self.updateConversionCache()
tformat = makeSortedTuple(format)
return self._cached_mime.get(tformat, ''), self._cached_data.get(tformat, '')
......@@ -129,6 +146,7 @@ class ConversionCacheMixin:
"""
Get cache details as string (for debugging)
"""
self.updateConversionCache()
s = 'CACHE INFO:<br/><table><tr><td>format</td><td>size</td><td>time</td><td>is changed</td></tr>'
for f in self._cached_time.keys():
t = self._cached_time[f]
......
......@@ -107,7 +107,7 @@ class File(Document, CMFFile):
file = kw.get('file')
precondition = kw.get('precondition')
if self._isNotEmpty(file):
CMFFile._edit(self, precondition=precondition, file=file)
self._setFile(file, precondition=precondition)
del kw['file']
Base._edit(self, **kw)
......@@ -141,6 +141,7 @@ class File(Document, CMFFile):
getcontentlength = get_size
# File management virtual accessor
security.declareProtected(Permissions.View, 'hasFile')
def hasFile(self):
"""
......@@ -151,6 +152,15 @@ class File(Document, CMFFile):
return getattr(self,'data') is not None
return False
def _setFile(self, data, precondition=None):
self.clearConversionCache()
CMFFile._edit(self, precondition=precondition, file=data)
security.declareProtected(Permissions.ModifyPortalContent,'setFile')
def setFile(self, data, precondition=None):
self._setFile(data, precondition=precondition)
self.reindexObject()
security.declarePrivate('_unpackData')
def _unpackData(self,data):
"""
......@@ -179,6 +189,7 @@ class File(Document, CMFFile):
security.declareProtected(Permissions.ModifyPortalContent,'PUT')
def PUT(self,REQUEST,RESPONSE):
self.clearConversionCache()
CMFFile.PUT(self,REQUEST,RESPONSE)
self.DMS_ingestFile(fname=self.getId()) # XXX-JPS we should call here Document_discoverMetadata
# with the filename as parameter
......
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