Commit f48eeb6f authored by Jérome Perrin's avatar Jérome Perrin

PDF: use cache conversion to cache PDF content information

parent 5875c450
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
############################################################################## ##############################################################################
import tempfile, os, pickle import tempfile, os, pickle
import json
import zope.interface import zope.interface
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
...@@ -278,18 +279,19 @@ class PDFDocument(Image): ...@@ -278,18 +279,19 @@ class PDFDocument(Image):
def getContentInformation(self): def getContentInformation(self):
"""Returns the information about the PDF document with pdfinfo. """Returns the information about the PDF document with pdfinfo.
""" """
content_information_pseudo_format = 'content_information_pseudo_format'
if not self.hasData(): if not self.hasData():
return {} return {}
try: # XXX we reuse the conversion infrastructure for cache
return self._content_information.copy() if self.hasConversion(format=content_information_pseudo_format):
except AttributeError: mime, data = self.getConversion(format=content_information_pseudo_format)
pass return json.loads(data)
tmp = tempfile.NamedTemporaryFile() tmp = tempfile.NamedTemporaryFile()
tmp.write(self.getData()) tmp.write(self.getData())
tmp.seek(0) tmp.seek(0)
command_result = None command_result = None
try: try:
# First, we use pdfinfo to get standard metadata # First, we use pdfinfo to get standard metadata
command = ['pdfinfo', '-meta', '-box', tmp.name] command = ['pdfinfo', '-meta', '-box', tmp.name]
try: try:
...@@ -341,16 +343,10 @@ class PDFDocument(Image): ...@@ -341,16 +343,10 @@ class PDFDocument(Image):
finally: finally:
tmp.close() tmp.close()
# Store cache as an instance of document. FIXME: we usually try to avoid this self.setConversion(json.dumps(result), format=content_information_pseudo_format)
# pattern and cache the result of methods using content md5 as a cache key.
self._content_information = result
return result.copy() return result.copy()
def _setFile(self, data, precondition=None): def _setFile(self, data, precondition=None):
try:
del self._content_information
except (AttributeError, KeyError):
pass
# Use File._setFile because Image._setFile will guess image size & content # Use File._setFile because Image._setFile will guess image size & content
# type # type
File._setFile(self, data, precondition=precondition) File._setFile(self, data, precondition=precondition)
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