Updated catalyst.pl
[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
38=over 4
39
cd3bb248 40=item $upload->new
41
bab3a22c 42Simple constructor.
cd3bb248 43
3ffaf022 44=item $upload->copy_to
47ae6960 45
bab3a22c 46Copies the temporary file using L<File::Copy>. Returns true for success,
47false for failure.
47ae6960 48
3ffaf022 49 $upload->copy_to('/path/to/target');
50
47ae6960 51=cut
52
3ffaf022 53sub copy_to {
c462faf0 54 my $self = shift;
55 return File::Copy::copy( $self->tempname, @_ );
47ae6960 56}
57
146554c5 58=item $upload->fh
59
bab3a22c 60Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
146554c5 61
62=cut
63
64sub fh {
65 my $self = shift;
66
a2f2cde9 67 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
4be535b1 68
a2f2cde9 69 unless ( defined $fh ) {
4be535b1 70
a2f2cde9 71 my $filename = $self->tempname;
4be535b1 72
a2f2cde9 73 Catalyst::Exception->throw(
4be535b1 74 message => qq/Can't open '$filename': '$!'/ );
a2f2cde9 75 }
146554c5 76
77 return $fh;
78}
79
80=item $upload->filename
81
bab3a22c 82Returns the client-supplied filename.
146554c5 83
4be535b1 84=item $upload->headers
85
bab3a22c 86Returns an L<HTTP::Headers> object for the request.
4be535b1 87
3ffaf022 88=item $upload->link_to
146554c5 89
bab3a22c 90Creates a hard link to the temporary file. Returns true for success,
91false for failure.
146554c5 92
3ffaf022 93 $upload->link_to('/path/to/target');
146554c5 94
95=cut
96
3ffaf022 97sub link_to {
47ae6960 98 my ( $self, $target ) = @_;
5c0ff128 99 return CORE::link( $self->tempname, $target );
146554c5 100}
101
102=item $upload->size
103
bab3a22c 104Returns the size of the uploaded file in bytes.
146554c5 105
32d4bba8 106=item $upload->slurp
107
bab3a22c 108Returns a scalar containing the contents of the temporary file.
32d4bba8 109
110=cut
111
112sub slurp {
113 my ( $self, $layer ) = @_;
114
4be535b1 115 unless ($layer) {
32d4bba8 116 $layer = ':raw';
117 }
118
119 my $content = undef;
120 my $handle = $self->fh;
121
122 binmode( $handle, $layer );
123
124 while ( $handle->sysread( my $buffer, 8192 ) ) {
125 $content .= $buffer;
126 }
127
128 return $content;
129}
130
146554c5 131=item $upload->tempname
132
bab3a22c 133Returns the path to the temporary file.
146554c5 134
135=item $upload->type
136
bab3a22c 137Returns the client-supplied Content-Type.
146554c5 138
139=back
140
bab3a22c 141=head1 AUTHORS
146554c5 142
143Sebastian Riedel, C<sri@cpan.org>
bab3a22c 144
146554c5 145Christian Hansen, C<ch@ngmedia.com>
146
147=head1 COPYRIGHT
148
149This program is free software, you can redistribute it and/or modify
150it under the same terms as Perl itself.
151
152=cut
153
1541;