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