summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ad14e93)
raw | patch | inline | side by side (parent: ad14e93)
author | Petr Baudis <pasky@suse.cz> | |
Sat, 17 Jun 2006 22:01:06 +0000 (00:01 +0200) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Mon, 19 Jun 2006 04:19:14 +0000 (21:19 -0700) |
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>
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>
gitweb/gitweb.cgi | patch | blob | history |
diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi
index 8fde1c8833c87b64030d46cd769c340594bc6d5f..fa90c51a4da437b8fa8bfd159ec7f0cad0596e37 100755 (executable)
--- a/gitweb/gitweb.cgi
+++ b/gitweb/gitweb.cgi
my $default_blob_plain_mimetype = 'text/plain';
my $default_text_plain_charset = undef;
+# file to use for guessing MIME types before trying /etc/mime.types
+# (relative to the current git repository)
+my $mimetypes_file = undef;
+
+
# input validation and dispatch
my $action = $cgi->param('a');
if (defined $action) {
git_footer_html();
}
+sub mimetype_guess_file {
+ my $filename = shift;
+ my $mimemap = shift;
+ -r $mimemap or return undef;
+
+ my %mimemap;
+ open(MIME, $mimemap) or return undef;
+ while (<MIME>) {
+ my ($mime, $exts) = split(/\t+/);
+ my @exts = split(/\s+/, $exts);
+ foreach my $ext (@exts) {
+ $mimemap{$ext} = $mime;
+ }
+ }
+ close(MIME);
+
+ $filename =~ /\.(.*?)$/;
+ return $mimemap{$1};
+}
+
+sub mimetype_guess {
+ my $filename = shift;
+ my $mime;
+ $filename =~ /\./ or return undef;
+
+ if ($mimetypes_file) {
+ my $file = $mimetypes_file;
+ $file =~ m#^/# or $file = "$projectroot/$path/$file";
+ $mime = mimetype_guess_file($filename, $file);
+ }
+ $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
+ return $mime;
+}
+
sub git_blob_plain_mimetype {
my $fd = shift;
my $filename = shift;
# just in case
return $default_blob_plain_mimetype unless $fd;
+ if ($filename) {
+ my $mime = mimetype_guess($filename);
+ $mime and return $mime;
+ }
+
if (-T $fd) {
return 'text/plain' .
($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');