| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::MIME; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 MIME | ||
| 18 | |||
| 19 | Nile::MIME - Handle MIME Types | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | # get mime object instance | ||
| 24 | $mime = $app->mime; | ||
| 25 | |||
| 26 | # get mime type for a file from its extension | ||
| 27 | $mime_type = $mime->for_file("filename.zip"); | ||
| 28 | # application/zip | ||
| 29 | |||
| 30 | # get mime type by name | ||
| 31 | $mime_type = $mime->for_name('xml'); | ||
| 32 | # application/xml | ||
| 33 | |||
| 34 | # add custom mime type | ||
| 35 | $mime->add_type(foo => "text/foo"); | ||
| 36 | |||
| 37 | # add an alias to an existing type | ||
| 38 | $mime->add_alias(bar => "foo"); | ||
| 39 | |||
| 40 | @exts = $mime->extensions(); | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | Nile::MIME - Handle MIME Types. This module extends L<MIME::Types> and all its methods are available. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | use Nile::Base; | ||
| 49 | use MooseX::NonMoose; | ||
| 50 | extends 'MIME::Types'; | ||
| 51 | #=========================================================# | ||
| 52 | has 'default' => ( | ||
| 53 | is => 'rw', | ||
| 54 | default => 'application/data', | ||
| 55 | ); | ||
| 56 | |||
| 57 | |||
| 58 | has 'custom_types' => ( | ||
| 59 | is => 'rw', | ||
| 60 | isa => 'HashRef', | ||
| 61 | default => sub { +{ } } | ||
| 62 | ); | ||
| 63 | |||
| 64 | #=========================================================# | ||
| 65 | =head2 for_file | ||
| 66 | |||
| 67 | $mime_type = $mime->for_file('filename.pdf'); | ||
| 68 | |||
| 69 | Returns the mime type for a file, based on a file extension. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub for_file { | ||
| 74 | my ($self, $file) = @_; | ||
| 75 | my ($ext) = $file =~ /\.([^.]+)$/; | ||
| 76 | return $self->default unless $ext; | ||
| 77 | return $self->for_name($ext); | ||
| 78 | } | ||
| 79 | #=========================================================# | ||
| 80 | =head2 for_name | ||
| 81 | |||
| 82 | $mime_type = $mime->for_name('pdf'); | ||
| 83 | |||
| 84 | Returns the mime type for a standard or a custom mime type. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub for_name { | ||
| 89 | my ($self, $name) = @_; | ||
| 90 | return $self->custom_types->{lc $name} || $self->mimeTypeOf(lc $name) || $self->default; | ||
| 91 | } | ||
| 92 | #=========================================================# | ||
| 93 | =head2 add_type | ||
| 94 | |||
| 95 | # add nonstandard mime type | ||
| 96 | $mime->add_type(foo => "text/foo"); | ||
| 97 | |||
| 98 | Add a custom mime type or overrides an existing one. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub add_type { | ||
| 103 | my ($self, $name, $type) = @_; | ||
| 104 | $self->custom_types->{$name} = $type; | ||
| 105 | $self; | ||
| 106 | } | ||
| 107 | #=========================================================# | ||
| 108 | =head2 add_alias | ||
| 109 | |||
| 110 | # add alias to standard or previous alias | ||
| 111 | $mime->add_alias( my_jpg => 'jpg' ); | ||
| 112 | |||
| 113 | Adds an alias to an existing mime type. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub add_alias { | ||
| 118 | my ($self, $alias, $orig) = @_; | ||
| 119 | my $type = $self->name($orig); | ||
| 120 | $self->add_type($alias, $type); | ||
| 121 | return $type; | ||
| 122 | } | ||
| 123 | #=========================================================# | ||
| 124 | |||
| 125 | =pod | ||
| 126 | |||
| 127 | =head1 Bugs | ||
| 128 | |||
| 129 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 130 | |||
| 131 | =head1 HOMEPAGE | ||
| 132 | |||
| 133 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 134 | |||
| 135 | =head1 SOURCE | ||
| 136 | |||
| 137 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 138 | |||
| 139 | =head1 ACKNOWLEDGMENT | ||
| 140 | |||
| 141 | This module is based on L<MIME::Types> and L<Dancer::MIME> | ||
| 142 | |||
| 143 | =head1 SEE ALSO | ||
| 144 | |||
| 145 | See L<Nile> for details about the complete framework. | ||
| 146 | |||
| 147 | =head1 AUTHOR | ||
| 148 | |||
| 149 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 150 | Website: http://www.mewsoft.com | ||
| 151 | |||
| 152 | =head1 COPYRIGHT AND LICENSE | ||
| 153 | |||
| 154 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 155 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 156 | |||
| 157 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | 1; |