Jifty/Web/FileUpload.pm

filter: Statementstmt 27.4% 14 / 51 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 36.3% 4 / 11 Podpod 100% 4 / 4 totaltotal 27.5% 22 / 80 CC 8 SCAR 24.6
CC
8
Cov
22%
new
30.0
content
12.0
new_from_plack
6.0
CRAP 11.7
« Jifty/Plugin/CompressedCSSandJS.pm ↑ Summary Jifty/Plugin/Halo.pm »
1 package Jifty::Web::FileUpload;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw/Jifty::Object Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 __PACKAGE__->mk_accessors(qw(filehandle _content filename content_type));
7  
8 1 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=1.2
9 0 ▶ q{""} => sub { $_[0]->filename },
Subroutine __ANON__: not called CC=1 SCAR=6.9
10 0 ▶ '*{}' => sub { $_[0]->filehandle },
Subroutine __ANON__: not called CC=1 SCAR=6.9
11 0 ▶ '${}' => sub { \($_[0]->filename) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 1 fallback => 1,
13 1 );
14  
15 =head1 NAME
16  
17 Jifty::Web::FileUpload - Describes an HTTP file upload
18  
19 =head1 DESCRIPTION
20  
21 Currently this module is very much geared towards the use case of the current
22 request offering a file upload, and inspecting L<CGI> to produce metadata.
23  
24 Refactorings to eliminate these assumptions are very welcome.
25  
26 =head2 new PARAMHASH
27  
28 Creates a new file upload object. The possible arguments in the C<PARAMHASH>
29 are:
30  
31 =over
32  
33 =item filehandle
34  
35 The filehandle to read the content from. If this is not an L<Fh> object
36 produced by L<CGI>, then C<content_type> is mandatory and you probably want to
37 set C<filename> yourself.
38  
39 =item content (optional)
40  
41 The upload's content. Will be intuited if possible.
42  
43 =item filename (optional)
44  
45 The upload's filename as given by the client (i.e. E<not> on disk).
46 Will be intuited if possible.
47  
48 =item content_type (optional)
49  
50 The content type as reported by the client.
51  
52 =back
53  
54 =cut
55  
56 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
57 0 my $class = shift;
58 0 my $self = bless {}, $class;
59  
60 0 my %args = (
61 filehandle => undef,
62 content => undef,
63 filename => undef,
64 content_type => undef,
65 @_,
66 );
67  
68 0 my $fh = $args{filehandle};
69  
70 0 ▶ if (!defined($args{filename})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"filename"}) 0 0 0
71 0 $args{filename} = "$fh";
72  
73 # Strip all but the basename for consistency between browsers
74 0 $args{filename} =~ s#^.*[\\/]##;
75 }
76  
77 0 ▶ if (!defined($args{content_type})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"content_type"}) 0 0 0
78 0 ▶ ref($fh) eq 'Fh'
Branch: 1 branch
BranchTrueFalseTotal
ref $fh eq "Fh" or die "The filehandle must be an Fh object produced by CGI" 0 0 0
79 or die "The filehandle must be an Fh object produced by CGI";
80  
81 0 my $info = Jifty->handler->cgi->uploadInfo($fh);
82 0 ▶ $args{content_type} = $info->{'Content-Type'}
Branch: 1 branch
BranchTrueFalseTotal
if defined $info 0 0 0
83 if defined $info;
84 }
85  
86 0 $self->filehandle($fh);
87 0 $self->content($args{content});
88 0 $self->filename($args{filename});
89 0 $self->content_type($args{content_type});
90 0 return $self;
91 }
92  
93 =head2 content
94  
95 Lazily slurps in the filehandle's content.
96  
97 =cut
98  
99 0 ▶sub content {
Subroutine content: not called CC=3 SCAR=24.8
Pod documented
100 0 my $self = shift;
101 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
102 0 return $self->_content(@_);
103 }
104  
105 0 my $content = $self->_content;
106 0 ▶ if (!defined($content)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 0 0
107 0 my $fh = $self->filehandle;;
108 0 local $/;
109 0 $content = <$fh>;
110 0 $self->_content($content);
111 }
112  
113 0 return $content;
114 }
115  
116 =head2 new_from_fh Fh
117  
118 Convenience method, since the other bits can be gleaned from the L<Fh> object.
119  
120 =cut
121  
122 # DEPRECATED
123 0 ▶sub new_from_fh {
Subroutine new_from_fh: not called CC=1 SCAR=6.9
Pod documented
124 0 my $self = shift;
125 0 my $fh = shift;
126  
127 0 $self->new(filehandle => $fh);
128 }
129  
130 =head2 new_from_plack $u
131  
132 =cut
133  
134 0 ▶sub new_from_plack {
Subroutine new_from_plack: not called CC=2 SCAR=17.9
Pod documented
135 0 my $self = shift;
136 0 my $u = shift;
137  
138 0 ▶ open my $fh, '<:raw', $u->tempname
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $u->tempname or die "Can't open '@{[$u->tempname];}': '$!'" 0 0 0
139 0 or die "Can't open '@{[ $u->tempname ]}': '$!'";
140  
141 0 $self->new(filehandle => $fh,
142 filename => $u->basename,
143 content_type => $u->type,
144 );
145 }
146  
147 1 1;
148  
« Jifty/Plugin/CompressedCSSandJS.pm ↑ Summary Jifty/Plugin/Halo.pm »