Attention

This document was written for an unsupported version of Plone, Plone 2.1.x, and was last updated 352 days ago.

For more information, see the version support policy.

To learn how to upgrade to the current version of Plone, read the upgrade manual.

Mimetypes

by Martin Aspeli last modified Jun 05, 2011 08:42 PM
Working with the MimetypesRegistry's mimetypes_tool to describe a custom MIME type

Registering a new MIME type with the 'mimetypes_tool' is easy. You must write a simple class that represents the type, and call the 'register()' method on the tool to announce its presence. Once registered, the type will become available to 'portal_transforms' and other parts of Archetypes.

The 'text_web_intelligent' class is found in 'intelligenttext/mimetype.py':

from Products.MimetypesRegistry.interfaces import IClassifier
from Products.MimetypesRegistry.MimeTypeItem import MimeTypeItem
from Products.MimetypesRegistry.common import MimeTypeException

from types import InstanceType

class text_web_intelligent(MimeTypeItem):

    __implements__ = MimeTypeItem.__implements__
    __name__   = "Web Intelligent Plain Text"
    mimetypes  = ('text/x-web-intelligent',)
    extensions = ('txt',)
    binary     = 0

This uses the 'MimeTypeItem' base class to provide all the necessary functionality. All you have to do is ensure that the class implements the appropriate interfaces, has a sensible human-readable name (via the '__name__' attribute), and is linked to the appropriate file extension(s) and MIME type(s).

According to RFC-2046, MIME types are described by a string that contains a major and a minor part, separated by a '/'. In this case, the major type is 'text', and the minor type is 'x-web-intelligent'. The 'x-' prefix is conventionally used for types that are "unofficial", such as the 'text/x-web-intelligent' type. For a list of other types registered, look at 'mimetypes_registry' in the ZMI.

It is possible to register more than one MIME type for the same class, for example where two or more types are equivalent. Hence, the 'mimetypes' variable must be a tuple. The 'extensions' tuple describes the associated file extensions. It can be empty if there is no sensible type. The 'binary' variable should be 1 if the MIME type describes binary content, or 0 if it represents textual content.

To register the type with the 'mimetypes_registry' tool, 'Extensions/Install.py' contains the following code:

from Products.CMFCore.utils import getToolByName

from StringIO import StringIO
from types import InstanceType

from Products.intelligenttext.mimetype import text_web_intelligent

def registerMimeType(self, out, mimetype):
    if type(mimetype) != InstanceType:
        mimetype = mimetype()
    mimetypes_registry = getToolByName(self, 'mimetypes_registry')
    mimetypes_registry.register(mimetype)
    print >> out, "Registered mimetype", mimetype

def unregisterMimeType(self, out, mimetype):
    if type(mimetype) != InstanceType:
        mimetype = mimetype()
    mimetypes_registry = getToolByName(self, 'mimetypes_registry')
    mimetypes_registry.unregister(mimetype)
    print >> out, "Unregistered mimetype", mimetype

...


def install(self):

    out = StringIO()

    print >> out, "Installing text/web-intelligent mimetype and transform"

    # Register mimetype
    registerMimeType(self, out, text_web_intelligent)


    ...

    return out.getvalue()

def uninstall(self):

    out = StringIO()

    ...

    # Remove mimetype
    unregisterMimeType(self, out, text_web_intelligent)

    return out.getvalue()

Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.