| 1 | package Test2::Harness::UI::Controller::Download; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Data::GUID; | ||
| 8 | use List::Util qw/max/; | ||
| 9 | use Text::Xslate(qw/mark_raw/); | ||
| 10 | use Test2::Harness::UI::Util qw/share_dir/; | ||
| 11 | use Test2::Harness::UI::Response qw/resp error/; | ||
| 12 | use Test2::Harness::Util::JSON qw/encode_json decode_json/; | ||
| 13 | use Test2::Harness::UI::UUID qw/uuid_inflate/; | ||
| 14 | |||
| 15 | use parent 'Test2::Harness::UI::Controller'; | ||
| 16 | use Test2::Harness::UI::Util::HashBase qw/-title/; | ||
| 17 | |||
| 18 | sub handle { | ||
| 19 | my $self = shift; | ||
| 20 | my ($route) = @_; | ||
| 21 | |||
| 22 | my $req = $self->{+REQUEST}; | ||
| 23 | |||
| 24 | my $res = resp(200); | ||
| 25 | |||
| 26 | my $user = $req->user; | ||
| 27 | |||
| 28 | die error(404 => 'Missing route') unless $route; | ||
| 29 | |||
| 30 | my $run; | ||
| 31 | |||
| 32 | if ($self->{+CONFIG}->single_run) { | ||
| 33 | $run = $user->runs->first or die error(404 => 'Invalid run'); | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | my $it = $route->{id} or die error(404 => 'No id'); | ||
| 37 | $it = uuid_inflate($it) or die error(404 => 'Invalid Run'); | ||
| 38 | my $schema = $self->{+CONFIG}->schema; | ||
| 39 | $run = $schema->resultset('Run')->search({run_id => $it})->first or die error(404 => 'Invalid Run'); | ||
| 40 | } | ||
| 41 | |||
| 42 | my $log = $run->log_file; | ||
| 43 | my $type = $log->name =~ m/\.bz2/ ? 'application/x-bzip2' : 'application/gzip'; | ||
| 44 | |||
| 45 | $res->content_type($type); | ||
| 46 | my $safe_name = $log->name; | ||
| 47 | $safe_name =~ s/[\r\n]//g; | ||
| 48 | $res->header('Content-Disposition' => "attachment; filename=\"" . $safe_name . "\""); | ||
| 49 | $res->body($log->data); | ||
| 50 | return $res; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ |