File Coverage

blib/lib/Dancer/Request/Upload.pm
Criterion Covered Total %
statement 44 44 100.0
branch 5 8 62.5
condition n/a
subroutine 13 13 100.0
pod 6 6 100.0
total 68 71 95.7


line stmt bran cond sub pod time code
1             package Dancer::Request::Upload;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: class representing file uploads requests
4             $Dancer::Request::Upload::VERSION = '1.3521';
5 182     182   1267 use File::Spec;
  182         385  
  182         5940  
6 182     182   891 use Carp;
  182         409  
  182         8969  
7              
8 182     182   1080 use strict;
  182         501  
  182         3869  
9 182     182   1016 use warnings;
  182         429  
  182         6914  
10 182     182   1141 use base 'Dancer::Object';
  182         1451  
  182         19459  
11 182     182   1403 use Dancer::FileUtils qw(open_file);
  182         437  
  182         9164  
12 182     182   1261 use Dancer::Exception qw(:all);
  182         471  
  182         98452  
13              
14             Dancer::Request::Upload->attributes(
15             qw(
16             filename tempname headers size
17             )
18             );
19              
20             sub file_handle {
21 11     11 1 21 my ($self) = @_;
22 11 100       28 return $self->{_fh} if defined $self->{_fh};
23 10 50       30 my $fh = open_file('<', $self->tempname)
24             or raise core_request => "Can't open `" . $self->tempname . "' for reading: $!";
25 10         29 $self->{_fh} = $fh;
26             }
27              
28             sub copy_to {
29 1     1 1 11 my ($self, $target) = @_;
30 1         507 require File::Copy;
31 1         2434 File::Copy::copy($self->{tempname}, $target);
32             }
33              
34             sub link_to {
35 1     1 1 698 my ($self, $target) = @_;
36 1         49 CORE::link($self->{tempname}, $target);
37             }
38              
39             sub content {
40 10     10 1 711 my ($self, $layer) = @_;
41             return $self->{_content}
42 10 50       28 if defined $self->{_content};
43              
44 10 50       27 $layer = ':raw' unless $layer;
45              
46 10         15 my $content = undef;
47 10         28 my $handle = $self->file_handle;
48              
49 10         53 binmode($handle, $layer);
50              
51 10         40 while ($handle->read(my $buffer, 8192)) {
52 10         359 $content .= $buffer;
53             }
54              
55 10         142 $self->{_content} = $content;
56             }
57              
58             sub basename {
59 1     1 1 483 my ($self) = @_;
60 1         8 require File::Basename;
61 1         5 File::Basename::basename($self->filename);
62             }
63              
64             sub type {
65 1     1 1 3 my $self = shift;
66              
67 1         4 return $self->headers->{'Content-Type'};
68             }
69              
70              
71              
72             # private
73              
74              
75             1;
76              
77             __END__