From: Dan Brook Date: Sat, 10 Apr 2010 10:13:35 +0000 (+0100) Subject: Implemented necessary functionality so binary files no longer render literally. X-Git-Tag: 0.000006~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e172b6b86d415fbe6137dd8ca4ef7dc6919f61ba;p=catagits%2FGitalist.git Implemented necessary functionality so binary files no longer render literally. * Added deps for File::Type and File::Type::WebImages. * Haven't been able to test binary non-image files as the presence of one (I tried a .zip) breaks Git::PurePerl on win32 at least. --- diff --git a/Makefile.PL b/Makefile.PL index d733e1c..6abc413 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -83,6 +83,8 @@ requires 'CGI'; requires 'DateTime'; requires 'DateTime::Format::Mail'; requires 'File::Copy::Recursive'; +requires 'File::Type'; +requires 'File::Type::WebImages'; requires 'File::Which'; requires 'HTML::Entities'; requires 'IPC::Run'; diff --git a/lib/Gitalist/Controller/Fragment/Ref.pm b/lib/Gitalist/Controller/Fragment/Ref.pm index e537896..2917616 100644 --- a/lib/Gitalist/Controller/Fragment/Ref.pm +++ b/lib/Gitalist/Controller/Fragment/Ref.pm @@ -8,6 +8,8 @@ with qw/ Gitalist::URIStructure::Fragment::WithLog /; +use File::Type::WebImages (); + sub base : Chained('/fragment/repository/find') PathPart('') CaptureArgs(0) {} sub _diff { @@ -82,7 +84,9 @@ after blob => sub { my ( $self, $c ) = @_; $c->stash( # XXX Hack hack hack, see View::SyntaxHighlight - language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''), + language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''), + is_image => File::Type::WebImages::mime_type($c->stash->{blob}), + is_binary => -B $c->stash->{blob}, ); $c->forward('View::SyntaxHighlight') diff --git a/lib/Gitalist/Controller/Ref.pm b/lib/Gitalist/Controller/Ref.pm index e43bd98..c0e4ce3 100644 --- a/lib/Gitalist/Controller/Ref.pm +++ b/lib/Gitalist/Controller/Ref.pm @@ -6,6 +6,9 @@ use namespace::autoclean; BEGIN { extends 'Gitalist::Controller' } with 'Gitalist::URIStructure::Ref'; +use File::Type; +use File::Type::WebImages (); + sub base : Chained('/repository/find') PathPart('') CaptureArgs(0) {} after commit => sub { @@ -20,7 +23,16 @@ sub raw : Chained('find') Does('FilenameArgs') Args() { my ($self, $c) = @_; $c->forward('find_blob'); - $c->response->content_type('text/plain; charset=utf-8'); + if(-T $c->stash->{blob}) { + $c->response->content_type('text/plain; charset=utf-8'); + } else { + my $ft = File::Type->new(); + $c->response->content_type( + File::Type::WebImages::mime_type($c->stash->{blob}) + || File::Type->new->mime_type($c->stash->{blob}) + ); + } + $c->response->body(delete $c->stash->{blob}); } diff --git a/root/fragment/ref/blob.tt2 b/root/fragment/ref/blob.tt2 index 592f8c5..aaaa65d 100644 --- a/root/fragment/ref/blob.tt2 +++ b/root/fragment/ref/blob.tt2 @@ -1 +1,7 @@ -[% blob %] +[%- IF is_image -%] +
+[%- ELSIF is_binary -%] +
This is a binary file which won't render natively on the web, but you can get it here all the same: [% filename %]
+[%- ELSE -%] +
[% blob | html %]
+[%- END -%] diff --git a/root/ref/blob.tt2 b/root/ref/blob.tt2 index ad81803..fb4497d 100755 --- a/root/ref/blob.tt2 +++ b/root/ref/blob.tt2 @@ -7,8 +7,8 @@ [% short_cmt(head.comment) %] [% END %] - -
[% subinclude('/fragment/ref/blob', c.req.captures, c.req.args.to_path) | html %]
+ +[% subinclude('/fragment/ref/blob', c.req.captures, c.req.args.to_path) %] \ No newline at end of file diff --git a/root/static/css/core.css b/root/static/css/core.css index 001b995..d4220de 100755 --- a/root/static/css/core.css +++ b/root/static/css/core.css @@ -467,6 +467,10 @@ pre.blob { overflow:auto; font-size:12px; } +div.blob { + text-align: center; + margin: 30px; +} /* /blobdiff etc */