gitprep / templates / archive.html.ep /
Newer Older
81 lines | 1.637kb
cleanup
Yuki Kimoto authored on 2013-03-19
1
<%
2
  # API
revert encoding support
Yuki Kimoto authored on 2013-11-22
3
  my $api = $self->gitprep_api;
cleanup
Yuki Kimoto authored on 2013-03-19
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...
Yuki Kimoto authored on 2013-05-03
10
  
cleanup
Yuki Kimoto authored on 2013-03-19
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
revert encoding support
Yuki Kimoto authored on 2013-11-22
26
  my $git = app->git;
cleanup
Yuki Kimoto authored on 2013-03-19
27

            
28
  # Object type
29
  my $type = $git->object_type($user, $project, "$rev^{}");
cleanup
Yuki Kimoto authored on 2013-05-13
30
  if (!$type || $type eq 'blob') {
do success xt tests
Yuki Kimoto authored on 2016-03-25
31
    $self->reply->not_found;
cleanup
Yuki Kimoto authored on 2013-05-13
32
    return;
33
  }
cleanup
Yuki Kimoto authored on 2013-03-19
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
Yuki Kimoto authored on 2013-03-27
43
    $git->cmd(
cleanup
Yuki Kimoto authored on 2013-03-19
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
Yuki Kimoto authored on 2013-05-13
57
  my $success = open my $fh, '-|', $cmd;
58
  
59
  unless ($success) {
60
    $self->render_exeption;
61
    return;
62
  }
cleanup
Yuki Kimoto authored on 2013-03-19
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
%>