cleanup
|
1 |
<% |
2 |
# API |
|
3 |
my $api = $self->gitprep_api; |
|
4 |
|
|
5 |
# Parameter |
|
6 |
my $user = param('user'); |
|
7 |
my $project = param('project'); |
|
8 |
my $rev = param('rev'); |
|
9 |
my $archive_type = stash('archive_type'); |
|
added merged branches to bra...
|
10 |
|
cleanup
|
11 |
my $content_type; |
12 |
my $format; |
|
13 |
my $ext; |
|
14 |
if ($archive_type eq 'tar') { |
|
15 |
$format = 'tar'; |
|
16 |
$ext = 'tar.gz'; |
|
17 |
$content_type = 'application/x-tar'; |
|
18 |
} |
|
19 |
elsif ($archive_type eq 'zip') { |
|
20 |
$format = 'zip'; |
|
21 |
$ext = 'zip'; |
|
22 |
$content_type = 'application/zip'; |
|
23 |
} |
|
24 |
|
|
25 |
# Git |
|
26 |
my $git = app->git; |
|
27 | ||
28 |
# Object type |
|
29 |
my $type = $git->object_type($user, $project, "$rev^{}"); |
|
cleanup
|
30 |
if (!$type || $type eq 'blob') { |
31 |
$self->render_not_found; |
|
32 |
return; |
|
33 |
} |
|
cleanup
|
34 |
|
35 |
my $name = "$project-$rev"; |
|
36 |
my $file = "$name.$ext"; |
|
37 |
my $quote = sub { |
|
38 |
return join(' ', |
|
39 |
map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ |
|
40 |
); |
|
41 |
}; |
|
42 |
my $cmd = $quote->( |
|
rename _cmd method to cmd
|
43 |
$git->cmd( |
cleanup
|
44 |
$user, |
45 |
$project, |
|
46 |
'archive', |
|
47 |
"--format=$format", |
|
48 |
"--prefix=$name/", |
|
49 |
$rev |
|
50 |
) |
|
51 |
); |
|
52 |
if ($archive_type eq 'tar') { |
|
53 |
$cmd .= ' | ' . $quote->('gzip', '-n'); |
|
54 |
} |
|
55 |
$file =~ s/(["\\])/\\$1/g; |
|
56 | ||
cleanup
|
57 |
my $success = open my $fh, '-|', $cmd; |
58 |
|
|
59 |
unless ($success) { |
|
60 |
$self->render_exeption; |
|
61 |
return; |
|
62 |
} |
|
cleanup
|
63 |
|
64 |
# Write chunk |
|
65 |
$self->res->headers->content_type($content_type); |
|
66 |
$self->res->headers->content_disposition(qq/attachment; filename="$file"/); |
|
67 |
my $cb; |
|
68 |
$cb = sub { |
|
69 |
my $c = shift; |
|
70 |
my $size = 500 * 1024; |
|
71 |
my $length = sysread($fh, my $buffer, $size); |
|
72 |
unless ($length) { |
|
73 |
close $fh; |
|
74 |
undef $cb; |
|
75 |
$c->finish; |
|
76 |
return; |
|
77 |
} |
|
78 |
$c->write_chunk($buffer, $cb); |
|
79 |
}; |
|
80 |
$self->$cb; |
|
81 |
%> |