HTTP-Promise
view release on metacpan or search on metacpan
lib/HTTP/Promise/MIME.pm view on Meta::CPAN
my $file = shift( @_ ) || return( $self->error( "No file was provided." ) );
require Module::Generic::File::Magic;
my $magic = Module::Generic::File::Magic->new( flags => MAGIC_MIME_TYPE ) ||
return( Module::Generic::File::Magic->error );
my $mime = $magic->from_file( "$file" ) ||
return( $self->pass_error( $magic->error ) );
return( $mime );
}
sub mime_type_from_suffix
{
my $self = shift( @_ );
my $suff = shift( @_ ) || return( $self->error( "No suffix was provided." ) );
$suff = lc( $suff );
my $types = $self->types;
foreach my $m ( keys( %$types ) )
{
my $ar = $types->{ $m };
if( scalar( grep( $_ eq $suff, @$ar ) ) )
{
return( $m );
}
}
# Empty, but not undef, because undef is reserved for errors
return( '' );
}
sub suffix
{
my $self = shift( @_ );
my $mime = shift( @_ ) ||
return( $self->error( "No mime type was provided to get its corresponding suffixes." ) );
my $types = $self->types;
return( wantarray() ? [@{$types->{ $mime }}] : $types->{ $mime }->[0] );
}
sub types { return( shift->_set_get_hash_as_mix_object( 'types', @_ ) ); }
sub _parse_file
{
my $self = shift( @_ );
my $file = shift( @_ ) || return;
my $f = $self->new_file( $file ) || return( $self->pass_error );
$f->open;
my $types = {};
$f->line(sub
{
return(1) if( /^[[:blank:]\h]*\#/ || /^[[:blank:]\h]*$/ );
s/^[[:blank:]\h]+//g;
my( $type, $exts ) = split( /[[:blank:]\h]+/, $_, 2 );
$types->{ $type } = [split( /[[:blank:]]+/, $exts )];
}, chomp => 1 );
return( $types );
}
# The data herein is the result of creating a new instance and dumping its data, like:
# say json_encode( HTTP::Promise::MIME->new( '/some/where/mime.types' )->types );
sub _data
{
my $data = <<'EOT';
{"application/vnd.sun.xml.impress":["sxi"],"application/vnd.mcd":["mcd"],"application/vnd.spotfire.dxp":["dxp"],"application/vnd.jisp":["jisp"],"image/webp":["webp"],"application/pkcs7-signature":["p7s"],"application/xspf+xml":["xspf"],"audio/vnd.nue...
EOT
return( \$data );
}
# NOTE: sub FREEZE is inherited
sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
# NOTE: sub THAW is inherited
1;
# NOTE: POD
__END__
=encoding utf-8
=head1 NAME
HTTP::Promise::MIME - MIME Types and File Extension Class
=head1 SYNOPSIS
use HTTP::Promise::MIME;
my $m = HTTP::Promise::MIME->new ||
die( HTTP::Promise::MIME->error, "\n" );
# or you can specify your own mime.types data by providing a file
my $m = HTTP::Promise::MIME->new( '/etc/mime.types' ) ||
die( HTTP::Promise::MIME->error, "\n" );
my $mime = $m->mime_type( '/some/where/file.txt' ); # text/plain
my $mime = $m->mime_type_from_suffix( 'txt' ); # text/plain
my $ext = $m->suffix( 'application/pgp-signature' ); # asc
my @ext = $m->suffix( 'application/pgp-signature' ); # asc, sig
my $hash = $m->types;
=head1 VERSION
v0.3.0
=head1 DESCRIPTION
L<HTTP::Promise::MIME> is a class to find out the mime type of a file or its suffix a.k.a. extension based on its mime type.
The database of mime types is stored internally, so there is no dependence on outside file. You can, however, specify an optional mime database file as the first parameter when instantiating a new object.
=head1 CONSTRUCTOR
=head2 new
Provided with an optional file path to a C<mime.types> database and this will return a new instance of L<HTTP::Promise::MIME>
If an error occurred, it sets an L<error|Module::Generic/error> and returns C<undef>
=head1 METHODS
=head2 dump
print( $m->dump );
( run in 0.723 second using v1.01-cache-2.11-cpan-39bf76dae61 )