MIME-Detect

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


0.11 2024-06-03
    * Update XML database to version 2.4
    * Update build infrastructure

0.10 2018-11-30
    * Corrected links to bug tracker etc, thanks to RENEEB
    * Minor distribution cleanup
    * Minor module cleanup, remove vars.pm
    * Switch to File::ShareDir for storing the common data
    * Update to a 2017 version of freedesktop.org.xml mime info

0.09 2018-10-28
    * Re-release with correct META.* information
    * No upgrade necessary

0.08 2016-10-03
    * Identify h++ files correctly

      On versions 5.10+, the "h++" was identified as a possessive RE quantifier,
      which made the code silently misbehave. CPAN Testers++ for testing

MANIFEST  view on Meta::CPAN

lib/MIME/Detect/Type.pm
LICENSE
Makefile.PL
MANIFEST
MANIFEST.SKIP
META.json
META.yml
README
README.mkdn
share/mime-info/COPYING
share/mime-info/freedesktop.org.xml
t/00-load.t
t/01-mime-info.t
t/02-identify.t
t/03-custom.t
t/custom.xml
testrules.yml
update.pod
xt/99-changes.t
xt/99-compile.t
xt/99-manifest.t

README  view on Meta::CPAN


BUG TRACKER

Please report bugs in this module via the RT CPAN bug queue at
L<https://rt.cpan.org/Public/Dist/Display.html?Name=MIME-Detect>
or via mail to L<mime-detect-Bugs@rt.cpan.org>.


SEE ALSO

L<https://www.freedesktop.org/wiki/Software/shared-mime-info/> - the website
where the XML file is distributed

L<File::MimeInfo> - module to read your locally installed and converted MIME database

L<File::LibMagic> - if you can install C<libmagic> and the appropriate C<magic> files

L<File::MMagic> - if you have the appropriate C<magic> files

L<File::MMagic::XS> - if you have the appropriate C<magic> files but want more speed

README.mkdn  view on Meta::CPAN

# METHODS

## `MIME::Detect->new( ... )`

    my $mime = MIME::Detect->new();

Creates a new instance and reads the database distributed with this module.

    my $mime = MIME::Detect->new(
        files => [
            '/usr/share/freedesktop.org/mimeinfo.xml',
            't/mimeinfo.xml',
        ],
    );

## `$mime->read_database %options`

    $mime->read_database(
        xml => MIME::Detect::FreeDesktopOrgDB->get_xml,
        files => [
            'mymime/mymime.xml',
            '/usr/share/freedesktop.org/mime.xml',
        ],
    );

If you want rules in addition to the default
database included with the distribution, you can load the rules from another file.
Passing in multiple filenames will join the multiple
databases. Duplicate file type definitions will not be detected
and will be returned as duplicates.

The rules will be sorted according to the priority specified in the database

README.mkdn  view on Meta::CPAN


    my $type = $mime->mime_type_from_name( 'some/file.ext' );
    print $type->mime_type, "\n"
        if $type;

