Commit db489629 authored by Jean-Paul Smets's avatar Jean-Paul Smets

make convert method return mime and data.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@13690 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 2675bd5e
...@@ -147,8 +147,9 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -147,8 +147,9 @@ class OOoDocument(File, ConversionCacheMixin):
if not self.hasBaseData(): if not self.hasBaseData():
self.convertToBaseFormat() self.convertToBaseFormat()
# Else try to convert the document and return it # Else try to convert the document and return it
result = self.convert(format=format) mime, result = self.convert(format=format)
mime = getToolByName(self, 'mimetypes_registry').lookupExtension('name.%s' % format) # XXX Sad we can not get the mimetype from the conversion cache if not mime:
mime = getToolByName(self, 'mimetypes_registry').lookupExtension('name.%s' % format)
RESPONSE.setHeader('Content-Length', len(result)) RESPONSE.setHeader('Content-Length', len(result))
RESPONSE.setHeader('Content-Type', mime) RESPONSE.setHeader('Content-Type', mime)
RESPONSE.setHeader('Accept-Ranges', 'bytes') RESPONSE.setHeader('Accept-Ranges', 'bytes')
...@@ -227,14 +228,14 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -227,14 +228,14 @@ class OOoDocument(File, ConversionCacheMixin):
if format == 'text-content': if format == 'text-content':
# Extract text from the ODF file # Extract text from the ODF file
cs = cStringIO.StringIO() cs = cStringIO.StringIO()
cs.write(self._unpackData(self.oo_data)) cs.write(self._unpackData(self.getBaseData()))
z = zipfile.ZipFile(cs) z = zipfile.ZipFile(cs)
s = z.read('content.xml') s = z.read('content.xml')
s = self.rx_strip.sub(" ", s) # strip xml s = self.rx_strip.sub(" ", s) # strip xml
s = self.rx_compr.sub(" ", s) # compress multiple spaces s = self.rx_compr.sub(" ", s) # compress multiple spaces
cs.close() cs.close()
z.close() z.close()
return 'text/text', s return 'text/plain', s
server_proxy = self._mkProxy() server_proxy = self._mkProxy()
kw = server_proxy.run_generate(self.getId(), kw = server_proxy.run_generate(self.getId(),
enc(self._unpackData(self.getBaseData())), enc(self._unpackData(self.getBaseData())),
...@@ -251,6 +252,9 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -251,6 +252,9 @@ class OOoDocument(File, ConversionCacheMixin):
""" """
# Make sure we can support html and pdf by default # Make sure we can support html and pdf by default
is_html = 0 is_html = 0
if format == 'base-data':
if not self.hasBaseData(): self.convertToBaseFormat()
return self.getBaseContentType(), self.getBaseData()
if format == 'pdf': if format == 'pdf':
format_list = [x for x in self.getTargetFormatList() if x.endswith('pdf')] format_list = [x for x in self.getTargetFormatList() if x.endswith('pdf')]
format = format_list[0] format = format_list[0]
...@@ -258,6 +262,16 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -258,6 +262,16 @@ class OOoDocument(File, ConversionCacheMixin):
format_list = [x for x in self.getTargetFormatList() if x.startswith('html')] format_list = [x for x in self.getTargetFormatList() if x.startswith('html')]
format = format_list[0] format = format_list[0]
is_html = 1 is_html = 1
elif format in ('txt', 'text', 'text-content'):
format_list = self.getTargetFormatList()
if format in format_list:
format = format_list[format_list.index(format)]
if 'txt' in format_list:
format = format_list[format_list.index('txt')]
elif 'text' in format_list:
format = format_list[format_list.index('text')]
else:
return 'text/plain', self.asTextContent()
# Raise an error if the format is not supported # Raise an error if the format is not supported
if not self.isTargetFormatAllowed(format): if not self.isTargetFormatAllowed(format):
raise ConversionError, 'Target format %s is not supported' % format raise ConversionError, 'Target format %s is not supported' % format
...@@ -293,7 +307,7 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -293,7 +307,7 @@ class OOoDocument(File, ConversionCacheMixin):
This is the simplest way, the most universal and it is compatible This is the simplest way, the most universal and it is compatible
will all formats. will all formats.
""" """
return self.convert(format='text-content') return self._convert(format='text-content')
security.declareProtected(Permissions.ModifyPortalContent, 'populateContent') security.declareProtected(Permissions.ModifyPortalContent, 'populateContent')
def populateContent(self, zip_file=None): def populateContent(self, zip_file=None):
......
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