@matheusbsbs / matheus-bagliano / commits / 2d00737489
Fork 0

Support for the standard mime.types map in gitweb

gitweb will try to look up the filename mimetype in /etc/mime.types and optionally a user-configured mime.types map as well. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed Jun 18, 2006 at 00:01 UTC 2d00737489b8c61ed616b261c7c9bd314e2b0b41
1 file changed +44
gitweb/gitweb.cgi
+44
@@ -49,6 +49,11 @@ my $projects_list = "index/index.aux";
49 my $default_blob_plain_mimetype = 'text/plain';
50 my $default_text_plain_charset = undef;
51
52 +# file to use for guessing MIME types before trying /etc/mime.types
53 +# (relative to the current git repository)
54 +my $mimetypes_file = undef;
55 +
56 +
57 # input validation and dispatch
58 my $action = $cgi->param('a');
59 if (defined $action) {
@@ -1486,6 +1491,40 @@ sub git_blob {
1491 git_footer_html();
1492 }
1493
1494 +sub mimetype_guess_file {
1495 + my $filename = shift;
1496 + my $mimemap = shift;
1497 + -r $mimemap or return undef;
1498 +
1499 + my %mimemap;
1500 + open(MIME, $mimemap) or return undef;
1501 + while (<MIME>) {
1502 + my ($mime, $exts) = split(/\t+/);
1503 + my @exts = split(/\s+/, $exts);
1504 + foreach my $ext (@exts) {
1505 + $mimemap{$ext} = $mime;
1506 + }
1507 + }
1508 + close(MIME);
1509 +
1510 + $filename =~ /\.(.*?)$/;
1511 + return $mimemap{$1};
1512 +}
1513 +
1514 +sub mimetype_guess {
1515 + my $filename = shift;
1516 + my $mime;
1517 + $filename =~ /\./ or return undef;
1518 +
1519 + if ($mimetypes_file) {
1520 + my $file = $mimetypes_file;
1521 + $file =~ m#^/# or $file = "$projectroot/$path/$file";
1522 + $mime = mimetype_guess_file($filename, $file);
1523 + }
1524 + $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1525 + return $mime;
1526 +}
1527 +
1528 sub git_blob_plain_mimetype {
1529 my $fd = shift;
1530 my $filename = shift;
@@ -1493,6 +1532,11 @@ sub git_blob_plain_mimetype {
1532 # just in case
1533 return $default_blob_plain_mimetype unless $fd;
1534
1535 + if ($filename) {
1536 + my $mime = mimetype_guess($filename);
1537 + $mime and return $mime;
1538 + }
1539 +
1540 if (-T $fd) {
1541 return 'text/plain' .
1542 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');