gitprep / templates / archive.html.ep /
Newer Older
80 lines | 1.661kb
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
cleanup tags
Yuki Kimoto authored on 2016-04-16
29
  my $type = $git->object_type(app->rep_info($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->(
remove cmd_rep
Yuki Kimoto authored on 2016-04-16
43
    $git->cmd(
44
      app->rep_info($user, $project),
cleanup
Yuki Kimoto authored on 2013-03-19
45
      'archive',
46
      "--format=$format",
47
      "--prefix=$name/",
48
      $rev
49
    )
50
  );
51
  if ($archive_type eq 'tar') {
52
    $cmd .= ' | ' . $quote->('gzip', '-n');
53
  }
54
  $file =~ s/(["\\])/\\$1/g;
55

            
cleanup
Yuki Kimoto authored on 2013-05-13
56
  my $success = open my $fh, '-|', $cmd;
57
  
58
  unless ($success) {
59
    $self->render_exeption;
60
    return;
61
  }
cleanup
Yuki Kimoto authored on 2013-03-19
62
  
63
  # Write chunk
64
  $self->res->headers->content_type($content_type);
65
  $self->res->headers->content_disposition(qq/attachment; filename="$file"/);
66
  my $cb;
67
  $cb = sub {
68
    my $c = shift;
69
    my $size = 500 * 1024;
70
    my $length = sysread($fh, my $buffer, $size);
71
    unless ($length) {
72
      close $fh;
73
      undef $cb;
74
      $c->finish;
75
      return;
76
    }
77
    $c->write_chunk($buffer, $cb);
78
  };
79
  $self->$cb;
80
%>