| 1 | |||||||||||
| 2 | package ASP4::FileUpload; | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Carp 'confess'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | sub new | ||||||||||
| 10 | 2 | ▶ | { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 11 | 2 | my ($class, %args) = @_; | |||||||||
| 12 | |||||||||||
| 13 | 2 | foreach(qw( ContentType FileHandle FileName )) | |||||||||
| 14 | { | ||||||||||
| 15 | confess "Required param '$_' was not provided" | ||||||||||
| 16 | 6 | ▶ | unless $args{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | }# end foreach() | ||||||||||
| 18 | |||||||||||
| 19 | 2 | $args{UploadedFileName} = $args{FileName}; | |||||||||
| 20 | 2 | ($args{FileName}) = $args{FileName} =~ m{[/\\]?([^/\\]+)$}; | |||||||||
| 21 | 2 | ($args{FileExtension}) = $args{FileName} =~ m/([^\.]+)$/; | |||||||||
| 22 | 2 | $args{FileSize} = (stat($args{FileHandle}))[7]; | |||||||||
| 23 | |||||||||||
| 24 | 2 | return bless \%args, $class; | |||||||||
| 25 | }# end new() | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | # Public readonly properties: | ||||||||||
| 29 | 0 | ▶ | sub ContentType { shift->{ContentType} } | ||||||||
|
Subroutine
ContentType: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 30 | 2 | ▶ | sub FileName { shift->{FileName} } | ||||||||
|
Subroutine
FileName: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 31 | 0 | ▶ | sub UploadedFileName { shift->{UploadedFileName} } | ||||||||
|
Subroutine
UploadedFileName: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 32 | 2 | ▶ | sub FileExtension { shift->{FileExtension} } | ||||||||
|
Subroutine
FileExtension: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 33 | 2 | ▶ | sub FileSize { shift->{FileSize} } | ||||||||
|
Subroutine
FileSize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | |||||||||||
| 35 | sub FileContents | ||||||||||
| 36 | 2 | ▶ | { | ||||||||
|
Subroutine
FileContents: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | 2 | my $s = shift; | |||||||||
| 38 | 2 | local $/; | |||||||||
| 39 | 2 | my $ifh = $s->FileHandle; | |||||||||
| 40 | 2 | return scalar(<$ifh>); | |||||||||
| 41 | }# end FileContents() | ||||||||||
| 42 | |||||||||||
| 43 | sub FileHandle | ||||||||||
| 44 | 6 | ▶ | { | ||||||||
|
Subroutine
FileHandle: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 45 | 6 | my $s = shift; | |||||||||
| 46 | 6 | my $ifh = $s->{FileHandle}; | |||||||||
| 47 | 6 | ▶ | seek($ifh,0,0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | or confess "Cannot seek to the beginning of filehandle '$ifh': $!"; | ||||||||||
| 49 | 6 | return $ifh; | |||||||||
| 50 | }# end FileHandle() | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | # Public methods: | ||||||||||
| 54 | sub SaveAs | ||||||||||
| 55 | 2 | ▶ | { | ||||||||
|
Subroutine
SaveAs: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 56 | 2 | my ($s, $path) = @_; | |||||||||
| 57 | |||||||||||
| 58 | # Create the file path if it doesn't yet exist: | ||||||||||
| 59 | 2 | my $folder = ""; | |||||||||
| 60 | 2 | my @parts = grep { $_ } split /\//, $path; | |||||||||
| 61 | 2 | pop(@parts); | |||||||||
| 62 | 2 | for( @parts ) | |||||||||
| 63 | { | ||||||||||
| 64 | 2 | $folder .= "/$_"; | |||||||||
| 65 | 2 | ▶ | unless( -d $folder ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | { | ||||||||||
| 67 | 0 | mkdir( $folder, 0777 ); | |||||||||
| 68 | }# end unless() | ||||||||||
| 69 | }# end for() | ||||||||||
| 70 | |||||||||||
| 71 | 2 | ▶ | open my $ofh, '>', $path | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | or confess "Cannot open '$path' for writing: $!"; | ||||||||||
| 73 | 2 | my $ifh = $s->FileHandle; | |||||||||
| 74 | 2 | ▶ | while( my $line = <$ifh> ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | { | ||||||||||
| 76 | 200 | print $ofh $line; | |||||||||
| 77 | }# end while() | ||||||||||
| 78 | 2 | close($ofh); | |||||||||
| 79 | |||||||||||
| 80 | 2 | return 1; | |||||||||
| 81 | }# end SaveAs() | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | sub DESTROY | ||||||||||
| 85 | 2 | ▶ | { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 86 | 2 | my $s = shift; | |||||||||
| 87 | 2 | my $ifh = $s->FileHandle; | |||||||||
| 88 | 2 | close($ifh); | |||||||||
| 89 | 2 | undef(%$s); | |||||||||
| 90 | }# end DESTROY() | ||||||||||
| 91 | |||||||||||
| 92 | 1 | 1;# return true: | |||||||||
| 93 | |||||||||||
| 94 | =pod | ||||||||||
| 95 | |||||||||||
| 96 | =head1 NAME | ||||||||||
| 97 | |||||||||||
| 98 | ASP4::FileUpload - Simple interface for handling File Uploads | ||||||||||
| 99 | |||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||
| 101 | |||||||||||
| 102 | # In your handler: | ||||||||||
| 103 | sub run { | ||||||||||
| 104 | my ($s, $context) = @_; | ||||||||||
| 105 | |||||||||||
| 106 | if( my $file = $Request->FileUpload('fieldname') ) { | ||||||||||
| 107 | |||||||||||
| 108 | # Save the file: | ||||||||||
| 109 | $file->SaveAs('/var/media/uploads/budget.csv'); | ||||||||||
| 110 | |||||||||||
| 111 | # Some info about it: | ||||||||||
| 112 | warn $file->UploadedFileName; # C:\Users\billg\budget.csv | ||||||||||
| 113 | warn $file->FileName; # budget.csv | ||||||||||
| 114 | warn $file->FileExtension; # csv | ||||||||||
| 115 | warn $file->FileSize; # 273478 (Calculated via (stat(FH))[7] ) | ||||||||||
| 116 | warn $file->ContentType; # text/csv | ||||||||||
| 117 | warn $file->FileContents; # (The contents of the file) | ||||||||||
| 118 | my $ifh = $file->FileHandle; # A normal, plain old filehandle | ||||||||||
| 119 | } | ||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | =head1 DESCRIPTION | ||||||||||
| 123 | |||||||||||
| 124 | This class provides a simple interface to uploaded files in ASP4. | ||||||||||
| 125 | |||||||||||
| 126 | =head1 PUBLIC PROPERTIES | ||||||||||
| 127 | |||||||||||
| 128 | =head2 UploadedFileName | ||||||||||
| 129 | |||||||||||
| 130 | The name of the file - as uploaded by the user. For example, if the user was on | ||||||||||
| 131 | Windows, it might look like C<C:\Users\billg\Desktop\file.txt> | ||||||||||
| 132 | |||||||||||
| 133 | =head2 Filename | ||||||||||
| 134 | |||||||||||
| 135 | The name of the file itself - eg: C<file.txt> | ||||||||||
| 136 | |||||||||||
| 137 | =head2 FileExtension | ||||||||||
| 138 | |||||||||||
| 139 | If the filename is C<file.txt>, C<FileExtension> would return C<txt>. | ||||||||||
| 140 | |||||||||||
| 141 | =head2 FileSize | ||||||||||
| 142 | |||||||||||
| 143 | The size of the uploaded file in bytes. | ||||||||||
| 144 | |||||||||||
| 145 | =head2 FileHandle | ||||||||||
| 146 | |||||||||||
| 147 | Returns a filehandle (open for reading) pointing to the uploaded file. | ||||||||||
| 148 | |||||||||||
| 149 | =head2 ContentType | ||||||||||
| 150 | |||||||||||
| 151 | The C<content-type> header supplied by the browser for the uploaded file. | ||||||||||
| 152 | |||||||||||
| 153 | =head2 FileContents | ||||||||||
| 154 | |||||||||||
| 155 | The contents of the uploaded file. | ||||||||||
| 156 | |||||||||||
| 157 | =head1 PUBLIC METHODS | ||||||||||
| 158 | |||||||||||
| 159 | =head2 SaveAs( $path ) | ||||||||||
| 160 | |||||||||||
| 161 | Writes the contents of the uploaded file to C<$path>. Will throw an exception if | ||||||||||
| 162 | something goes wrong. | ||||||||||
| 163 | |||||||||||
| 164 | =head1 BUGS | ||||||||||
| 165 | |||||||||||
| 166 | It's possible that some bugs have found their way into this release. | ||||||||||
| 167 | |||||||||||
| 168 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports. | ||||||||||
| 169 | |||||||||||
| 170 | =head1 HOMEPAGE | ||||||||||
| 171 | |||||||||||
| 172 | Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples | ||||||||||
| 173 | of ASP4 in action. | ||||||||||
| 174 | |||||||||||
| 175 | =cut | ||||||||||
| 176 | |||||||||||