File Coverage

blib/lib/Dancer2/Core/Request/Upload.pm
Criterion Covered Total %
statement 35 35 100.0
branch 5 6 83.3
condition n/a
subroutine 11 11 100.0
pod 6 6 100.0
total 57 58 98.2


line stmt bran cond sub pod time code
1             package Dancer2::Core::Request::Upload;
2             # ABSTRACT: Class representing file upload requests
3             $Dancer2::Core::Request::Upload::VERSION = '2.1.0';
4 166     166   271746 use Moo;
  166         52678  
  166         1684  
5              
6 166     166   86692 use Carp;
  166         504  
  166         14605  
7 166     166   14291 use Path::Tiny ();
  166         227117  
  166         4484  
8 166     166   94182 use File::Copy ();
  166         661637  
  166         5848  
9              
10 166     166   1981 use Dancer2::Core::Types;
  166         362  
  166         1888  
11              
12             has filename => (
13             is => 'ro',
14             isa => Str,
15             );
16              
17             has tempname => (
18             is => 'ro',
19             isa => Str,
20             );
21              
22             has headers => (
23             is => 'ro',
24             isa => HashRef,
25             );
26              
27             has size => (
28             is => 'ro',
29             isa => Num,
30             );
31              
32             sub file_handle {
33 20     20 1 3825 my ($self) = @_;
34 20 100       75 return $self->{_fh} if defined $self->{_fh};
35 17         158 $self->{_fh} = Path::Tiny::path( $self->tempname )->openr_raw;
36             }
37              
38             sub copy_to {
39 3     3 1 873 my ( $self, $target ) = @_;
40 3         24 File::Copy::copy( $self->tempname, $target );
41             }
42              
43             sub link_to {
44 3     3 1 3332 my ( $self, $target ) = @_;
45 3         356 CORE::link( $self->tempname, $target );
46             }
47              
48             sub content {
49 20     20 1 134 my ( $self, $layer ) = @_;
50             return $self->{_content}
51 20 100       80 if defined $self->{_content};
52              
53 17 50       56 $layer = ':raw' unless $layer;
54              
55 17         36 my $content = undef;
56 17         56 my $handle = $self->file_handle;
57              
58 17         3342 binmode( $handle, $layer );
59              
60 17         125 while ( $handle->read( my $buffer, 8192 ) ) {
61 17         755 $content .= $buffer;
62             }
63              
64 17         298 $self->{_content} = $content;
65             }
66              
67             sub basename {
68 3     3 1 1650 my ($self) = @_;
69 3         20 return Path::Tiny::path( $self->filename )->basename;
70             }
71              
72             sub type {
73 3     3 1 6 my $self = shift;
74 3         23 return $self->headers->{'Content-Type'};
75             }
76              
77             1;
78              
79             __END__