Implemented necessary functionality so binary files no longer render literally.
[catagits/Gitalist.git] / lib / Gitalist / Controller / Ref.pm
CommitLineData
2980657b 1package Gitalist::Controller::Ref;
f6a72048 2
3use Moose;
f6a72048 4use namespace::autoclean;
5
b6ec181b 6BEGIN { extends 'Gitalist::Controller' }
2980657b 7with 'Gitalist::URIStructure::Ref';
f6a72048 8
e172b6b8 9use File::Type;
10use File::Type::WebImages ();
11
f6a72048 12sub base : Chained('/repository/find') PathPart('') CaptureArgs(0) {}
13
ae36e02c 14after commit => sub {
15 my($self, $c) = @_;
16
17 $c->stash->{diff_tree} = ($c->stash->{Repository}->diff(
18 commit => $c->stash->{Commit},
19 ))[0];
20};
21
ca06a177 22sub raw : Chained('find') Does('FilenameArgs') Args() {
b132dce6 23 my ($self, $c) = @_;
ca06a177 24 $c->forward('find_blob');
b132dce6 25
e172b6b8 26 if(-T $c->stash->{blob}) {
27 $c->response->content_type('text/plain; charset=utf-8');
28 } else {
29 my $ft = File::Type->new();
30 $c->response->content_type(
31 File::Type::WebImages::mime_type($c->stash->{blob})
32 || File::Type->new->mime_type($c->stash->{blob})
33 );
34 }
35
b132dce6 36 $c->response->body(delete $c->stash->{blob});
37}
38
39=head2 snapshot
40
41Provides a snapshot of a given commit.
42
43=cut
44
d423d020 45sub snapshot : Chained('find') PathPart('snapshot') Args() {
b132dce6 46 my ($self, $c, $format) = @_;
47 $format ||= 'tgz';
48 my @snap = $c->stash->{Repository}->snapshot(
49 sha1 => $c->stash->{Commit}->sha1,
50 format => $format
51 );
52 $c->response->status(200);
53 $c->response->headers->header( 'Content-Disposition' =>
54 "attachment; filename=$snap[0]");
55 $c->response->body($snap[1]);
56}
57
8fd20a28 58=head2 patch
59
60A raw patch for a given commit.
61
62=cut
63
64sub patch : Chained('find') Args(0) {
65 my ($self, $c) = @_;
66 $c->detach('patches', [1]);
67}
68
69=head2 patches
70
71The patcheset for a given commit ???
72
73=cut
74
75sub patches : Chained('find') Args(1) {
76 my ($self, $c, $count) = @_;
77 $count ||= Gitalist->config->{patches}{max};
78 my $commit = $c->stash->{Commit};
79 my $parent = $c->req->param('hp') || undef; # FIXME
80 my $patch = $commit->get_patch( $parent, $count );
81 $c->response->body($patch);
82 $c->response->content_type('text/plain');
83 $c->response->status(200);
84}
85
f6a72048 86__PACKAGE__->meta->make_immutable;