From af9fe51ca13b55f2a361192760207dbff9f22a2b Mon Sep 17 00:00:00 2001
From: Yusei Tahara <yusei@nexedi.com>
Date: Thu, 29 May 2008 11:57:56 +0000
Subject: [PATCH] This method is used when a document is exported to external
 resource as raw data(for example, attach to an email).

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@21219 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/File.py | 41 +++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/product/ERP5/Document/File.py b/product/ERP5/Document/File.py
index 053a49783c..1da68b44d4 100644
--- a/product/ERP5/Document/File.py
+++ b/product/ERP5/Document/File.py
@@ -35,6 +35,7 @@ from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
 from Products.ERP5Type.Cache import CachingMethod
 from Products.ERP5.Document.Document import Document
 from Products.ERP5.Document.Document import ConversionCacheMixin
+from Products.ERP5.Document.Document import ConversionError
 from Products.ERP5Type.Base import Base
 from Products.CMFDefault.File import File as CMFFile
 from zLOG import LOG
@@ -189,3 +190,43 @@ class File(Document, CMFFile, ConversionCacheMixin):
   manage_FTPget = CMFFile.manage_FTPget
   manage_FTPlist = CMFFile.manage_FTPlist
   manage_FTPstat = CMFFile.manage_FTPstat
+
+  security.declareProtected(Permissions.AccessContentsInformation, 'getMimeTypeAndContent')
+  def getMimeTypeAndContent(self):
+    """This method returns a tuple which contains mimetype and content."""
+    from Products.ERP5.Document.EmailDocument import MimeTypeException
+    # return a tuple (mime_type, data)
+    mime_type = None
+    content = None
+
+    # WARNING - this could fail since getContentType
+    # is not (yet) part of Document API
+    if getattr(self, 'getContentType', None) is not None:
+      mime_type = self.getContentType()
+    elif getattr(self, 'getTextFormat', None) is not None:
+      mime_type = self.getTextFormat()
+    else:
+      raise ValueError, "Cannot find mimetype of the document."
+
+    if mime_type is not None:
+      try:
+        mime_type, content = self.convert(mime_type)
+      except ConversionError:
+        mime_type = self.getBaseContentType()
+        content = self.getBaseData()
+      except (NotImplementedError, MimeTypeException):
+        pass
+
+    if content is None:
+      if getattr(self, 'getTextContent', None) is not None:
+        content = self.getTextContent()
+      elif getattr(self, 'getData', None) is not None:
+        content = self.getData()
+      elif getattr(self, 'getBaseData', None) is not None:
+        content = self.getBaseData()
+
+    if content and not isinstance(content, str):
+      content = str(content)
+
+    return (mime_type, content)
+
-- 
2.30.9