| 1 | package Test2::Harness::UI::Controller::Binary; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Test2::Harness::UI::Response qw/resp error/; | ||
| 8 | |||
| 9 | use parent 'Test2::Harness::UI::Controller'; | ||
| 10 | use Test2::Harness::UI::Util::HashBase; | ||
| 11 | use Test2::Harness::UI::UUID qw/uuid_inflate/; | ||
| 12 | |||
| 13 | sub title { 'Binary' } | ||
| 14 | |||
| 15 | sub handle { | ||
| 16 | my $self = shift; | ||
| 17 | my ($route) = @_; | ||
| 18 | |||
| 19 | my $req = $self->{+REQUEST}; | ||
| 20 | my $res = resp(200); | ||
| 21 | |||
| 22 | die error(404 => 'Missing route') unless $route; | ||
| 23 | my $binary_id = uuid_inflate($route->{binary_id}) or die error(404 => "Invalid Route"); | ||
| 24 | |||
| 25 | die error(404 => 'No id') unless $binary_id; | ||
| 26 | |||
| 27 | my $schema = $self->{+CONFIG}->schema; | ||
| 28 | my $binary = $schema->resultset('Binary')->find({binary_id => $binary_id}); | ||
| 29 | |||
| 30 | die error(404 => 'No such binary file') unless $binary; | ||
| 31 | |||
| 32 | my $filename = $binary->filename; | ||
| 33 | |||
| 34 | $res->content_type('application/x-binary'); | ||
| 35 | |||
| 36 | my $safe_filename = $filename; | ||
| 37 | $safe_filename =~ s/[\r\n]//g; | ||
| 38 | |||
| 39 | if ($binary->is_image) { | ||
| 40 | $res->header('Content-Disposition' => "inline; filename=\"" . $safe_filename . "\""); | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | $res->header('Content-Disposition' => "attachment; filename=\"" . $safe_filename . "\""); | ||
| 44 | } | ||
| 45 | |||
| 46 | $res->body($binary->data); | ||
| 47 | return $res; | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | __END__ |