line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package ASP4::FileUpload; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
48
|
|
5
|
1
|
|
|
1
|
|
7
|
use warnings 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
68
|
|
6
|
1
|
|
|
1
|
|
7
|
use Carp 'confess'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
892
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new |
10
|
|
|
|
|
|
|
{ |
11
|
2
|
|
|
2
|
0
|
9
|
my ($class, %args) = @_; |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
|
|
6
|
foreach(qw( ContentType FileHandle FileName )) |
14
|
|
|
|
|
|
|
{ |
15
|
|
|
|
|
|
|
confess "Required param '$_' was not provided" |
16
|
6
|
50
|
|
|
|
16
|
unless $args{$_}; |
17
|
|
|
|
|
|
|
}# end foreach() |
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
|
|
5
|
$args{UploadedFileName} = $args{FileName}; |
20
|
2
|
|
|
|
|
20
|
($args{FileName}) = $args{FileName} =~ m{[/\\]?([^/\\]+)$}; |
21
|
2
|
|
|
|
|
14
|
($args{FileExtension}) = $args{FileName} =~ m/([^\.]+)$/; |
22
|
2
|
|
|
|
|
25
|
$args{FileSize} = (stat($args{FileHandle}))[7]; |
23
|
|
|
|
|
|
|
|
24
|
2
|
|
|
|
|
25
|
return bless \%args, $class; |
25
|
|
|
|
|
|
|
}# end new() |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Public readonly properties: |
29
|
0
|
|
|
0
|
1
|
0
|
sub ContentType { shift->{ContentType} } |
30
|
2
|
|
|
2
|
0
|
9
|
sub FileName { shift->{FileName} } |
31
|
0
|
|
|
0
|
1
|
0
|
sub UploadedFileName { shift->{UploadedFileName} } |
32
|
2
|
|
|
2
|
1
|
9
|
sub FileExtension { shift->{FileExtension} } |
33
|
2
|
|
|
2
|
1
|
5
|
sub FileSize { shift->{FileSize} } |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub FileContents { |
36
|
2
|
|
|
2
|
1
|
4
|
my $s = shift; |
37
|
2
|
|
|
|
|
9
|
local $/; |
38
|
2
|
|
|
|
|
6
|
my $ifh = $s->FileHandle; |
39
|
2
|
|
|
|
|
35
|
return scalar(<$ifh>); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub FileHandle { |
43
|
6
|
|
|
6
|
1
|
6
|
my $s = shift; |
44
|
6
|
|
|
|
|
8
|
my $ifh = $s->{FileHandle}; |
45
|
6
|
50
|
|
|
|
36
|
seek($ifh,0,0) |
46
|
|
|
|
|
|
|
or confess "Cannot seek to the beginning of filehandle '$ifh': $!"; |
47
|
6
|
|
|
|
|
10
|
return $ifh; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Public methods: |
52
|
|
|
|
|
|
|
sub SaveAs |
53
|
|
|
|
|
|
|
{ |
54
|
2
|
|
|
2
|
1
|
3
|
my ($s, $path) = @_; |
55
|
|
|
|
|
|
|
|
56
|
2
|
50
|
|
|
|
302
|
open my $ofh, '>', $path |
57
|
|
|
|
|
|
|
or confess "Cannot open '$path' for writing: $!"; |
58
|
2
|
|
|
|
|
8
|
my $ifh = $s->FileHandle; |
59
|
2
|
|
|
|
|
38
|
while( my $line = <$ifh> ) |
60
|
|
|
|
|
|
|
{ |
61
|
200
|
|
|
|
|
359
|
print $ofh $line; |
62
|
|
|
|
|
|
|
}# end while() |
63
|
2
|
|
|
|
|
105
|
close($ofh); |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
11
|
return 1; |
66
|
|
|
|
|
|
|
}# end SaveAs() |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub DESTROY |
70
|
|
|
|
|
|
|
{ |
71
|
2
|
|
|
2
|
|
3
|
my $s = shift; |
72
|
2
|
|
|
|
|
5
|
my $ifh = $s->FileHandle; |
73
|
2
|
|
|
|
|
15
|
close($ifh); |
74
|
2
|
|
|
|
|
12
|
undef(%$s); |
75
|
|
|
|
|
|
|
}# end DESTROY() |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1;# return true: |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=pod |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 NAME |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
ASP4::FileUpload - Simple interface for handling File Uploads |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 SYNOPSIS |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# In your handler: |
88
|
|
|
|
|
|
|
sub run { |
89
|
|
|
|
|
|
|
my ($s, $context) = @_; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
if( my $file = $Request->FileUpload('fieldname') ) { |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Save the file: |
94
|
|
|
|
|
|
|
$file->SaveAs('/var/media/uploads/budget.csv'); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Some info about it: |
97
|
|
|
|
|
|
|
warn $file->UploadedFileName; # C:\Users\billg\budget.csv |
98
|
|
|
|
|
|
|
warn $file->FileName; # budget.csv |
99
|
|
|
|
|
|
|
warn $file->FileExtension; # csv |
100
|
|
|
|
|
|
|
warn $file->FileSize; # 273478 (Calculated via (stat(FH))[7] ) |
101
|
|
|
|
|
|
|
warn $file->ContentType; # text/csv |
102
|
|
|
|
|
|
|
warn $file->FileContents; # (The contents of the file) |
103
|
|
|
|
|
|
|
my $ifh = $file->FileHandle; # A normal, plain old filehandle |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 DESCRIPTION |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
This class provides a simple interface to uploaded files in ASP4. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 PUBLIC PROPERTIES |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 UploadedFileName |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
The name of the file - as uploaded by the user. For example, if the user was on |
116
|
|
|
|
|
|
|
Windows, it might look like C |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 Filename |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
The name of the file itself - eg: C |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 FileExtension |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
If the filename is C, C would return C. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 FileSize |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
The size of the uploaded file in bytes. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 FileHandle |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Returns a filehandle (open for reading) pointing to the uploaded file. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 ContentType |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
The C header supplied by the browser for the uploaded file. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 FileContents |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
The contents of the uploaded file. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 PUBLIC METHODS |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 SaveAs( $path ) |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Writes the contents of the uploaded file to C<$path>. Will throw an exception if |
147
|
|
|
|
|
|
|
something goes wrong. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
150
|
|
|
|
|
|
|
|