view release on metacpan or search on metacpan
- Follow the current mime-apps-spec (fixes #8, #20)
The current version of the mime-apps spec locates the per-user defaults
file in `$XDG_CONFIG_HOME/mimeapps.list`. Use that location, and fall
back to the per-system and distribution defaults as specified, with the
previous legacy defaults file as a final fallback.
Fix by Patrick Burroughs (Celti)
- POD clarifications by Nitish Bezzala
0.27 2015-02-23
- Allow to use mimeinfo on a Path::Tiny object. Reported by Smylers.
- Misspelled NoDisplay attribute in .desktop file.
Fix by Bernhard Rosenkraenzer (berolinux)
- Fix typos in README by Sean Smith (ncstang) as part of CPAN Pull Request
Challenge February 2015.
0.26 2014-05-11
- Fix mimeinfo not adhering to the priorization of globs -
kudos Stefan Seifert.
- Fixed URL in POD. Only run POD 404 tests with extended testing.
0.25 2014-04-03
- Included new test file in MANIFEST.
0.24 2014-04-02
- Fixed freedesktop.org desktop-file-utils URL - David Steinbrunner.
0.23 2014-04-02
- Fixed opening of files with '+' in mime type using
File::MimeInfo::Applications.
Debian bug 690334, brian m. carlson.
0.22 2014-02-27
- Fixed Double-close on a filehandle issue (RT 93221) - Christian Ludwig.
- Fixed cpantesters issue with old version of CPAN::Changes
- Added a "no warnings" in the default routine to suppress warnings when
input is latin2 (thus neither ascii or utf8). Not sure whether this
really fixes the problem but it at least ignores it.
The problem was reported by Daniel Raska.
0.8 2003-10-22
Seems that the new version of the mime-info spec takes longer then I expected,
so I decided to release last weeks bug fixes.
- Fine tuned the method interface for describe()
- Made mimetype -d -l .. default to english for missing translations
- updated URL's for freedesktop
- Fixed small bug in Magic.pm causing a lot of warnings on initialisation when
using 'perl -w', reported by Steve Barton
0.7 2003-10-03
- Added File::MimeInfo::Rox
- ++'ing version number to keep CPAN satisfied
0.6 2003-09-04
- Emergency release because of a bug in the magic test file
0.5 2003-09-03
- added tests for magic typing
- fixed design mistake in the basedir file search all xdg data dirs are now
used, not only the first one found
- put basedir code in a separate package called File::BaseDir
- disabled utf8 binmode layer for perl versions prior to 5.8.0
0.4 2003-08-27
- Added the --dir switch.
- Added File::MimeInfo::Magic that inherits from File::MimeInfo but also does
use the freedesktop magic file
- Shifted some code around in File::MimeInfo to make inheriting easier
- Made the mimetype script work with Magic by default
- Added the --debug switch to trace why a certain file is typed the way it is
- Updated and reviewed the documentation
0.3 2003-08-24
- Moved file test operator stuff to a subroutine called inodetype()
- Actually implemented the --file-compat switch
- Added the -L and -i commandline switches for file(1) compatibility
- Fixed a few bugs with relative file names
- Changed the script 'mimetype' to 'mimeinfo'
- added some commandline options to make this script a little file(1) compat
- added some file test operators for determining types from the inode/* class.
- use utf8 binmode for filehandles
0.2 2003-07-12
- bug fix for OO interface, forget to test the constructor :S
- added specified default behaviour
0.1 2003-07-11
- original version; created by h2xs 1.22 with options -AX File::MMagic::Freedesktop
- implemented finding file type by glob folowing the shared-mime-info-spec version 0.11
- decided to rename it File::MimeInfo because it is less cryptic
t/02_magic.t
t/03_rox.t
t/04_IO_objects.t
t/05_more.t
t/06_pod_ok.t
t/07_pod_cover.t
t/08_changes.t
t/09_no404s.t
t/10filehandle.t
t/11mimeinfo.t
t/applications/foo.desktop
t/applications/mimeinfo.cache
t/applications/mirage.desktop
t/default/binary_file
t/default/empty_file
t/default/encoding_breakage
t/default/plain_text
t/default/utf8_text
t/magic/application_msword
t/magic/application_octet-stream
t/magic/application_vnd.corel-draw
t/magic/application_x-executable
t/magic/application_x-perl
# File-MimeInfo
This module can be used to determine the mime type of a file; it's a
replacement for [File::MMagic](https://metacpan.org/pod/File::MMagic)
trying to implement the freedesktop specification for using the shared
mime-info database. The package comes with a script called `mimetype`
that can be used as a `file(1)` work-alike.
Because this module uses the FreeDesktop MIME database, it's most
suited for use in perl scripts that run on a desktop OS; in fact,
this module is typically pre-installed on Debian and Ubuntu OSes
so you can use its command line script `mimeopen` to open files in
the GUI just like you can use `open` on macOS.
## INSTALLATION
To install this module type the following:
perl Makefile.PL
make
make test
make install
## DEPENDENCIES
This module expects the freedesktop mime database to be installed,
some linux distributions include it, otherwise it can obtained
from:
http://freedesktop.org/Software/shared-mime-info
This module requires these other modules which can be obtained from
the [CPAN](https://metacpan.org) if they are not already installed on
your system:
* Carp
* Exporter
* Fcntl
* Pod::Usage
* File::Basename
lib/File/MimeInfo.pm view on Meta::CPAN
sub rehash {
(@globs, %literal, %extension, %mime2ext) = (); # clear all data
local $_; # limit scope of $_ ... :S
my @globfiles = @DIRS
? ( grep {-e $_ && -r $_} map "$_/globs", @DIRS )
: ( reverse data_files('mime/globs') );
if (@globfiles) {
$_has_mimeinfo_database = 1;
} else {
carp "WARNING: You don't seem to have a mime-info database. " .
"The shared-mime-info package is available from http://freedesktop.org/";
}
my @done;
for my $file (@globfiles) {
next if grep {$file eq $_} @done;
_hash_globs($file);
push @done, $file;
}
$_hashed = 1;
}
lib/File/MimeInfo.pm view on Meta::CPAN
=head1 SYNOPSIS
use File::MimeInfo;
my $mime_type = mimetype($file);
my $mime_type2 = mimetype('test.png');
=head1 DESCRIPTION
This module can be used to determine the mime type of a file. It
tries to implement the freedesktop specification for a shared
MIME database.
For this module shared-mime-info-spec 0.13 was used.
This package only uses the globs file. No real magic checking is
used. The L<File::MimeInfo::Magic> package is provided for magic typing.
If you want to determine the mimetype of data in a memory buffer you should
use L<File::MimeInfo::Magic> in combination with L<IO::Scalar>.
lib/File/MimeInfo.pm view on Meta::CPAN
=item C<rehash_subclasses()>
Rehashes the F<mime/subclasses> files.
=back
=head1 DIAGNOSTICS
This module throws an exception when it can't find any data files, when it can't
open a data file it found for reading or when a subroutine doesn't get enough arguments.
In the first case you either don't have the freedesktop mime info database installed,
or your environment variables point to the wrong places,
in the second case you have the database installed, but it is broken
(the mime info database should logically be world readable).
=head1 TODO
Make an option for using some caching mechanism to reduce init time.
Make C<describe()> use real xml parsing ?
lib/File/MimeInfo.pm view on Meta::CPAN
Perl versions prior to 5.8.0 do not have the ':utf8' IO Layer, thus
for the default method and for reading the xml files
utf8 is not supported for these versions.
Since it is not possible to distinguish between encoding types (utf8, latin1, latin2 etc.)
in a straightforward manner only utf8 is supported (because the spec recommends this).
This module does not yet check extended attributes for a mimetype.
Patches for this are very welcome.
This module uses the FreeDesktop.org shared mime info database. On your desktop
linux this is typically pre-installed so it's not a problem. On your server
you can install the shared-mime-info package via apt or dnf or apk or whatnot.
To install on macOS, you can install it like this:
brew install shared-mime-info
=head1 AUTHOR
Jaap Karssenberg E<lt>pardus@cpan.orgE<gt>
lib/File/MimeInfo.pm view on Meta::CPAN
L<File::MimeInfo::Magic>,
L<File::MimeInfo::Applications>,
L<File::MimeInfo::Rox>
=over 4
=item related CPAN modules
L<File::MMagic>
=item freedesktop specifications used
L<http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec>,
L<http://www.freedesktop.org/wiki/Specifications/basedir-spec>,
L<http://www.freedesktop.org/wiki/Specifications/desktop-entry-spec>
=item freedesktop mime database
L<http://www.freedesktop.org/wiki/Software/shared-mime-info>
=back
=cut
lib/File/MimeInfo/Applications.pm view on Meta::CPAN
our $VERSION = '0.35';
our @ISA = qw(Exporter);
our @EXPORT = qw(
mime_applications mime_applications_all
mime_applications_set_default mime_applications_set_custom
);
print STDERR << 'EOT' unless data_files(qw/applications mimeinfo.cache/);
WARNING: You don't seem to have any mimeinfo.cache files.
Try running the update-desktop-database command. If you
don't have this command you should install the
desktop-file-utils package. This package is available from
http://freedesktop.org/wiki/Software/desktop-file-utils/
EOT
sub mime_applications {
croak "usage: mime_applications(MIMETYPE)" unless @_ == 1;
my $mime = mimetype_canon(shift @_);
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
return wantarray ? (_default($mime), _others($mime)) : _default($mime);
}
sub mime_applications_all {
croak "usage: mime_applications(MIMETYPE)" unless @_ == 1;
my $mime = shift;
return mime_applications($mime),
grep defined($_), map mime_applications($_), mimetype_isa($mime);
}
sub mime_applications_set_default {
croak "usage: mime_applications_set_default(MIMETYPE, APPLICATION)"
unless @_ == 2;
my ($mimetype, $desktop_file) = @_;
(undef, undef, $desktop_file) =
File::Spec->splitpath($desktop_file->{file})
if ref $desktop_file;
croak "missing desktop entry filename for application"
unless length $desktop_file;
$desktop_file .= '.desktop' unless $desktop_file =~ /\.desktop$/;
_write_list($mimetype, $desktop_file);
}
sub mime_applications_set_custom {
croak "usage: mime_applications_set_custom(MIMETYPE, COMMAND)"
unless @_ == 2;
my ($mimetype, $command) = @_;
$command =~ /(\w+)/;
my $word = $1 or croak "COMMAND does not contain a word !?";
# Algorithm to generate name copied from other implementations
my $i = 1;
my $desktop_file =
data_home('applications', $word.'-usercreated-'.$i.'.desktop');
while (-e $desktop_file) {
$i++;
$desktop_file =
data_home('applications', $word.'-usercreated-'.$i.'.desktop');
}
my $object = File::DesktopEntry->new();
$object->set(
Type => 'Application',
Name => $word,
NoDisplay => 'true',
Exec => $command,
);
my (undef, undef, $df) = File::Spec->splitpath($desktop_file);
_write_list($mimetype, $df); # creates dir if needed
$object->write($desktop_file);
return $object;
}
sub _default {
my $mimetype = shift;
my $user = config_home(qw/mimeapps.list/);
my $system = config_dirs(qw/mimeapps.list/);
my $deprecated = data_home(qw/applications mimeapps.list/);
my $distro = data_dirs(qw/applications mimeapps.list/);
lib/File/MimeInfo/Applications.pm view on Meta::CPAN
|| ($deprecated && -f $deprecated)
|| ($distro && -f $distro)
|| ($legacy && -f $legacy) )
&& -r _ ) {
return undef;
}
$Carp::CarpLevel++;
my @paths = grep defined, ($mimetype, $user, $system, $deprecated, $distro, $legacy);
my @list = _read_list(@paths);
my $desktop_file = _find_file(reverse @list);
$Carp::CarpLevel--;
return $desktop_file;
}
sub _others {
my $mimetype = shift;
$Carp::CarpLevel++;
my (@list, @done);
for my $dir (data_dirs('applications')) {
my $cache = File::Spec->catfile($dir, 'mimeinfo.cache');
next if grep {$_ eq $cache} @done;
lib/File/MimeInfo/Applications.pm view on Meta::CPAN
my $file = File::Spec->catfile($dir, $_);
next unless -f $file and -r _;
push @list, File::DesktopEntry->new($file);
}
}
$Carp::CarpLevel--;
return @list;
}
sub _read_list { # read list with "mime/type=foo.desktop;bar.desktop" format
my $mimetype = shift;
my @list;
my $succeeded;
for my $file (@_) {
if (open LIST, '<', $file) {
$succeeded = 1;
while (<LIST>) {
/^\Q$mimetype\E=(.*)$/ or next;
lib/File/MimeInfo/Applications.pm view on Meta::CPAN
}
unless ($succeeded) {
croak "Could not read any defaults, tried:\n" . join("\t\n", @_);
}
return @list;
}
sub _write_list {
my ($mimetype, $desktop_file) = @_;
my $file = config_home(qw/mimeapps.list/);
my $text;
if (-f $file) {
open LIST, '<', $file or croak "Could not read file: $file";
while (<LIST>) {
$text .= $_ unless /^\Q$mimetype\E=/;
}
close LIST;
$text =~ s/[\n\r]?$/\n/; # just to be sure
}
else {
_mkdir($file);
$text = "[Default Applications]\n";
}
open LIST, '>', $file or croak "Could not write file: $file";
print LIST $text;
print LIST "$mimetype=$desktop_file;\n";
close LIST or croak "Could not write file: $file";
}
sub _find_file {
my @list = @_;
for (@list) {
my $file = data_files('applications', $_);
return File::DesktopEntry->new($file) if $file;
}
return undef;
lib/File/MimeInfo/Applications.pm view on Meta::CPAN
}
else {
# prompt user with choice from @others
# ...
}
=head1 DESCRIPTION
This module tries to find applications that can open files
with a certain mimetype. This is done in the way suggested by
the freedesktop Desktop Entry specification. This module is
intended to be compatible with file managers and other applications that
implement this specification.
This module depends on L<File::DesktopEntry> being installed.
To use this module effectively you need to have the desktop-file-utils
package from freedesktop and run update-desktop-database after installing
new .desktop files.
See L<http://www.freedesktop.org/wiki/Software/desktop-file-utils>.
At the moment of writing this module is compatible with the way Nautilus (Gnome)
and with Thunar (XFCE) handle applications for mimetypes. I understand KDE
is still working on implementing the freedesktop mime specifications but will
follow. At the very least all perl applications using this module are using
the same defaults.
=head1 EXPORT
All methods are exported by default.
=head1 METHODS
=over 4
lib/File/MimeInfo/Applications.pm view on Meta::CPAN
can open mimetypes from which MIMETYPE inherits. Parent mimetypes tell
something about the data format, all code inherits from text/plain for example.
=item C<mime_applications_set_default(MIMETYPE, APPLICATION)>
Save a default application for this mimetype. This action will
affect other applications using the same mechanism to find a default
application.
APPLICATION can either be a File::DesktopEntry object or
the basename of a .desktop file.
=item C<mime_applications_set_custom(MIMETYPE, COMMAND)>
Save a custom shell command as default application.
Generates a DesktopEntry file on the fly and calls
C<mime_applications_set_custom>.
Returns the DesktopEntry object.
No checks are done at all on COMMAND.
It should however contain at least one word.
lib/File/MimeInfo/Applications.pm view on Meta::CPAN
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 SEE ALSO
L<File::DesktopEntry>,
L<File::MimeInfo>,
L<File::MimeInfo::Magic>,
L<File::BaseDir>
L<http://freedesktop.org/wiki/Software/desktop-file-utils/>
L<http://freedesktop.org/wiki/Specifications/mime-apps-spec/>
=cut
lib/File/MimeInfo/Magic.pm view on Meta::CPAN
File::MimeInfo::Magic - Determine file type with magic
=head1 SYNOPSIS
use File::MimeInfo::Magic;
my $mime_type = mimetype($file);
=head1 DESCRIPTION
This module inherits from L<File::MimeInfo>, it is transparent
to its functions but adds support for the freedesktop magic file.
Magic data is hashed when you need it for the first time.
If you want to force hashing earlier use the C<rehash()> function.
=head1 EXPORT
The method C<mimetype> is exported by default. The methods C<magic>,
C<inodetype>, C<globs> and C<describe> can be exported on demand.
=head1 METHODS
sub choose {
my ($mime, $set_default, @app) = @_;
print $set_default ?
"Please choose a default application for files of type $mime\n\n" :
"Please choose an application\n\n" ;
my @done;
for my $i (0 .. $#app) {
my (undef, undef, $file) =
File::Spec->splitpath( $app[$i]->{file} );
$file =~ s/\.desktop$//;
if (grep {$_ eq $file} @done) {
$app[$i] = undef;
}
else {
push @done, $file;
print "\t", scalar(@done), ") ",
$app[$i]->get_value('Name'), " ($file)\n";
}
}
@app = grep defined($_), @app;
mimeopen - Open files by mimetype
=head1 SYNOPSIS
mimeopen [options] [-] files
=head1 DESCRIPTION
This script tries to determine the mimetype of a file and open it with the
default desktop application. If no default application is configured the
user is prompted with an "open with" menu in the terminal.
To use this script you need the freedesktop mime-info database and the
freedesktop desktop-file-utils package. See L<File::MimeInfo::Applications(3)>
for more details.
=head1 OPTIONS
=over 4
=item B<-a>, B<--ask>
Do not execute the default application but ask which application to run.
This does not change the default application.
modify it under the same terms as Perl.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=head1 SEE ALSO
L<mimetype(1)>,
L<update-mime-database(1)>,
L<update-desktop-database(1)>,
L<File::MimeInfo(3)>,
L<File::MimeInfo::Applications(3)>
These variables can list base directories to search for data files. The shared
mime-info will be expected in the "mime" sub directory of one of these
directories. If these are not set, there will be searched for the
following directories:
$HOME/.local/share/mime
/usr/local/share/mime
/usr/share/mime
See also the "XDG Base Directory Specification"
L<http://freedesktop.org/Standards/basedir-spec>
=back
=head1 FILES
The base dir for all data files is determined by two environment variables,
see L</ENVIRONMENT>.
=over 4
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=head1 SEE ALSO
L<file(1)>,
L<update-mime-database(1)>,
L<File::MimeInfo(3)>,
L<http://freedesktop.org/Software/shared-mime-info>
t/05_more.t view on Meta::CPAN
## Tests for Applications
SKIP: {
eval { require File::DesktopEntry };
skip "File::DesktopEntry not installed", 3 if $@;
use_ok('File::MimeInfo::Applications');
my %list = (
'text/plain' => 'foo.desktop',
'image/svg+xml' => 'mirage.desktop',
);
for my $type (keys %list) {
my ($default, @other) = mime_applications($type);
ok (
!defined($default) &&
(@other == 1) &&
ref($other[0]) eq 'File::DesktopEntry',
'mime_application() works'
);
is (
$other[0]->{file},
File::Spec->catfile('t', 'applications', $list{$type}),
"desktop file is the right one",
);
}
}
done_testing;
t/applications/mimeinfo.cache view on Meta::CPAN
[MIME Cache]
text/plain=foo.desktop
image/svg+xml=mirage.desktop
t/magic/text_x-patch view on Meta::CPAN
Index: update-mime-database.c
===================================================================
RCS file: /home/freedesktop/shared-mime-info/update-mime-database.c,v
retrieving revision 1.27
diff -u -r1.27 update-mime-database.c
--- update-mime-database.c 2 Jun 2003 10:25:06 -0000 1.27
+++ update-mime-database.c 27 Aug 2003 14:39:22 -0000
@@ -786,7 +786,7 @@
*/
static void parse_int_value(int bytes, const char *in, const char *in_mask,
GString *parsed_value, char **parsed_mask,
- GError **error)
+ gboolean big_endian, GError **error)
t/mime/text/plain.xml view on Meta::CPAN
<?xml version="1.0" encoding="utf-8"?>
<mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="text/plain">
<!--Created automatically by update-mime-database. DO NOT EDIT!-->
<comment>Plain Text</comment>
<comment xml:lang="id">Teks Murni</comment>
<comment xml:lang="is">Texti</comment>
<comment xml:lang="it">Testo semplice</comment>
<comment xml:lang="ja">ãã¬ã¼ã³ããã¹ã</comment>
<comment xml:lang="ko">ë³´íµ ê¸ì</comment>
<comment xml:lang="lt">Paprastas Tekstas</comment>
<comment xml:lang="lv">AtklÄts Teksts</comment>
<comment xml:lang="mi">Panui Mäori</comment>