Development/Howto/Mimetypes

Материал из Rosalab Wiki
Версия от 20:55, 8 февраля 2012; D uragan (обсуждение | вклад) (Created MimeTypes Howto)

(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Это снимок страницы. Он включает старые, но не удалённые версии шаблонов и изображений.
Перейти к: навигация, поиск

What is MIME ?

Multipurpose Internet Mail Extensions (MIME) is an Internet Standard that extends the format of e-mail to support:

  • text in character sets other than US-ASCII;
  • non-text attachments;
  • multi-part message bodies; and
  • header information in non-ASCII character sets.

More informations in wikipedia/MIME.

The use of MIME type has been generalized in Unix and other free OS. When the header of the file giving the MIME type is missing, the system can guess the type of file with the (unreliable) suffix of its name.

MIME type is a subject often view as a complex subject. But in fact, it is not. It is often used to designate a lot of different things under the same umbrella.

On Linux systems, MIME type covers two different problems:

  • associating a file to an identifier describing it type; this is called a MIME type.
  • associating a MIME type to a application which will handle files from this type.

Associating file to MIME type

This part is taking of detecting which mimetype should be assigned to a particular file on disk (it is not relevant for network stream, see below for this).

KDE 4 and GNOME (2 and 3) are using Freedesktop.org specification. File used for this specification are packaged in shared-mime-info package. If a file type is not recognized, a small XML file can be written like the following example:

<?xml version="1.0"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/x-foobar">
    <comment>foobar file</comment>
    <glob pattern="*.foobar"/>
     <magic priority="60">
       <match value="foobar-file" type="string" offset="20:140"/>
     </magic>
  </mime-type>
</mime-info>

This file must be installed in /usr/share/mime/packages/ and %update_mime_database / %clean_mime_database macro should be called in package %post / %postun.

This file will detect all files with .foobar as extension as all as file containing string "foobar-file" as offset 20 to 140 (this is often called magic).

It is highly recommended to submit bugs on Freedesktop.org bugzilla for file type not recognized.

Associating MIME type to applications

This part is specified at freedesktop.org.

This part is also relevant for files downloaded through network (for instance a web browser). When a file is requested through a web browser, a mimetype is sent by web server, before the real file and web browser can then decide which application will be used to handle this file. It can happen that incorrect mimetype is sent but this particular case is a web server problem and can't be fixed on the client side.

To associate an application to mimetypes, .desktop file for the particular application should have the following line (list must always end with a semicolon):

MimeType=application/x-foobar;application/x-foobar2;

To add or remove mimetypes to .desktop file when doing packaging, it is best to use desktop-file-install utility. For instance :

desktop-file-install --add-mime-type="foo/bar" --remove-mime-type="broken/mime" --vendor="" --dir %{buildroot}%{_datadir}/applications foobar.desktop

A common question is handle how applications are prioritized when several applications claim to support the same mimetype.

According to freedesktop.org specification, every mimetype specified at /usr/share/applications/defaults.list file (packaged in share-mime-info) should list, in order or preference, the .desktops file corresponding to application to use.

For KDE, a key named InitialPreference is added in application .desktop file and application order is based on this key value : higher InitialPreference value will determine default application.

Exec line of .desktop file can be modified when command-line need to be tuned for specific applications (see here for complete list),

  • if only one file can be added as argument : %f
  • if several files can be added as argument : %F
  • if one url can be added as argument : %u
  • if several urls can be added as argument : %U

Note:
This page is based on the Mandriva Mimetypes Howto.