handle office files that are no valid ZIP file (e.g. because of password protection)

This commit is contained in:
Helmut Merz 2014-03-18 08:04:46 +01:00
parent 8ce1eb7222
commit e0f30d7a96
2 changed files with 19 additions and 6 deletions

View file

@ -1,5 +1,5 @@
# #
# Copyright (c) 2011 Helmut Merz helmutm@cy55.de # Copyright (c) 2014 Helmut Merz helmutm@cy55.de
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -18,8 +18,6 @@
""" """
Integrator interfaces. Integrator interfaces.
$Id$
""" """
from zope.interface import Interface, Attribute from zope.interface import Interface, Attribute
@ -133,3 +131,5 @@ class IOfficeFile(IExternalFile):
It provides access to the document content and properties. It provides access to the document content and properties.
""" """
documentPropertiesAccessible = Attribute(
'Are document properties accessible?')

View file

@ -1,5 +1,5 @@
# #
# Copyright (c) 2013 Helmut Merz helmutm@cy55.de # Copyright (c) 2014 Helmut Merz helmutm@cy55.de
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -26,7 +26,7 @@ from lxml import etree
import os import os
import shutil import shutil
from time import strptime from time import strptime
from zipfile import ZipFile from zipfile import ZipFile, BadZipfile
from zope.cachedescriptors.property import Lazy from zope.cachedescriptors.property import Lazy
from zope import component from zope import component
from zope.component import adapts from zope.component import adapts
@ -52,12 +52,22 @@ class OfficeFile(ExternalFileAdapter):
implements(IOfficeFile) implements(IOfficeFile)
_adapterAttributes = (ExternalFileAdapter._adapterAttributes +
('documentPropertiesAccessible',))
propertyMap = {u'Revision:': 'version'} propertyMap = {u'Revision:': 'version'}
propFileName = 'docProps/custom.xml' propFileName = 'docProps/custom.xml'
corePropFileName = 'docProps/core.xml' corePropFileName = 'docProps/core.xml'
fileExtensions = ('.docm', '.docx', 'dotm', 'dotx', 'pptx', 'potx', 'ppsx', fileExtensions = ('.docm', '.docx', 'dotm', 'dotx', 'pptx', 'potx', 'ppsx',
'.xlsm', '.xlsx', '.xltm', '.xltx') '.xlsm', '.xlsx', '.xltm', '.xltx')
def getDocumentPropertiesAccessible(self):
return getattr(self.context, '_documentPropertiesAccessible', True)
def setDocumentPropertiesAccessible(self, value):
self.context._documentPropertiesAccessible = value
documentPropertiesAccessible = property(
getDocumentPropertiesAccessible, setDocumentPropertiesAccessible)
@Lazy @Lazy
def logger(self): def logger(self):
return getLogger('loops.integrator.office.base.OfficeFile') return getLogger('loops.integrator.office.base.OfficeFile')
@ -84,9 +94,10 @@ class OfficeFile(ExternalFileAdapter):
return result return result
try: try:
zf = ZipFile(fn, 'r') zf = ZipFile(fn, 'r')
except IOError, e: except (IOError, BadZipfile), e:
from logging import getLogger from logging import getLogger
self.logger.warn(e) self.logger.warn(e)
self.documentPropertiesAccessible = False
return result return result
if self.corePropFileName not in zf.namelist(): if self.corePropFileName not in zf.namelist():
self.logger.warn('Core properties not found in file %s.' % self.logger.warn('Core properties not found in file %s.' %
@ -123,6 +134,8 @@ class OfficeFile(ExternalFileAdapter):
attributes = {} attributes = {}
# get dc:description from core.xml # get dc:description from core.xml
desc = self.getCoreProperty('description') desc = self.getCoreProperty('description')
if not self.documentPropertiesAccessible:
return
if desc is not None: if desc is not None:
attributes['comments'] = desc attributes['comments'] = desc
dom = self.docPropertyDom['custom'] dom = self.docPropertyDom['custom']