Neaf/Upload.pm

filter: Statementstmt 93.7% 60 / 64 Branchbran 55.0% 11 / 20 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 79.3% 92 / 116 CC 15 SCAR 16.9
CC
15
Cov
79%
new
11.2
content
4.5
size
3.2
CRAP 5.4
« Neaf/Request.pm ↑ Summary Neaf/X/Form.pm »
1 package MVC::Neaf::Upload;
2  
3 97 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 97 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 MVC::Neaf::Upload - File upload object for Not Even A Framework
9  
10 =head1 METHODS
11  
12 Generally, this class isn't used directly; instead, it's returned by an
13 L<MVC::Neaf::Request> object.
14  
15 =cut
16  
17 97 our $VERSION = '0.2901';
18 97 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 97 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 97 ▶use PerlIO::encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head2 new(%options)
23  
24 %options may include:
25  
26 =over
27  
28 =item * id (required) - the form id by which upload is known.
29  
30 =item * tempfile - file where upload is stored.
31  
32 =item * handle - file handle opened for readin. One of these is required.
33  
34 =item * filename - user-supplied filename. Don't trust this.
35  
36 =item * utf8 - if set, all data read from the file will be utf8-decoded.
37  
38 =back
39  
40 =cut
41  
42 # TODO 0.30 figure out if GLOBs are worth the hassle
43 # We use GLOB objects so that <$upload> works as expected.
44 # This may turn out to be not worth it, so it's not even in the docs yet.
45 # See also t/*diamond*.t
46  
47 97 my %new_opt;
48 97 my @copy_fields = qw(id tempfile filename utf8);
49 97 $new_opt{$_}++ for @copy_fields, "handle";
50 7 ▶sub new {
Subroutine new: called CC=9 SCAR=24.1
Pod documented
51 7 my ($class, %args) = @_;
52  
53 # TODO 0.30 add "unicode" flag to open & slurp in utf8 mode
54  
55 7 my @extra = grep { !$new_opt{$_} } keys %args;
56 7 ▶ croak( "$class->new(): unknown options @extra" )
Branch: 1 branch
BranchTrueFalseTotal
if @extra 0 7 7
57 if @extra;
58 defined $args{id}
59 7 ▶ or croak( "$class->new(): id option is required" );
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"id"} or croak("$class->new(): id option is required") 7 0 7
60  
61 7 my $self;
62 7 ▶ if ($args{tempfile}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'tempfile'}) { } 0 7 7
elsif ($args{'handle'}) { } 7 0 7
63 open $self, "<", $args{tempfile}
64 0 ▶ or croak "$class->new(): Failed to open $args{tempfile}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $self, "<", $args{"tempfile"} or croak(qq[$class->new(): Failed to open $args{"tempfile"}: $!]) 0 0 0
65 } elsif ($args{handle}) {
66 open $self, "<&", $args{handle}
67 7 ▶ or croak "$class->new(): Failed to dup handle $args{handle}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $self, "<&", $args{"handle"} or croak(qq[$class->new(): Failed to dup handle $args{"handle"}: $!]) 7 0 7
68 } else {
69 0 croak( "$class->new(): Either tempfile or handle option required" );
70 };
71  
72 7 ▶ if ($args{utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"utf8"}) 2 5 7
73 2 local $PerlIO::encoding::fallback = Encode::FB_CROAK;
74 2 binmode $self, ":encoding(UTF-8)"
75 };
76 7 bless $self, $class;
77  
78 *$self->{$_} = $args{$_}
79 7 for @copy_fields;
80  
81 7 return $self;
82 };
83  
84 =head2 id()
85  
86 Return upload id.
87  
88 =cut
89  
90 2 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod documented
91 2 my $self = shift;
92 2 return *$self->{id};
93 };
94  
95 =head2 filename()
96  
97 Get user-supplied file name. Don't trust this value.
98  
99 =cut
100  
101 2 ▶sub filename {
Subroutine filename: called CC=2 SCAR=7.1
Pod documented
102 2 my $self = shift;
103  
104 2 ▶ *$self->{filename} = '/dev/null' unless defined *$self->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *$self->{"filename"} 0 2 2
105 2 return *$self->{filename};
106 };
107  
108 =head2 size()
109  
110 Calculate file size.
111  
112 B<CAVEAT> May return 0 if file is a pipe.
113  
114 =cut
115  
116 1 ▶sub size {
Subroutine size: called CC=3 SCAR=11.8
Pod documented
117 1 my $self = shift;
118  
119 1 ▶ return *$self->{size} ||= do {
Condition33%1/3
*$self->{"size"} ||= do { my $fd = $self->handle; my(@stat) = stat $fd; $stat[7] or 0 }
l!l&&r!l&&!r
010
MC/DC0%0/2
*$self->{"size"} ||= do { my $fd = $self->handle; my(@stat) = stat $fd; $stat[7] or 0 }
*$self->{"size"} do { my $fd = $self->handle; my(@stat) = stat $fd; $stat[7] or 0 }
Truth table33%1/3
A ||= B
A= *$self->{"size"}B= do { my $fd = $self->handle; my(@stat) = stat $fd; $stat[7] or 0 }
ABresult
1X1
011
000
120 # calc size
121 1 my $fd = $self->handle;
122 1 my @stat = stat $fd;
123 1 ▶ $stat[7] || 0;
Condition50%1/2
$stat[7] || 0
l!l
10
MC/DC0%0/1
$stat[7] || 0
$stat[7]
Truth table50%1/2
A
A= $stat[7]
Aresult
11
00
124 };
125 };
126  
127 =head2 handle()
128  
129 Return file handle, opening temp file if needed.
130  
131 =cut
132  
133 15 ▶sub handle {
Subroutine handle: called CC=1 SCAR=0.0
Pod documented
134 15 my $self = shift;
135  
136 15 return $self;
137 };
138  
139 =head2 content()
140  
141 Return file content (aka slurp), caching it in memory.
142  
143 B<CAVEAT> May eat up a lot of memory. Be careful...
144  
145 B<NOTE> This breaks file current position, resetting it to the beginning.
146  
147 =cut
148  
149 7 ▶sub content {
Subroutine content: called CC=4 SCAR=15.0
Pod documented
150 7 my $self = shift;
151  
152 # TODO 0.30 remember where the file was 1st time
153 7 ▶ if (!defined *$self->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined *$self->{"content"}) 5 2 7
154 5 $self->rewind;
155 5 my $fd = $self->handle;
156  
157 5 local $/;
158 5 my $content = <$fd>;
159 4 ▶ if (!defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 4 4
160 0 ▶ my $fname = *$self->{tempfile} || $fd;
Condition0%0/3
*$self->{'tempfile'} || $fd
l!l&&r!l&&!r
000
MC/DC0%0/2
*$self->{'tempfile'} || $fd
*$self->{'tempfile'} $fd
Truth table0%0/3
A || B
A= *$self->{'tempfile'}B= $fd
ABresult
1X1
011
000
161 0 croak( "Upload *$self->{id}: failed to read file $fname: $!");
162 };
163  
164 4 $self->rewind;
165 4 *$self->{content} = $content;
166 };
167  
168 6 return *$self->{content};
169 };
170  
171 =head2 rewind()
172  
173 Reset the file to the beginning. Will fail silently on pipes.
174  
175 Returns self.
176  
177 =cut
178  
179 9 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
180 9 my $self = shift;
181  
182 9 my $fd = $self->handle;
183 9 seek $fd, 0, 0;
184 9 return $self;
185 };
186  
187 # TODO 0.30 kill the tempfile, if any?
188 # sub DESTROY { };
189  
190 =head1 LICENSE AND COPYRIGHT
191  
192 This module is part of L<MVC::Neaf> suite.
193  
194 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
195  
196 This program is free software; you can redistribute it and/or modify it
197 under the terms of either: the GNU General Public License as published
198 by the Free Software Foundation; or the Artistic License.
199  
200 See L<http://dev.perl.org/licenses/> for more information.
201  
202 =cut
203  
204 97 1;
« Neaf/Request.pm ↑ Summary Neaf/X/Form.pm »