| 1 | |||
| 2 | package ASP4::FileUpload; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings 'all'; | ||
| 6 | use Carp 'confess'; | ||
| 7 | |||
| 8 | |||
| 9 | sub new | ||
| 10 | { | ||
| 11 | my ($class, %args) = @_; | ||
| 12 | |||
| 13 | foreach(qw( ContentType FileHandle FileName )) | ||
| 14 | { | ||
| 15 | confess "Required param '$_' was not provided" | ||
| 16 | unless $args{$_}; | ||
| 17 | }# end foreach() | ||
| 18 | |||
| 19 | $args{UploadedFileName} = $args{FileName}; | ||
| 20 | ($args{FileName}) = $args{FileName} =~ m{[/\\]?([^/\\]+)$}; | ||
| 21 | ($args{FileExtension}) = $args{FileName} =~ m/([^\.]+)$/; | ||
| 22 | $args{FileSize} = (stat($args{FileHandle}))[7]; | ||
| 23 | |||
| 24 | return bless \%args, $class; | ||
| 25 | }# end new() | ||
| 26 | |||
| 27 | |||
| 28 | # Public readonly properties: | ||
| 29 | sub ContentType { shift->{ContentType} } | ||
| 30 | sub FileName { shift->{FileName} } | ||
| 31 | sub UploadedFileName { shift->{UploadedFileName} } | ||
| 32 | sub FileExtension { shift->{FileExtension} } | ||
| 33 | sub FileSize { shift->{FileSize} } | ||
| 34 | |||
| 35 | sub FileContents { | ||
| 36 | my $s = shift; | ||
| 37 | local $/; | ||
| 38 | my $ifh = $s->FileHandle; | ||
| 39 | return scalar(<$ifh>); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub FileHandle { | ||
| 43 | my $s = shift; | ||
| 44 | my $ifh = $s->{FileHandle}; | ||
| 45 | seek($ifh,0,0) | ||
| 46 | or confess "Cannot seek to the beginning of filehandle '$ifh': $!"; | ||
| 47 | return $ifh; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | # Public methods: | ||
| 52 | sub SaveAs | ||
| 53 | { | ||
| 54 | my ($s, $path) = @_; | ||
| 55 | |||
| 56 | open my $ofh, '>', $path | ||
| 57 | or confess "Cannot open '$path' for writing: $!"; | ||
| 58 | my $ifh = $s->FileHandle; | ||
| 59 | while( my $line = <$ifh> ) | ||
| 60 | { | ||
| 61 | print $ofh $line; | ||
| 62 | }# end while() | ||
| 63 | close($ofh); | ||
| 64 | |||
| 65 | return 1; | ||
| 66 | }# end SaveAs() | ||
| 67 | |||
| 68 | |||
| 69 | sub DESTROY | ||
| 70 | { | ||
| 71 | my $s = shift; | ||
| 72 | my $ifh = $s->FileHandle; | ||
| 73 | close($ifh); | ||
| 74 | 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<C:\Users\billg\Desktop\file.txt> | ||
| 117 | |||
| 118 | =head2 Filename | ||
| 119 | |||
| 120 | The name of the file itself - eg: C<file.txt> | ||
| 121 | |||
| 122 | =head2 FileExtension | ||
| 123 | |||
| 124 | If the filename is C<file.txt>, C<FileExtension> would return C<txt>. | ||
| 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<content-type> 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 |