Indy 9
TIdMimeTable
Hierarchy, Properties, Methods, See Also, Unit: IdGlobal
Provides access to MIME types for the local computer.
TIdMimeTable = class(TObject)
Unit
IdGlobal
Description
TIdMimeTable is a class used in Indy to provide access to the known MIME types and file name extensions from settings on the local computer system. TIdMimeTable provides a degree of isolation from the issues related to platform-specific MIME type storage.

On the Windows platform, TIdMimeTable relies on an initial set of common MIME types supplemented by values harvested from the Windows HKEY_CLASSES_ROOT Registry key. On the Linux platform, there is no universal counterpart to the Windows Registry for MIME types. A text-based configuration file is normally present in most Linux distributions. But it's name and location in the file system hierarchy is different for each family of distribution. Each Desktop/Window manager, and many applications, maintain their own MIME type configuration files. Their names and locations are almost always radically different, too.

To accommodate developers on both platforms, TIdMimeTable provides methods that can be used to control the location, format, and content of values used for MIME types. The OnBuildCache event handler can be used to override loading of the cached MIME types registered on the local computer. In addition, LoadFromStrings and SaveToStrings can be used to override the values loaded and stored for TIdMimeTable.

GetFileMIMEType can be used to retrieve the MIME type for a specified file name based on the file extension. GetDefaultFileExt can be used to retrieve the file extension associated with the specified MIME type. Use AddMimeType to add a file extension and MIME type to TIdMimeTable.

TIdMimeTable is designed primarily for use by client applications. But, TIdMimeTable is also used in TIdCustomHTTPServer to determine the file names and extensions that can be provided for an HTTP request with "Accept" header(s) for specific MIME types.


TIdMimeTable.OnBuildCache
TIdMimeTable, See Also
Event handler used to load MIME type mappings from a non-standard source.
property OnBuildCache: TNotifyEvent;
Description
OnBuildCache is a TNotifyEvent property that represents the event handler signalled when the MIME type mapping table needs to be loaded from an external or non-standard source.

When a procedure has been assigned to the OnBuildCache event handler, it is entirely responsible for loading the file extensions and MIME type/subtypes used in the class instance. When OnBuildCache is unassigned, the default behavior is to loaded the MIME type and file extension mappings specific to the development platform.

OnBuildCache is triggered in the BuildCache method.


TIdMimeTable.AddMimeType
TIdMimeTable, See Also
Adds a file extension to MIME type and subtype mapping.
procedure AddMimeType(const Ext: string; const MIMEType: string);
Parameters
const Ext: string
File extensions for the MIME type and subtype.
const MIMEType: string
IANA MIME type and subtype for the mapping.
Description
AddMimeType is a procedure that allows adding the new file name extension specified in Ext and the MIME type specified in MIMEType to the mappings in the TIdMimeTable instance. AddMimeType will raise an EIdException exception if the file extension in Ext already exists in the MIME type mapping.

Ext is the case-insensitive file extension including the '.' character. If the period character is not included in Ext, it will be inserted prior to inclusion in the list of file extensions. If Ext is an empty string (''), an EIdException will be raised with the message RSMIMEExtensionEmpty.

MIMEType is the case-insensitive IANA registered MIME type and subtype to be associated with the file extension in Ext. If MIMEType contains an empty string (''), and EIdException will be raised with the message RSMIMEMIMETypeEmpty.

Use GetFileMIMEType to get a MIME type for a specified file name and extension. Use GetDefaultFileExt to find the initial file extension found for a given MIME type and subtype. The MIME type mapping table can be loaded with custom values using LoadFromStrings.


TIdMimeTable.BuildCache
TIdMimeTable, See Also
Populates MIME types and file extensions.
procedure BuildCache; virtual;
Description
BuildCache is a procedure used to control populating the MIME type/subtype and file name extension mappings used in the TIdMimeTable instance. BuildCache allows the MIME type mapping to be loaded using the default values for the development platform, or populated in the OnBuildCache event handler. When a procedure has been assigned to the OnBuildCache event handler, it is entirely responsible for loading the file extensions and MIME type/subtypes used in the class instance.

When OnBuildCache is unassigned, the default behavior is to loaded the MIME type and file extension mappings specific to the development platform. This includes loading a common set of MIME type mapping values used for both Windows and Linux development platforms.

On the Windows platform, TIdMimeTable supplements these values by harvesting content types from the Windows HKEY_CLASSES_ROOT Registry key.

