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