#!/usr/bin/perl -w

# Copyright 2001-2006 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

=head1 NAME

magic_mime_map - Use File::MMagic to set MIME type

=head1 SYNOPSIS

    Plugin magic_mime_map

=head1 DESCRIPTION

This plugin uses C<File::MMagic> to set the MIME type of the request. This has
the potential to open the file and be a synchronous action, so use with
caution.

=cut

use File::MMagic;

my $mm = File::MMagic->new();

sub hook_mime_map {
    my ($self, $hd, $filename) = @_;
    
    my $ct = $mm->checktype_filename($filename) || return DECLINED;
    $hd->mime_type($ct);
    
    # we return DECLINED here, even though we have definitively set the MIME
    # type because another MIME plugin may wish to read this value and set
    # it to something else.
    return DECLINED;
}
