aboutsummaryrefslogtreecommitdiff
path: root/gitweb
diff options
context:
space:
mode:
authorPetr Baudis <pasky@suse.cz>2006-06-18 00:01:06 +0200
committerJunio C Hamano <junkio@cox.net>2006-06-18 21:19:14 -0700
commit2d00737489b8c61ed616b261c7c9bd314e2b0b41 (patch)
treec62caf0d87fbdd41dc383352f0efb09ea42273ce /gitweb
parentad14e9317513f132293406e570f0b9360704371e (diff)
downloadgit-2d00737489b8c61ed616b261c7c9bd314e2b0b41.tar.gz
git-2d00737489b8c61ed616b261c7c9bd314e2b0b41.tar.xz
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>
Diffstat (limited to 'gitweb')
-rwxr-xr-xgitweb/gitweb.cgi44
1 files changed, 44 insertions, 0 deletions
diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi
index 8fde1c883..fa90c51a4 100755
--- a/gitweb/gitweb.cgi
+++ b/gitweb/gitweb.cgi
@@ -49,6 +49,11 @@ my $projects_list = "index/index.aux";
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) {
@@ -1486,6 +1491,40 @@ sub git_blob {
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;
@@ -1493,6 +1532,11 @@ sub git_blob_plain_mimetype {
# 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 : '');