summaryrefslogtreecommitdiffstats
path: root/scripts/mimetype
blob: 3c0c17e9b1d3c709051e0ef55eb9a4418acd8c11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/perl
use warnings;
use strict;
use File::MimeInfo::Magic qw(mimetype globs);

exit 1 unless @ARGV == 2;

my $filename = $ARGV[0];
my $file = $ARGV[1];
my $type = "";
my $file_type = "";

exit 1 unless -f $file;

$type = globs($filename);

# globbing takes priority over all other
if (!$type) {
	$type = mimetype($file);
	if ($type eq "application/octet-stream") {
		# application/octet-stream normally means the detection failed
		# use the output of file in this case
		$file_type = `file -b --mime-type $file`;
		chomp $file_type;
		$type = $file_type;
		if ($type eq "text/plain") {
			# detect ascii with color codes
			$file_type = `file -b $file`;
			chomp $file_type;
			if ($file_type eq "ASCII text, with escape sequences") {
				$type = "text/plain-ascii";
			}
		}
	}
}

print "$type\n";