Updated uri_for signature
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
CommitLineData
146554c5 1package Catalyst::Request::Upload;
2
3use strict;
4use base 'Class::Accessor::Fast';
5
a2f2cde9 6use Catalyst::Exception;
47ae6960 7use File::Copy ();
8use IO::File ();
146554c5 9
4be535b1 10__PACKAGE__->mk_accessors(qw/filename headers size tempname type/);
146554c5 11
b549b049 12sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
146554c5 13
14=head1 NAME
15
bab3a22c 16Catalyst::Request::Upload - handles file upload requests
146554c5 17
18=head1 SYNOPSIS
19
bab3a22c 20 $upload->copy_to;
21 $upload->fh;
146554c5 22 $upload->filename;
4be535b1 23 $upload->headers;
3ffaf022 24 $upload->link_to;
146554c5 25 $upload->size;
32d4bba8 26 $upload->slurp;
146554c5 27 $upload->tempname;
28 $upload->type;
29
30See also L<Catalyst>.
31
32=head1 DESCRIPTION
33
bab3a22c 34This class provides accessors and methods to handle client upload requests.
146554c5 35
36=head1 METHODS
37
b5ecfcf0 38=head2 $upload->new
cd3bb248 39
bab3a22c 40Simple constructor.
cd3bb248 41
b5ecfcf0 42=head2 $upload->copy_to
47ae6960 43
bab3a22c 44Copies the temporary file using L<File::Copy>. Returns true for success,
45false for failure.
47ae6960 46
3ffaf022 47 $upload->copy_to('/path/to/target');
48
47ae6960 49=cut
50
3ffaf022 51sub copy_to {
c462faf0 52 my $self = shift;
53 return File::Copy::copy( $self->tempname, @_ );
47ae6960 54}
55
b5ecfcf0 56=head2 $upload->fh
146554c5 57
bab3a22c 58Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
146554c5 59
60=cut
61
62sub fh {
63 my $self = shift;
64
a2f2cde9 65 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
4be535b1 66
a2f2cde9 67 unless ( defined $fh ) {
4be535b1 68
a2f2cde9 69 my $filename = $self->tempname;
4be535b1 70
a2f2cde9 71 Catalyst::Exception->throw(
4be535b1 72 message => qq/Can't open '$filename': '$!'/ );
a2f2cde9 73 }
146554c5 74
75 return $fh;
76}
77
b5ecfcf0 78=head2 $upload->filename
146554c5 79
bab3a22c 80Returns the client-supplied filename.
146554c5 81
b5ecfcf0 82=head2 $upload->headers
4be535b1 83
bab3a22c 84Returns an L<HTTP::Headers> object for the request.
4be535b1 85
b5ecfcf0 86=head2 $upload->link_to
146554c5 87
bab3a22c 88Creates a hard link to the temporary file. Returns true for success,
89false for failure.
146554c5 90
3ffaf022 91 $upload->link_to('/path/to/target');
146554c5 92
93=cut
94
3ffaf022 95sub link_to {
47ae6960 96 my ( $self, $target ) = @_;
5c0ff128 97 return CORE::link( $self->tempname, $target );
146554c5 98}
99
b5ecfcf0 100=head2 $upload->size
146554c5 101
bab3a22c 102Returns the size of the uploaded file in bytes.
146554c5 103
b5ecfcf0 104=head2 $upload->slurp
32d4bba8 105
bab3a22c 106Returns a scalar containing the contents of the temporary file.
32d4bba8 107
108=cut
109
110sub slurp {
111 my ( $self, $layer ) = @_;
112
4be535b1 113 unless ($layer) {
32d4bba8 114 $layer = ':raw';
115 }
116
117 my $content = undef;
118 my $handle = $self->fh;
119
120 binmode( $handle, $layer );
121
122 while ( $handle->sysread( my $buffer, 8192 ) ) {
123 $content .= $buffer;
124 }
125
126 return $content;
127}
128
b5ecfcf0 129=head2 $upload->tempname
146554c5 130
bab3a22c 131Returns the path to the temporary file.
146554c5 132
b5ecfcf0 133=head2 $upload->type
146554c5 134
bab3a22c 135Returns the client-supplied Content-Type.
146554c5 136
bab3a22c 137=head1 AUTHORS
146554c5 138
139Sebastian Riedel, C<sri@cpan.org>
bab3a22c 140
146554c5 141Christian Hansen, C<ch@ngmedia.com>
142
143=head1 COPYRIGHT
144
145This program is free software, you can redistribute it and/or modify
146it under the same terms as Perl itself.
147
148=cut
149
1501;