Commit da1616cb authored by Fred Drake's avatar Fred Drake

Add a "mime-types" configuration value which names a file giving

additional MIME type to filename extension mappings.  The "mime-types"
setting may be given more than once in the configuration file; the
files have the same format at the mime.types file distributed with
Apache.

Not sure if this should be ported to the Zope 2.7 branch.
parent c3bd1b46
......@@ -8,6 +8,12 @@ Zope Changes
Features added
- Added a "mime-types" configuration value which names a file
giving additional MIME type to filename extension mappings.
The "mime-types" setting may be given more than once in the
configuration file; the files have the same format at the
mime.types file distributed with Apache.
- Changed the ZEO server and control process to work with a
single configuration file; this is now the default way to
configure these processes. (It's still possible to use
......
......@@ -11,12 +11,14 @@
#
##############################################################################
"""A utility module for content-type handling."""
__version__='$Revision: 1.19 $'[11:-2]
__version__='$Revision: 1.20 $'[11:-2]
import re, mimetypes
import mimetypes
import os.path
import re
find_binary=re.compile('[\0-\7]').search
find_binary = re.compile('[\0-\7]').search
def text_type(s):
# Yuk. See if we can figure out the type by content.
......@@ -30,70 +32,43 @@ def text_type(s):
return 'text/plain'
# This gives us a hook to add content types that
# aren't currently listed in the mimetypes module.
_addtypes=(
('.mp3', 'audio/mpeg'),
('.ra', 'audio/x-pn-realaudio'),
('.pdf', 'application/pdf'),
('.c', 'text/plain'),
('.bat', 'text/plain'),
('.h', 'text/plain'),
('.pl', 'text/plain'),
('.ksh', 'text/plain'),
('.ram', 'application/x-pn-realaudio'),
('.cdf', 'application-x-cdf'),
('.doc', 'application/msword'),
('.dot', 'application/msword'),
('.wiz', 'application/msword'),
('.xlb', 'application/vnd.ms-excel'),
('.xls', 'application/vnd.ms-excel'),
('.ppa', 'application/vnd.ms-powerpoint'),
('.ppt', 'application/vnd.ms-powerpoint'),
('.pps', 'application/vnd.ms-powerpoint'),
('.pot', 'application/vnd.ms-powerpoint'),
('.pwz', 'application/vnd.ms-powerpoint'),
('.eml', 'message/rfc822'),
('.nws', 'message/rfc822'),
('.mht', 'message/rfc822'),
('.mhtml', 'message/rfc822'),
('.css', 'text/css'),
('.p7c', 'application/pkcs7-mime'),
('.p12', 'application/x-pkcs12'),
('.pfx', 'application/x-pkcs12'),
('.js', 'application/x-javascript'),
('.pct', 'image/pict'),
('.pic', 'image/pict'),
('.pict', 'image/pict'),
('.m1v', 'video/mpeg'),
('.mpa', 'video/mpeg'),
('.vcf', 'text/x-vcard'),
('.xml', 'text/xml'),
('.xsl', 'text/xml'),
('.xul', 'application/vnd.mozilla.xul+xml'),
)
for name, val in _addtypes:
mimetypes.types_map[name]=val
def guess_content_type(name='', body='', default=None):
# Attempt to determine the content type (and possibly
# content-encoding) based on an an object's name and
# entity body.
type, enc=mimetypes.guess_type(name)
type, enc = mimetypes.guess_type(name)
if type is None:
if body:
if find_binary(body) is not None:
type=default or 'application/octet-stream'
type = default or 'application/octet-stream'
else:
type=(default or text_type(body)
type = (default or text_type(body)
or 'text/x-unknown-content-type')
else:
type=default or 'text/x-unknown-content-type'
type = default or 'text/x-unknown-content-type'
return type.lower(), enc and enc.lower() or None
if __name__=='__main__':
items=mimetypes.types_map.items()
def add_files(filenames):
# Make sure the additional files are either loaded or scheduled to
# be loaded:
if mimetypes.inited:
# Re-initialize the mimetypes module, loading additional files
mimetypes.init(filenames)
else:
# Tell the mimetypes module about the additional files so
# when it is initialized, it will pick up all of them, in
# the right order.
mimetypes.knownfiles.extend(filenames)
# Provide definitions shipped as part of Zope:
here = os.path.dirname(os.path.abspath(__file__))
add_files([os.path.join(here, "mime.types")])
if __name__ == '__main__':
items = mimetypes.types_map.items()
items.sort()
for item in items: print "%s:\t%s" % item
application-x-cdf cdf
application/msword doc dot wiz
application/pdf pdf
application/pkcs7-mime p7c
application/vnd.mozilla.xul+xml xul
application/vnd.ms-excel xlb xls
application/vnd.ms-powerpoint pot ppa pps ppt pwz
application/x-javascript js
application/x-pkcs12 p12 pfx
application/x-pn-realaudio ram
audio/mpeg mp3
audio/x-pn-realaudio ra
image/pict pct pic pict
message/rfc822 eml nws mht mhtml
text/css css
text/plain bat c h pl ksh
text/x-vcard vcf
text/xml xml xsl
video/mpeg m1v mpa
......@@ -153,6 +153,11 @@ def root_handler(config):
L.append(d)
Products.__path__[:] = L
# Augment the set of MIME types:
if config.mime_types:
import OFS.content_types
OFS.content_types.add_files(config.mime_types)
# if no servers are defined, create default http server and ftp server
if not config.servers:
import ZServer.datatypes
......
......@@ -230,6 +230,22 @@
<metadefault>$instancehome/var</metadefault>
</key>
<multikey name="mime-types" datatype="existing-file">
<description>
This specifies additional lists of MIME types that should be
loaded into Python's "mimetypes" module. The files should have
the same form as the mime.types file distributed with the Apache
HTTP server.
Each line describing a MIME type should contain the major/minor
type, followed by a space-separated list of file extensions used
for files of that type. The extensions must not include the '.'
used to separate an extension from the base file name.
Blank lines and lines beginning with a '#' are ignored.
</description>
</multikey>
<multikey name="products" datatype="existing-directory">
<description>
This specifies additional product directories which are added to
......
......@@ -393,7 +393,8 @@ setup(
author=AUTHOR,
packages=['OFS', 'OFS.tests'],
data_files=[['OFS/dtml', ['OFS/dtml/*']],
data_files=[['OFS', ['OFS/mime.types']],
['OFS/dtml', ['OFS/dtml/*']],
['OFS/standard', ['OFS/standard/*']],
['OFS/www', ['OFS/www/*']]],
)
......
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