def updateBaseMetadata(self, **kw): """ Updates metadata information in the converted OOo document based on the values provided by the user. This is implemented through the invocation of the conversion server. """ if not self.hasBaseData(): # XXX please pass a meaningful description of error as argument raise NotConvertedError() server_proxy = DocumentConversionServerProxy(self) response_code, response_dict, response_message = \ server_proxy.run_setmetadata(self.getId(), enc(str(self.getBaseData())), kw) if response_code == 200: # successful meta data extraction self._setBaseData(dec(response_dict['data'])) self.updateFileMetadata( ) # record in workflow history # XXX must put appropriate comments. else: # Explicitly raise the exception! raise ConversionError( "OOoDocument: error getting document metadata (Code %s: %s)" % (response_code, response_message))
def _getConversionFromProxyServer(self, format): # pylint: disable=redefined-builtin """ Communicates with server to convert a file """ if not self.hasBaseData(): # XXX please pass a meaningful description of error as argument raise NotConvertedError() if format == 'text-content': # Extract text from the ODF file cs = cStringIO.StringIO() cs.write(str(self.getBaseData())) z = zipfile.ZipFile(cs) s = z.read('content.xml') s = self.rx_strip.sub(" ", s) # strip xml s = self.rx_compr.sub(" ", s) # compress multiple spaces cs.close() z.close() return 'text/plain', s server_proxy = DocumentConversionServerProxy(self) orig_format = self.getBaseContentType() generate_result = server_proxy.run_generate( self.getId(), enc(str(self.getBaseData())), None, format, orig_format) try: _, response_dict, _ = generate_result except ValueError: # This is for backward compatibility with older oood version returning # only response_dict response_dict = generate_result # XXX: handle possible OOOd server failure return response_dict['mime'], Pdata(dec(response_dict['data']))
def _oooConvertByFormat(self, printout, content_type, extra_context, REQUEST, format, batch_mode): """ Convert the ODF document into the given format. Keyword arguments: printout -- ODF document content_type -- the content type of the printout extra_context -- extra_context including a format REQUEST -- Request object format -- requested output format batch_mode -- Disable headers overriding """ if REQUEST is not None and not format: format = REQUEST.get('format', None) filename = self.getProperty('filename') # Call refresh through cloudooo # XXX This is a temporary implementation: # Calling a webservice must be done through a WebServiceMethod # and a WebServiceConnection from erp5.component.document.Document import DocumentConversionServerProxy, enc, dec server_proxy = DocumentConversionServerProxy(self) extension = guess_extension(content_type).strip('.') printout = dec(server_proxy.convertFile(enc(printout), extension, # source_format extension, # destination_format False, # zip True)) # refresh # End of temporary implementation if not format: if REQUEST is not None and not batch_mode: REQUEST.RESPONSE.setHeader('Content-Length', len(printout)) REQUEST.RESPONSE.setHeader('Content-Type','%s' % content_type) REQUEST.RESPONSE.setHeader('Content-disposition', 'inline;filename="%s%s"' % \ (filename, guess_extension(content_type) or '')) return printout tmp_ooo = self.newContent(temp_object=True, portal_type='OOo Document', id=self.title_or_id()) tmp_ooo.edit(data=printout, base_data=printout, filename=self.title_or_id(), content_type=content_type, base_content_type=content_type) mime, data = tmp_ooo.convert(format) if REQUEST is not None and not batch_mode: REQUEST.RESPONSE.setHeader('Content-Length', len(data)) REQUEST.RESPONSE.setHeader('Content-type', mime) REQUEST.RESPONSE.setHeader('Content-disposition', 'attachment;filename="%s.%s"' % (filename, format)) return str(data)
def convertTo(self, format): # XXX Must be replaced by portal_data_adapters soon from erp5.component.document.Document import DocumentConversionServerProxy server_proxy = DocumentConversionServerProxy(self.context) response_code, response_dict, message = \ server_proxy.getAllowedTargetItemList(self.mimetype) allowed_extension_list = response_dict['response_data'] if format in dict(allowed_extension_list): # XXX Must be replaced by portal_data_adapters soon from erp5.component.document.Document import enc, dec response_code, response_dict, message = server_proxy.run_generate( '', enc(self.data), None, format, self.mimetype) data = dec(response_dict['data']) if self.mimetype == 'text/html': data = self.includeImageList(data) return data else: raise ConversionError('Format not allowed %s' % format)
def _convertToBaseFormat(self): """ Converts the original document into ODF by invoking the conversion server. Store the result on the object. Update metadata information. """ server_proxy = DocumentConversionServerProxy(self) response_code, response_dict, response_message = server_proxy.run_convert( self.getFilename() or self.getId(), enc(str(self.getData())), None, None, self.getContentType()) if response_code == 200: # sucessfully converted document self._setBaseData(dec(response_dict['data'])) metadata = response_dict['meta'] self._base_metadata = metadata if metadata.get('MIMEType', None) is not None: self._setBaseContentType(metadata['MIMEType']) else: # Explicitly raise the exception! raise ConversionError( "OOoDocument: Error converting document to base format. (Code %s: %s)" % (response_code, response_message))
def convert(self, orig, data, context=None, **kwargs): server_proxy = DocumentConversionServerProxy(context) source_mimetype = self._getAllowedSourceMimetypeFromConversionServer( server_proxy) if source_mimetype is None: raise ConversionError( "Format(s) not allowed on Conversion Server %r" % self.inputs) source_format = self._getFormatFromMimetype(source_mimetype) destination_format = self._getFormatFromMimetype(self.output) data.setData( dec( server_proxy.convertFile( enc(orig), source_format, destination_format, # Default values are ConversionServer default ones kwargs.get('zip', False), kwargs.get('refresh', False), kwargs.get('conversion_kw', {})))) return data