On the Linux platform, there is no universal counterpart to the Windows Registry for MIME types. A text-based configuration file is normally present in most Linux distributions. But it's name and location in the file system hierarchy is different for each family of distributions. Each Desktop/Window manager, and many applications, maintain their own MIME type configuration files. Their names and locations are almost always radically different, too. On the Linux platform, no additional MIME types are loaded other than the known values used for both development platforms.

No action is performed, however, when existing values are found in the MIME type mappings.

Use LoadFromStrings to load custom MIME type mappings from a TStringList.


TIdMimeTable.Create
TIdMimeTable, See Also
Constructor for the object instance.
constructor Create(Autofill: boolean = true); virtual;
Parameters
Autofill: boolean = true
Automatically load MIME types and file extensions.
Description
Create is the constructor for the object instance, and initializes the TStringList objects used to store MIME types and File extensions.

When Autofill is True, Create calls the BuildCache method to populate the MIME type entries from system settings on the local computer, or using the OnBuildCache event handler. When Autofill is False, use LoadFromStrings to populate the MIME type mapping table.


TIdMimeTable.Destroy
TIdMimeTable, See Also
Destructor for the object instance.
destructor Destroy; override;
Description
Destroy is the destructor for the object instance, and is responsible for freeing and releasing the TStringList instances used to store File Extensions and MIME types used by the class. Destroy calls the inherited Destroy method prior to exiting.

TIdMimeTable.GetDefaultFileExt
TIdMimeTable, See Also
Returns the file extension for the specified MIME type.
function GetDefaultFileExt(Const MIMEType: string): string;
Parameters
Const MIMEType: string
The MIME type and subtype to examine.
Return Value
String - The file extension for the MIME type, or ''.
Description
GetDefaultFileExt is a String function used to retrieve the file extension for the case-insensitive registered MIME type specified in MIMEType. GetDefaultFileExt will call BuildCache if the value in MIMEType is not already present in the MIME type mapping table.

The return value for GetDefaultFileExt will contain the file extension, including the '.' character, associate dwith the MIME type in MIMEType. The return value for the method can be an empty string ('') when the value in MIMEType cannot be found in the MIME type mapping after checking the cached values.

Use AddMimeType to add a new file extension to MIME type mapping to the table.

Use GetFileMIMEType to retrieve the MIME type for a specified file name and file extension.


TIdMimeTable.GetFileMIMEType
TIdMimeTable
Returns the MIME type for a file name.
function GetFileMIMEType(const AFileName: string): string;
Parameters
fileName
The file name to be examined.
Return Value
String - The registered MIME type for the file name.
Description
GetFileMIMEType is a String function used to retrieve the registered MIME type for the file name specified in Filename. GetFileMIMEType examines the currently cached MIME types, but calls BuildCache is no registered MIME type exists in the cache for the specified file. If no MIME type is found after reloading the cache, the value "application/octet-stream" is used as the MIME type for the file.

TIdMimeTable.LoadFromStrings
TIdMimeTable, See Also
Loads the MIME Type mapping table from the specified TStrings values.
procedure LoadFromStrings(AStrings: TStrings; const MimeSeparator: Char = '=');
Parameters
AStrings: TStrings
Values to be loaded into the MIME type mapping table.
const MimeSeparator: Char = '='
Character used to delimit file extention and MIME type values in AStrings.
Description
LoadFromStrings is a procedure used to parse file extension and MIME type and subtype values in AStrings in the MIME mapping table. MimeSeparator is the character used to delimit file extention and MIME type and subtype values in AStrings.

LoadFromStrings clears any existing file extension and MIME type mappings prior to loading each line in AStrings. LoadFromStrings converts file extensions to their lowercase representation prior to addition to the file extenstion list. LoadFromStrings calls AddMimeType if the file extension does not already exist in the MIME type mapping table.


TIdMimeTable.SaveToStrings
TIdMimeTable
Stores MIME Type mapping values to a TStrings variable.
procedure SaveToStrings(AStrings: TStrings; const MimeSeparator: Char = '=');
Parameters
AStrings: TStrings
Destination for MIME Type mapping values.
const MimeSeparator: Char = '='
Delimiter for extension and MIME type values.
Description
SaveToStrings is a procedure used to store values in the MIME type mapping to table to the TStrings specified in AStrings. MimeSeparator is the charcter to use as a delimiter between the file extension and the MIME Type and Subtype in lines added to AStrings. SaveToStrings clears any existing values in AStrings prior to storing the MIME Type mappings, with one file extension, delimiter character, and MIME Type value per line.

Created with Doc-O-Matic 2 donated to Project JEDI. Commercial license available from the Doc-O-Matic site.