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.3514_04'; # TRIAL
5             $Dancer::Request::Upload::VERSION = '1.351404';
6 183     183   1521 use File::Spec;
  183         1221  
  183         4869  
7 183     183   794 use Carp;
  183         302  
  183         7254  
8              
9 183     183   874 use strict;
  183         358  
  183         3871  
10 183     183   967 use warnings;
  183         348  
  183         4927  
11 183     183   908 use base 'Dancer::Object';
  183         348  
  183         16257  
12 183     183   1118 use Dancer::FileUtils qw(open_file);
  183         334  
  183         7936  
13 183     183   1058 use Dancer::Exception qw(:all);
  183         351  
  183         82843  
14              
15             Dancer::Request::Upload->attributes(
16             qw(
17             filename tempname headers size
18             )
19             );
20              
21             sub file_handle {
22 11     11 1 24 my ($self) = @_;
23 11 100       36 return $self->{_fh} if defined $self->{_fh};
24 10 50       182 my $fh = open_file('<', $self->tempname)
25             or raise core_request => "Can't open `" . $self->tempname . "' for reading: $!";
26 10         30 $self->{_fh} = $fh;
27             }
28              
29             sub copy_to {
30 1     1 1 8 my ($self, $target) = @_;
31 1         593 require File::Copy;
32 1         2322 File::Copy::copy($self->{tempname}, $target);
33             }
34              
35             sub link_to {
36 1     1 1 1394 my ($self, $target) = @_;
37 1         55 CORE::link($self->{tempname}, $target);
38             }
39              
40             sub content {
41 10     10 1 1402 my ($self, $layer) = @_;
42             return $self->{_content}
43 10 50       41 if defined $self->{_content};
44              
45 10 50       30 $layer = ':raw' unless $layer;
46              
47 10         17 my $content = undef;
48 10         30 my $handle = $self->file_handle;
49              
50 10         56 binmode($handle, $layer);
51              
52 10         59 while ($handle->read(my $buffer, 8192)) {
53 10         491 $content .= $buffer;
54             }
55              
56 10         140 $self->{_content} = $content;
57             }
58              
59             sub basename {
60 1     1 1 623 my ($self) = @_;
61 1         11 require File::Basename;
62 1         6 File::Basename::basename($self->filename);
63             }
64              
65             sub type {
66 1     1 1 4 my $self = shift;
67              
68 1         5 return $self->headers->{'Content-Type'};
69             }
70              
71              
72              
73             # private
74              
75              
76             1;
77              
78             __END__