Returns the most likely type of a file name as [MIME::Detect::Type](https://metacpan.org/pod/MIME%3A%3ADetect%3A%3AType). Returns
`undef` if no file type can be determined.

# SEE ALSO

[https://www.freedesktop.org/wiki/Software/shared-mime-info/](https://www.freedesktop.org/wiki/Software/shared-mime-info/) - the website
where the XML file is distributed

[File::MimeInfo](https://metacpan.org/pod/File%3A%3AMimeInfo) - module to read your locally installed and converted MIME database

[File::LibMagic](https://metacpan.org/pod/File%3A%3ALibMagic) - if you can install `libmagic` and the appropriate `magic` files

[File::MMagic](https://metacpan.org/pod/File%3A%3AMMagic) - if you have the appropriate `magic` files

[File::MMagic::XS](https://metacpan.org/pod/File%3A%3AMMagic%3A%3AXS) - if you have the appropriate `magic` files but want more speed

lib/MIME/Detect.pm  view on Meta::CPAN

=head1 METHODS

=head2 C<< MIME::Detect->new( ... ) >>

  my $mime = MIME::Detect->new();

Creates a new instance and reads the database distributed with this module.

  my $mime = MIME::Detect->new(
      files => [
          '/usr/share/freedesktop.org/mimeinfo.xml',
          't/mimeinfo.xml',
      ],
  );

=cut

sub BUILD( $self, $args ) {
    my %db_args = map { exists( $args->{$_} )? ($_ => $args->{$_}) : () } (qw(xml files));
    $self->read_database( %db_args );
}

lib/MIME/Detect.pm  view on Meta::CPAN

has 'known_types' => (
    is => 'rw',
    default => sub { {} },
);

# The XPath context we use
has 'xpc' => (
    is => 'lazy',
    default => sub {
        my $XPC = XML::LibXML::XPathContext->new;
        $XPC->registerNs('x', 'http://www.freedesktop.org/standards/shared-mime-info');
        $XPC
    },
);

=head2 C<< $mime->read_database %options >>

  $mime->read_database(
      xml => MIME::Detect::FreeDesktopOrgDB->get_xml,
      files => [
          'mymime/mymime.xml',
          '/usr/share/freedesktop.org/mime.xml',
      ],
  );

If you want rules in addition to the default
database included with the distribution, you can load the rules from another file.
Passing in multiple filenames will join the multiple
databases. Duplicate file type definitions will not be detected
and will be returned as duplicates.

The rules will be sorted according to the priority specified in the database

lib/MIME/Detect.pm  view on Meta::CPAN

        substr $self->buffer, $offset-$self->offset
    } else {
        return ''
    };
}

1;

=head1 SEE ALSO

L<https://www.freedesktop.org/wiki/Software/shared-mime-info/> - the website
where the XML file is distributed

L<File::MimeInfo> - module to read your locally installed and converted MIME database

L<File::LibMagic> - if you can install C<libmagic> and the appropriate C<magic> files

L<File::MMagic> - if you have the appropriate C<magic> files

L<File::MMagic::XS> - if you have the appropriate C<magic> files but want more speed

lib/MIME/Detect/FreeDesktopOrgDB.pm  view on Meta::CPAN

package MIME::Detect::FreeDesktopOrgDB;
use strict;
use File::ShareDir 'dist_file';
our $VERSION = '0.12';

=head1 NAME

MIME::Detect::FreeDesktopOrgDB - default freedesktop.org database

=head1 NOTICE

This distribution contains a verbatim copy of the freedesktop.org
MIME database available from
L<https://www.freedesktop.org/wiki/Software/shared-mime-info/>
.
That database is licensed under the General Public License v2,
see the accompanying COPYING file distributed with the file
for its exact terms.

=cut

sub url {'https://www.freedesktop.org/wiki/Software/shared-mime-info/'}

=head2 C<< get_xml >>

    my $xml = MIME::Detect::FreeDesktopOrgDB->get_xml;

Returns a reference to the XML string from C<freedesktop.org.xml> distributed
with this module.

=cut

sub get_xml {
    (my $xml_name = dist_file('MIME-Detect', 'mime-info/freedesktop.org.xml'));
    open my $fh, '<', $xml_name
        or die "Couldn't read '$xml_name': $!";
    binmode $fh;
    local $/;
    return \<$fh>
}

1;

=head1 REPOSITORY

share/mime-info/freedesktop.org.xml  view on Meta::CPAN

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mime-info [
  <!ELEMENT mime-info (mime-type)+>
  <!ATTLIST mime-info xmlns CDATA #FIXED "http://www.freedesktop.org/standards/shared-mime-info">

  <!ELEMENT mime-type (comment+, (acronym,expanded-acronym)? , (icon? | generic-icon? | glob | magic | treemagic | root-XML | alias | sub-class-of)*)>
  <!ATTLIST mime-type type CDATA #REQUIRED>

  <!-- a comment describing a document with the respective MIME type. Example: "WMV video" -->
  <!ELEMENT comment (#PCDATA)>
  <!ATTLIST comment xml:lang CDATA #IMPLIED>

  <!-- a comment describing the respective unexpanded MIME type acronym. Example: "WMV" -->
  <!ELEMENT acronym (#PCDATA)>

share/mime-info/freedesktop.org.xml  view on Meta::CPAN

  <!ATTLIST root-XML localName CDATA #REQUIRED>

  <!ELEMENT alias EMPTY>
  <!ATTLIST alias type CDATA #REQUIRED>

  <!ELEMENT sub-class-of EMPTY>
  <!ATTLIST sub-class-of type CDATA #REQUIRED>
]>

<!--
The freedesktop.org shared MIME database (this file) was created by merging
several existing MIME databases (all released under the GNU GPL).

It comes with ABSOLUTELY NO WARRANTY, to the extent permitted by law. You may
redistribute copies of freedesktop.org.xml under the terms of the GNU General
Public License version 2 or later. For more information about these matters,
see the file named COPYING.

The latest version is available from:

	http://www.freedesktop.org/wiki/Software/shared-mime-info/

To extend this database, users and applications should create additional
XML files in the 'packages' directory and run the update-mime-database
command to generate the output files.
-->

<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/x-atari-2600-rom">
    <comment>Atari 2600 ROM</comment>
    <generic-icon name="application-x-executable"/>
    <glob pattern="*.a26"/>
  </mime-type>
  <mime-type type="application/x-atari-7800-rom">
    <comment>Atari 7800 ROM</comment>
    <generic-icon name="application-x-executable"/>
    <glob pattern="*.a78"/>
    <magic>

share/mime-info/freedesktop.org.xml  view on Meta::CPAN

    <sub-class-of type="application/x-xz"/>
    <glob pattern="*.raw-disk-image.xz"/>
    <glob pattern="*.img.xz"/>
  </mime-type>
  <mime-type type="application/vnd.efi.iso">
    <comment>Raw CD image</comment>
    <sub-class-of type="application/vnd.efi.img"/>
    <generic-icon name="media-optical"/>
    <alias type="application/x-cd-image"/>
    <alias type="application/x-iso9660-image"/>
    <!-- No magic, see https://bugs.freedesktop.org/show_bug.cgi?id=10049 -->
    <glob pattern="*.iso" weight="80"/>
    <glob pattern="*.iso9660"/>
  </mime-type>
  <mime-type type="application/x-compressed-iso">
    <comment>Compressed CD image</comment>
    <generic-icon name="media-optical"/>
    <magic>
      <match value="CISO" type="string" offset="0"/>
    </magic>
    <glob pattern="*.cso"/>

share/mime-info/freedesktop.org.xml  view on Meta::CPAN

        <match type="string" value="{" offset="9:3009"/>
      </match>
      <match type="string" value="import Qml" offset="0:3000">
        <match type="string" value="{" offset="9:3009"/>
      </match>
    </magic>
    <glob pattern="*.qml"/>
    <glob pattern="*.qmltypes"/>
    <glob pattern="*.qmlproject"/>
  </mime-type>
  <mime-type type="application/x-desktop">
    <comment>Desktop entry</comment>
    <sub-class-of type="text/plain"/>
    <generic-icon name="text-x-generic"/>
    <magic>
      <match type="string" value="[Desktop Entry]" offset="0:32"/>
      <match type="string" value="[Desktop Action" offset="0"/>
      <match type="string" value="[KDE Desktop Entry]" offset="0"/>
      <match type="string" value="# Config File" offset="0"/>
      <match type="string" value="# KDE Config File" offset="0"/>
    </magic>
    <glob pattern="*.desktop"/>
    <glob pattern="*.kdelnk"/>
    <alias type="application/x-gnome-app-info"/>
  </mime-type>
  <mime-type type="application/x-fictionbook+xml">
    <comment>FictionBook document</comment>
    <sub-class-of type="application/xml"/>
    <glob pattern="*.fb2"/>
    <magic priority="80">
      <match type="string" value="&lt;FictionBook" offset="0:256"/>
    </magic>

share/mime-info/freedesktop.org.xml  view on Meta::CPAN

  <mime-type type="application/x-tgif">
    <comment>TGIF document</comment>
    <generic-icon name="x-office-document"/>
    <magic>
      <match type="string" value="%TGIF" offset="0"/>
    </magic>
    <glob pattern="*.obj"/>
  </mime-type>
  <mime-type type="application/x-theme">
    <comment>Theme</comment>
    <sub-class-of type="application/x-desktop"/>
    <generic-icon name="package-x-generic"/>
    <glob pattern="*.theme"/>
  </mime-type>
  <mime-type type="application/x-toutdoux">
    <comment>ToutDoux document</comment>
    <generic-icon name="x-office-document"/>
  </mime-type>
  <mime-type type="application/x-trash">
    <comment>Backup file</comment>
    <glob pattern="*~"/>

share/mime-info/freedesktop.org.xml  view on Meta::CPAN

    <glob pattern="*.service"/>
  </mime-type>
  <mime-type type="text/x-systemd-unit">
    <comment>Systemd unit file</comment>
    <sub-class-of type="text/plain"/>
    <magic>
      <!-- Matches part-way through the file. -->
      <match type="string" value="\n[Unit]\n" offset="0:256"/>
      <match type="string" value="\n[Install]\n" offset="0:256"/>
      <match type="string" value="\n[Automount]\n" offset="0:256"/>
      <!-- Note no [Device] section exists (https://www.freedesktop.org/software/systemd/man/systemd.device.html) -->
      <match type="string" value="\n[Mount]\n" offset="0:256"/>
      <match type="string" value="\n[Path]\n" offset="0:256"/>
      <match type="string" value="\n[Scope]\n" offset="0:256"/>
      <match type="string" value="\n[Service]\n" offset="0:256"/>
      <match type="string" value="\n[Slice]\n" offset="0:256"/>
      <match type="string" value="\n[Socket]\n" offset="0:256"/>
      <match type="string" value="\n[Swap]\n" offset="0:256"/>
      <!-- Note no [Target] section exists (https://www.freedesktop.org/software/systemd/man/systemd.target.html) -->
      <match type="string" value="\n[Timer]\n" offset="0:256"/>

      <!-- Matches at the start of the file. -->
      <match type="string" value="[Unit]\n" offset="0"/>
      <match type="string" value="[Install]\n" offset="0"/>
      <match type="string" value="[Automount]\n" offset="0"/>
      <match type="string" value="[Mount]\n" offset="0"/>
      <match type="string" value="[Path]\n" offset="0"/>
      <match type="string" value="[Scope]\n" offset="0"/>
      <match type="string" value="[Service]\n" offset="0"/>

share/mime-info/freedesktop.org.xml  view on Meta::CPAN

    <!-- https://github.com/ostreedev/ostree/blob/master/man/ostree-create-usb.xml -->
    <comment>OSTree software updates</comment>
    <treemagic>
      <treematch path=".ostree" type="directory" non-empty="true" match-case="true" />
      <treematch path="ostree/repo" type="directory" non-empty="true" match-case="true" />
      <treematch path="var/lib/flatpak/repo" type="directory" non-empty="true" match-case="true" />
    </treemagic>
  </mime-type>

  <mime-type type="x-content/software">
    <!-- http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
         http://bugzilla.gnome.org/show_bug.cgi?id=509823#c3 -->
    <comment>Software</comment>
  </mime-type>

  <mime-type type="x-content/unix-software">
    <!-- http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
         http://bugzilla.gnome.org/show_bug.cgi?id=509823#c3 -->
    <comment>UNIX software</comment>
    <sub-class-of type="x-content/software"/>
    <treemagic>
      <treematch path=".autorun" type="file" match-case="true" />
      <treematch path="autorun" type="file" match-case="true" />
      <treematch path="autorun.sh" type="file" match-case="true" />
    </treemagic>
  </mime-type>

  <mime-type type="x-content/win32-software">
    <!-- http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
         http://bugzilla.gnome.org/show_bug.cgi?id=509823#c3 -->
    <comment>Windows software</comment>
    <sub-class-of type="x-content/software"/>
    <treemagic>
      <treematch path="autorun.exe" type="file" executable="true" />
      <treematch path="autorun.inf" type="file" />
    </treemagic>
  </mime-type>

  <mime-type type="application/trig">

t/02-identify.t  view on Meta::CPAN

#!perl -w
use strict;
use Test::More tests => 3;
use MIME::Detect;
use Data::Dumper;

my $mime = MIME::Detect->new(
    database => ['t/freedesktop.org.xml'],
);

my @type = $mime->mime_types($0);

if( !ok 0+@type, "We identify $0 with at least one type" ) {
    SKIP: { skip "Didn't identify $0", 1 };
} else {
    my $type = $type[0];
    is $type->mime_type, "application/x-perl", "It's a Perl program as the highest priority";

t/custom.xml  view on Meta::CPAN

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mime-info [
  <!ELEMENT mime-info (mime-type)+>
  <!ATTLIST mime-info xmlns CDATA #FIXED "http://www.freedesktop.org/standards/shared-mime-info">

  <!ELEMENT mime-type (comment+, (acronym,expanded-acronym)? , (generic-icon? | glob | magic | treemagic | root-XML | alias | sub-class-of)*)>
  <!ATTLIST mime-type type CDATA #REQUIRED>

  <!-- a comment describing a document with the respective MIME type. Example: "WMV video" -->
  <!ELEMENT comment (#PCDATA)>
  <!ATTLIST comment xml:lang CDATA #IMPLIED>

  <!-- a comment describing the respective unexpanded MIME type acronym. Example: "WMV" -->
  <!ELEMENT acronym (#PCDATA)>

t/custom.xml  view on Meta::CPAN

  <!ELEMENT root-XML EMPTY>
  <!ATTLIST root-XML namespaceURI CDATA #REQUIRED>
  <!ATTLIST root-XML localName CDATA #REQUIRED>

  <!ELEMENT alias EMPTY>
  <!ATTLIST alias type CDATA #REQUIRED>

  <!ELEMENT sub-class-of EMPTY>
  <!ATTLIST sub-class-of type CDATA #REQUIRED>
]>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/x-7z-custom">
    <comment>7-zip archive</comment>
    <comment xml:lang="ar">أرشيف 7-zip</comment>
    <comment xml:lang="be@latin">ArchiÅ­ 7-zip</comment>
    <comment xml:lang="bg">Архив — 7-zip</comment>
    <comment xml:lang="ca">arxiu 7-zip</comment>
    <comment xml:lang="cs">archiv 7-zip</comment>
    <comment xml:lang="da">7-zip-arkiv</comment>
    <comment xml:lang="de">7zip-Archiv</comment>
    <comment xml:lang="el">Συμπιεσμένο αρχείο 7-zip</comment>

update.pod  view on Meta::CPAN

Run the following to update the copy of the MIME database
before distributing a new version:

    lwp-mirror https://cgit.freedesktop.org/xdg/shared-mime-info/plain/freedesktop.org.xml.in

# nasty, I know



( run in 1.498 second using v1.01-cache-2.11-cpan-299005ec8e3 )