Introduction

For web developers wishing to provide downloadable documents of various types common on Linux desktop systems, it can be frustrating to assemble all of the mime types required to instruct clients how to view the different kinds of documents. In that Linux is a very rich environment, people install and use both Gnome and KDE applications on their systems. Unfortunately, there's no simple way to extract the mime types from both the Gnome and KDE environments, providing Apache with the mime.types file it needs.

Downloads

Background

`mimetypes` is a perl script to extract MIME type information from a Linux machine running a combination of KDE and GNOME applications and display it nicely formatted in a manner appropriate to Apache.

The need for this script was motivated in the course of setting up an internal company document repository for the Open Source Development Lab (www.osdl.org). Because OSDL is heavily Linux-oriented, the bulk of its documentation is done using a wide variety of open source (and often cutting-edge) applications, and so new document mime types tend to spring up like wild bamboo. We needed to be able to update our server's mime.types file to be more representative of what actual mimetypes are assumed by the diversity of workstations in the lab.

Unfortunately, as near as could be ascertained, neither KDE nor GNOME contain a command to produce an Apache-compatible listing of mimetypes on the machine. Furthermore, the way that GNOME registers its mime types is different than the way KDE does it. Thus, the problem is not reducible to some fashion of 'grep -r'. ;-)

This script, 'mimetypes', was written to address this specific problem. It scans the GNOME and KDE program registries, extracts mimetypes and file extensions, parses and merges them into a single list, and renders the information in an Apache-compatible fashion.

Further, it is able to take one or more Apache-formatted mimetype files and merge those as well; this is extremely handy for merging the output of 'mimetypes' from several workstations with the central server's mime type file.

The algorithm used by this program uses a hash inversion mechanism to ensure that any given file extension is registered to only a single mime type. This is important because different computers could have the same file extension mapped to different mimetypes (e.g., *.doc could be mapped to WordPerfect one one machine, KWord on another, Star Office on a third...) The decision method for resolving conflicts is 'sequential overwrite'. First, GNOME types are read from the system. Next, KDE types are read, and in any cases of conflict, the KDE file extension mappings replace the GNOME ones. Finally, any apache formatted files given by the user on the commandline are read in turn, and in each case its mappings are used in case of any conflicts.

Problems, questions, etc. may be sent to brycehar@bryceharrington.com.

mimetypes is Copyright (c) 2002, by Bryce W. Harrington. All rights reserved.

You may distribute this code under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.

Brought to you by the Open Source Development Labs